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

7 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
  3. Pingback: How to perform a SOAP Web Service Request in ASP.NET C# without using WSDL, proxy classes or SoapClient | Web Developer
  4. Re How to perform a SOAP Web Service Request in ASP.NET C# without using WSDL, proxy classes or SoapClient

    This is very useful but I’m looking for a solution which works for SOAP 1.2 with WCF (using .svc). I have played around with your solution but without success.

    I would appreciate any guidance.

  5. Hi, this is very useful, thanks a lot. Can you tell how can i insert another variable in a higher section?

  6. Something like this:

    soap:Body
    tem:Section1
    tem:token
    INSERT ANOTHER VARIABLE HERE
    /tem:token
    tem:Section2
    {0} xmlns=””{1}””>{2}</{0}
    /tem:Section2
    /tem:Section1
    /soap:Body

    1. Just add another Dictionary parameter to the SendSOAPRequest method (call it “tokenParameters” or something like that) and replicate the same string.Format behavior: 1 dictionary for the tem:token attribute contents, and a 2nd dictionary for the tem:Section2 contents.

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.

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