开发者

Add service reference to ASP.NET Web API service

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开发者_StackOverflow中文版 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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜