WCF自定义HTTP标头已添加到服务器响应中,但未从服务器返回

时间:2018-07-30 13:12:05

标签: http wcf http-headers filtering custom-headers

我希望所有WCF服务调用都返回CallDuration自定义HTTP标头。

在服务器上,有一个IDispatchMessageInspector实现和此BeforeSendReply实现:

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

这应该将CallDuration作为自定义HTTP标头添加到所有WCF响应。但是事实并非如此。

哪些可能阻止自定义HTTP标头到达客户端的过滤器?其他HTTP标头保持不变。

1 个答案:

答案 0 :(得分:1)

而不是使用WebOperationContext,而是将标头添加到答复中:

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());
}

可以使用SoapUI验证标头的添加

CallDuration