6

I have been trying to add a header to SOAP request as follows

<soapenv:Header>
     <UsernameToken xmlns="http://test.com/webservices">username</UsernameToken>
     <PasswordText xmlns="http://test.com/webservices">password</PasswordText>
     <SessionType xmlns="http://test.com/webservices">None</SessionType>
</soapenv:Header>

I have found suggestions to use SoapHeader to include header values, but introduces another level such as

<soapenv:Header>
    <CustomHeader>
        <UsernameToken xmlns="http://test.com/webservices">username</UsernameToken>
        <PasswordText xmlns="http://test.com/webservices">password</PasswordText>
        <SessionType xmlns="http://test.com/webservices">None</SessionType>
    </CustomHeader>
</soapenv:Header>

Can anyone suggest how I can form a request without CustomHeader.

Anupdas
  • 10,211
  • 2
  • 35
  • 60

1 Answers1

11

Try to use this one

private static void Main()
{
    using (var client = new ServiceClient())
    using (var scope = new OperationContextScope(client.InnerChannel))
    {
        MessageHeader usernameTokenHeader = MessageHeader.CreateHeader("UsernameToken",
            "http://test.com/webservices", "username");
        OperationContext.Current.OutgoingMessageHeaders.Add(usernameTokenHeader);

        MessageHeader passwordTextHeader = MessageHeader.CreateHeader("PasswordText",
            "http://test.com/webservices", "password");
        OperationContext.Current.OutgoingMessageHeaders.Add(passwordTextHeader);

        MessageHeader sessionTypeHeader = MessageHeader.CreateHeader("SessionType",
            "http://test.com/webservices", "None");
        OperationContext.Current.OutgoingMessageHeaders.Add(sessionTypeHeader);

        string result = client.GetData(1);
        Console.WriteLine(result);
    }
    Console.ReadKey();
}

The Service Trace viewer shows following

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
        <UsernameToken xmlns="http://test.com/webservices">username</UsernameToken>
        <PasswordText xmlns="http://test.com/webservices">password</PasswordText>
        <SessionType xmlns="http://test.com/webservices">None</SessionType>
        <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:13332/Service1.svc</To>
        <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/GetData</Action>
    </s:Header>
</s:Envelope>

Take a look OperationContextScope for more info

GSerjo
  • 4,725
  • 1
  • 36
  • 55
  • 1
    Be careful using `using` on your `ServiceClient` proxy. `Dispose` [can throw exceptions.](https://learn.microsoft.com/en-us/dotnet/framework/wcf/samples/use-close-abort-release-wcf-client-resources) – xr280xr Feb 25 '20 at 04:44
  • How about a 2nd namespace like xmlns:web="http://example.com/blah" then expects a header like this: Bar How to add the above header? – unruledboy Jun 24 '20 at 09:41