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?
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 .
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With