tags:

views:

543

answers:

1

I'm trying to add a custom header to a wcf client hitting a standard, RESTful endpoint. I am trying to add some kind of header that will just allow me to trace requests from one layer to the next. Here's how I've tried to implement it:

public class DynatracePurePathHeaderAppender : IClientMessageInspector, IEndpointBehavior
 {
  object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel)
  {
   var dynaHeader = MessageHeader.CreateHeader("Action", "ns.yellowbook.jeff", "dynatrace",false);
   request.Headers.Add(dynaHeader);
   return null;
  }

  void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState)
  {
   return;
  }

  public void Validate(ServiceEndpoint endpoint){}

  public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters){}

  public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher){}

  public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  {
   clientRuntime.MessageInspectors.Add(this);
  }
 }

public class DynatracePurePathHeaderAppenderElement : BehaviorExtensionElement
 {
  protected override object CreateBehavior()
  {
   return new DynatracePurePathHeaderAppender();
  }

  public override Type BehaviorType
  {
   get { return typeof(DynatracePurePathHeaderAppender); }
  }
 }

I then configure the client successfully, but when this runs, I get the following exception: System.InvalidOperationException: Envelope Version 'EnvelopeNone (http://schemas.microsoft.com/ws/2005/05/envelope/none)' does not support adding Message Headers.

Anyone have any suggestions on how to insert this little barium meal?

+1  A: 

I assume you mean HTTP header and not a SOAP header? If so, MessageHeader has nothing to do with this.

Try something like this:

HttpRequestMessageProperty hrmp = new HttpRequestMessageProperty();
//Set hrmp.Headers, then:
request.Properties.Add(hrmp, HttpRequestMessageProperty.Name);

In general, the WCF REST support is not really optimized on the client side (it was created mostly to allow people to create REST services). For much better client-side REST support, check out HttpClient in the WCF REST Starter Kit.

Eugene Osovetsky