18

I've got an MVC solution that hosts a few routes for Web API services. In some situations, I will call these from JavaScript with a simple HTTP get. In others, I want to call them from some .NET code, perhaps another MVC application.

Is there a way to add a service reference to these Web API endpoints and have the tooling create the proxy client and CLR types as it would do with a typical WCF service? I know there is no SOAP involved here but I did read that it is possible, just not how.

3 Answers 3

18

No it is REST service. REST service doesn't expose metadata for creating proxy by service reference (except WCF Data Services which have some special form of metadata). Use Web-API's HttpClient class to call the service.

4
  • Thanks for the response. So there isn't a metadata schema exposed as such, but do you think one could be derived from the response much like xsd does for XML. Adding a service reference would need to derive some sort of schema, build a clr class based on this and the client would essentially deserialise to a collection of this. Is this something that the tooling might practically support to give the flexibility of using this type of service at design time Jul 13, 2011 at 15:46
  • It's a pretty lame excuse. The (typical) scenario here is that you're calling your own service - and you always have the metadata for your own service. This is merely an issue of inferior tooling. Matters become worse if you want OData: Then you will need to write your own linq provider to get the expressiveness you had with good old WCF Services. Better tooling could change that.
    – John
    Aug 22, 2014 at 10:29
  • @John: There is plenty of UserVoice sites run by MS. Try to rise request (if it doesn't exist already) to add support for WADL or WSDL2 description for REST services. Once the description is available, you can also have a generation for the client. Aug 22, 2014 at 10:30
  • @LadislavMrnka After some more research I found the project "WebApiProxy", which generates client code, but it's not exactly high-profile and so far only c# and javascript are supported. Having descriptions online with the service may be an additional benefit, but I see it more as an afterthought.
    – John
    Aug 22, 2014 at 10:42
8

We don't have any standard mechanism for doing that. REST is about building systems that alllow clients to evolve independently of the server. HTTP defines a uniform interface of GET, PUT, POST, DELETE, etc thus there is no need for a method description. For both reasons there is no REST WSDL equivalent, or I should say no equivalent that has really gotten momentum among the REST community (i.e. there is WADL).

The point of coupling in REST services is really around the media type / the body format. For that we do support a strongly typed mechanism. In Web API we ship an HttpClient (HttpClient on Nuget) that allows you to take a CLR type and transform into some representation. Out of the box it supports XML and JSON.

Thus you could create a CLR type and share it with clients, and then use HttpClient on the client.

To create the type itself there are also several options.

  1. Create it by hand
  2. Use the "Paste as Xml" tool and use web api's automatic help page feature to copy/paste.
1

Not directly but from the few samples I've seen, using Web Api involves setting up a ServiceContract. It seems that if you add a second service contract interface with the regular OperationContract & DataContract attributes as required then you can create an endpoint with a standard WCF binding of your choice and its matching MEX endpoint. The service would implement both interfaces so the add Service Reference can get a WSDL document from the standard WCF endpoint.

1
  • There are some details to consider, such as serialization (should be the same in both cases to avoid impedance), duplicate method attributes (those are not compatible between the frameworks), also authentication and authorization. I would suspect it's a hassle not worth the benefits, but it's an interesting idea nonetheless.
    – John
    Aug 22, 2014 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.