httpwcfhttp-headersfilteringcustom-headers

WCF custom HTTP header added to server response but not returned from server


I want all WCF service calls to return a CallDuration custom HTTP header.

On the server there is a an IDispatchMessageInspector implementation with this BeforeSendReply implementation:

public void BeforeSendReply(ref Message reply, object correlationState)
{
  // ... calculate CallDuration etc. ...
  // send CallDuration
  WebOperationContext.Current?.OutgoingResponse?.Headers.Add("CallDuration", $"{duration.TotalSeconds}");
}

This ought to add CallDuration as a custom HTTP header to all WCF responses. However that is not the case.

What are the possible filters that could prevent the custom HTTP header from reaching the client? Other HTTP headers remain intact.


Solution

  • Instead of using the WebOperationContext, add the header to the reply instead:

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        //assumes "duration" is a variable initialized in AfterReceiveRequest, containing the time in ticks at that moment
        long callDuration = DateTime.Now.Ticks - duration;
        HttpResponseMessageProperty prop;
        if (reply.Properties.ContainsKey(HttpResponseMessageProperty.Name))
        {
            prop = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
        }
        else
        {
            prop = new HttpResponseMessageProperty();
            reply.Properties.Add(HttpResponseMessageProperty.Name, prop);
        }
    
        prop.Headers.Add("CallDuration", callDuration.ToString());
    }
    

    Addition of the header can be verified with SoapUI

    CallDuration