Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 896
Daniel Revell Avatar asked Jul 13 '11 14:07

Daniel Revell


People also ask

How do I add a service reference to Web API?

To add a Web ReferenceOn the Project menu, click Add Web Reference. In the URL box of the Add Web Reference dialog box, type the URL to obtain the service description of the Excel Web Services, such as http://<server>/<customsite>/_vti_bin/excelservice.asmx or http://<server>/_vti_bin/excelservice.asmx .

How do I add an existing service reference?

In Solution Explorer, right-click the name of the project to which you want to add the service, and then click Add Service Reference. The Add Service Reference dialog box appears. In the Address box, enter the URL for the service, and then click Go to search for the service.

How do I add a web service reference in Visual Studio 2019 net core?

In Solution Explorer, right-click the name of the project to add the Web service to and then click Add Web Reference. The Add Web Reference dialog box is displayed. In the URL box, enter the URL of the Web service to use. If you do not know the URL, use the links in the browse pane to locate the Web service you want.

How do I add a reference to Web services Asmx in asp net using Visual Studio?

On the Project menu, select Add Web Reference. In the Add Web Reference dialog box, type the URL for the web service in the Address text box and press ENTER. If you set the local computer to host the web service, the URL is http://localhost/MathService/MathService.asmx . Select Add Reference.


3 Answers

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.

like image 182
Ladislav Mrnka Avatar answered Oct 19 '22 18:10

Ladislav Mrnka


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.
like image 44
Glenn Block Avatar answered Oct 19 '22 19:10

Glenn Block


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.

like image 1
Sixto Saez Avatar answered Oct 19 '22 19:10

Sixto Saez