WCF 自定义 HTTP header 添加到服务器响应但未从服务器返回

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

我希望所有 WCF 服务调用 return CallDuration 自定义 HTTP header。

在服务器上有一个带有此 BeforeSendReply 实现的 IDispatchMessageInspector 实现:

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

这应该将 CallDuration 作为自定义 HTTP header 添加到所有 WCF 响应。然而事实并非如此。

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

不使用 WebOperationContext,而是将 header 添加到回复中:

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 验证 header 的添加