Visual Studio + Webservices + SOAP Headers

2011, May 12    

Suppose you have to consume a Webservice that requires you to provide a custom header. If you generate automatically the proxy with Visual Studio, you’ll soon discover that there’s no way to add an header or to access the SOAP request.
Ok, here’s what you have to do:

1) download the Web Services Enhancements 3.0 and reference microsoft.web.services3.dll in your project

2) change the base class of your proxy from <strong>System.Web.Services.Protocols.SoapHttpClientProtocol</strong>to <strong>Microsoft.Web.Services3.WebServicesClientProtocol</strong>

3) Use the newly exposed SoapContext property to add your custom headers and/or security tokens

Just keep in mind that if you ask Visual Studio to generate again the proxy, you’ll loose the changes…

Did you like this post? Then