Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top