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!
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.
Hi, this is very useful, thanks a lot. Can you tell how can i insert another variable in a higher section?
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
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.