Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking a web service - asmx - through a Microsoft Web API end point?

I am using Microsoft ASP.NET Web API 2 and one of my end points has to internally invoke a legacy asmx web service.

Is this the correct way?

...................................................
WebRequest req = WebRequest.Create(myWebServiceURL);

req.Method = "POST";
eq.ContentType = "application/x-www-form-urlencoded";

string postData = whateverDataNeedsToBePosted;

using ( Stream reqStream = req.GetRequestStream() )
      {
            reqStream.Write( new ASCIIEncoding().GetBytes( postData ),
                              0, postData.Length );
            reqStream.Close();
       }
 WebResponse resp = req.GetResponse();

................................................

UPDATE: I do have a bunch of non-Microsoft technology web services (no asnx or svc). Is the above method good enough for those type of services?

like image 926
Sartorial Avatar asked Dec 31 '13 20:12

Sartorial


People also ask

How do I add Asmx to my web service?

Please refer to the document to add Web Service (ASMX) file in VS 2022: create ASP.NET Web Application(. NET Framework) project > right-click your project > Add > New Item… > search Web Service (ASMX) > click on Add .

How does Asmx Web service work?

ASMX file is used to add Web Services logic to the methods visible by the client application and it acts as the base URL for clients calling the XML web service. This file is generated when we add a Web Service application to a client application. This file describes the Web Services interface available to the client.

What is implemented using Service1 Asmx file of a Web service?

In the Service1. asmx, we will see that the Service uses the WebService directive with the attribute. From here, this will show us that the application invokes the Service, not by the end-user.


1 Answers

That will work, but you're making your life difficult :)

You can add a Service Reference to your project in Visual Studio and call methods in the ASMX service just like you would call methods in a referenced DLL. See this article.

UPDATE:

Yes, your method for calling other services will work, but I would check to see if adding your other services as Service References to your project works first. The service reference feature works with all kinds of protocols (whether built with Microsoft technologies or not).

like image 57
jebar8 Avatar answered Oct 30 '22 03:10

jebar8