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!

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

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

  1. Pingback: WCF Web Service on Win2012 and IIS8 - 404 File Not Found Error - Fix
  2. Pingback: Create a SOAP Web Service using ASP.NET WCF, Visual Studio and IIS 8

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.