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..
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.
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.