How to add soap header to WCF service call. Part 2

If you want to add to every wcf call a soap header and don't want to wrap them - you can easily implement method ApplyClientBehavior of an interface IOperationBehavior, and in this method attach your own message wrapper:

clientOperation.Formatter = new MessageWrapper(clientOperation.Formatter);

MessageWrapper should implement an interface IClientMessageFormatter.
To add your header you have to add next code to SerializeRequest method:

public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
{
    Message res = curFormatter.SerializeRequest(messageVersion, parameters);
    if (SPContext.Current == null)
    {
        res.Headers.Add(MessageHeader.CreateHeader("MyTestHeader", "Test Value"));
    }
}


Your Operation Behavior can be attached on initializing stage of your service proxy:
foreach (OperationDescription operation in serviceProxy.Endpoint.Contract.Operations)
{
    operation.Behaviors.Add(new CustomOperationBehavior());
}

Comments

Popular posts from this blog

Client-side scripting for Taxonomy/Term Set Fields

SharePoint delegate control is rendered twice

Some thoughts on using Sitecore Habitat for Production codebase