2

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.

2
  • Have you tried accessing the call .Headers without ?. calls in previous steps and using only . there? The ?. operation stops execution if left hand side is null from some reason and you won't get any error as a result. Commented Aug 3, 2018 at 16:26
  • Hello yes, I have verified in the debugger that no subexpression is null - the elvis operator is just in there as an extra, perhaps superfluous, safety. Commented Aug 3, 2018 at 16:55

1 Answer 1

1
+250

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

Sign up to request clarification or add additional context in comments.

2 Comments

If this works for me, you have my deep appreciation and humble bounty - will check ASAP!
It worked like a charm. Bounty well earned. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.