Site icon Ryadel

How to perform a SOAP Web Service Request in ASP.NET C# without using WSDL, proxy classes or SoapClient

Classe ASP.NET C# per il controllo e il calcolo formale del Codice Fiscale

If you are a well-seasoned ASP.NET Web Service developer you most certainly know about the SoapClient class and how it can be used to send SOAP messages using transport-independent protocols using two main methods:

  • The ASP.NET services, which is old way of doing SOA
  • The WCF framework, which is the "latest" and "newest" way to do that.

We already talked about these approaches in a couple posts some months ago (read here and here for further info). However, there could be some edge-case scenarios where you want (or need) to call a SOAP-based web service without using the WSDL and/or the Add Service Reference Visual Studio feature, maybe because you can't possibly put the code bloat it produces into your code base.

When such need arises, the following helper class might be precisely what you're looking for:

As you can see, the above code can be used to issue a manual SOAP-based request using a standard WebRequest C# class: to emphasize the fact that this is an home-cooked, emergency-level code, we also chose to go with manually (un)typed XML blocks, avoiding XMLDocument, XmlWriter, System.Xml and other C# native approaches.

Here's how you can use it in your ASP.NET / ASP.NET Core project:

As we can see, we're issuing a sample request to an ASMX web service, which has a WSDL that will require a specific soapAction property value: whenever the SOAP Web Service you're connecting to requires a SOAPAction equals to the endpoint URL, you can leave that property set to null. When in doubt, take a look at the WSDL and act accordingly.

That's it for now: happy coding!

 

Exit mobile version