Quite an old post but can be usefull for others.
When integrating the WSDL in Visual Studio, you get a new class deriving from SoapHttpClientProtocol
within a new namespace.
Fortunatelly, this class is partial, this means you can extend it in your code so that the modifications you make are not overwritten when you refresh from the WSDL.
My WSDL generated class is :
namespace TheServiceNameSpace {
public partial class TheClassName : System.Web.Services.Protocols.SoapHttpClientProtocol {
//Class Code
}
}
To add the custom header, I add to my project a new cs file with the following content :
namespace TheServiceNameSpace
{
public partial class SecurityHeader : SoapHeader
{
public String username { get; set; }
public String password { get; set; }
public String apikey { get; set; }
}
public partial class TheClassName
{
public SecurityHeader SEC = new SecurityHeader() { username = "xxxx", password = "xxxx", apikey = "xxxx" };
protected override XmlWriter GetWriterForMessage(SoapClientMessage message, int bufferSize)
{
message.Headers.Add(SEC);
return base.GetWriterForMessage(message, bufferSize);
}
}
}
What it does, is :
- 1 - Create a custom SOAP Header that contains the expected variables
- 2 - Extend the generated class to add a reference to the new header
- 3 - Override the GetWriterForMessage method so that I can add my header in the request that will be sent to the service.