0

Overriding GetWebRequest and accessing the Headers works just fine. Now I need to set an attribute. My goal looks like

<soapenv:Envelope xmlns:urn="urn:company:sap:ds:sales" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
  <messageId xmlns="http://www.company.com/foo/bar/soap/features/messageId/">urn:uuid:123c155c-3ab4-19ca-a045-02003b1bb7f5</messageId>
</soapenv:Header>
<soapenv:Body>
    ...
</soapenv:Body>
</soapenv:Envelope>

The problem is how to generate the xmlns="http://www.company.com/foo/bar/soap/features/messageId" attribute in the Header. I just spent a few hours reading documentation, and it seems there simply is no way to set an attribute in the header.

Could I simply quote the quotes and write

request.Headers["messageID xmlns=""http://www.company.com/foo/bar/soap/features/messageId"""] = "urn:uuid:123c155c-3ab4-19ca-a045-02003b1bb7f5";

But this feels somehow wrong to me..

Batox
  • 574
  • 1
  • 4
  • 16
  • SOAP headers and HTTP headers are not the same thing. How are you creating the SOAP message? *Where* are you overriding GetWebRequest? I'm not aware of an extension point in WCF that would allow you to do this but I'm prepared to be corrected. How is WCF involved here? – Tom W Feb 06 '20 at 09:19
  • I have a WSDL file describing the target service, and an XML file as example. I imported the WSDL using Add->Web Service Reference. This created a SoapHttpClientProtocol-derived service in my project (implemented as a partial class). I'm overriding this services GetWebRequest. – Batox Feb 06 '20 at 14:13
  • `SoapHttpClientProtocol` is not WCF, this is from the older XML Web Services toolset, hence my confusion. From a cursory Google search, modifying headers seems to be quite difficult here whereas the accepted answer provides a nice method of doing this with an `IClientMessageInspector` - any reason you aren't using a WCF client and chose the ASMX web client instead? – Tom W Feb 07 '20 at 11:19

1 Answers1

0

please refer to the below code snippets, wish it is useful to you.

    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.ServiceClient client = new ServiceClient();
            using (new OperationContextScope(client.InnerChannel))
            {
                UserInfo userInfo = new UserInfo();
                userInfo.FirstName = "John";
                userInfo.LastName = "Doe";
                MessageHeader aMessageHeader = MessageHeader.CreateHeader("UserInfo", "http://tempuri.org", userInfo);
                MessageHeader bheader = MessageHeader.CreateHeader("messageId", "http://www.company.com/foo/bar/soap/features/messageId/", "urn:uuid:123c155c-3ab4-19ca-a045-02003b1bb7f5");
           OperationContext.Current.OutgoingMessageHeaders.Add(aMessageHeader);
                OperationContext.Current.OutgoingMessageHeaders.Add(bheader);
                var result = client.Test();
                Console.WriteLine(result);
            }
        }
    }
    public class UserInfo
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
}

Result.
enter image description here
This is an implementation on the client-side, you can also use IClientMessageInspector or IDispatchMessageInspector interface to add the custom header on the client-side and server-side.
Feel free to let me know if there is anything I can help with.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22