HttpContext and OperationContext in a DataService with HTTP headers

I have a WCF DataService (v5.2) that overrides OnStartProcessingRequest(ProcessRequestArgs args) . I want to add some headers to the answer (in this method, which I assume is the right place?). I tried this first:

 args.OperationContext.ResponseHeaders.Add(...) 

This did not work. Then I tried this:

 OperationContext.Current.OutgoingMessageHeaders.Add(...) 

This did not work. I tried to add a new OperationContextScope on this suction cup. He still failed. Finally I tried this:

 HttpContext.Current.Response.AddHeader(...); 

This option worked! (By β€œwork”, I mean that he really appeared in the response to the client.) Why didn't the first two options work?

After reading on the Internet, I found that

 WebOperationContext.Current.OutgoingResponse.Headers.Add(...) 

also works. Why do we have four current contexts inside this method? How can a person know which one to use (at runtime)? Which of them are valid in my [WebGet] methods? Which of them are valid in my [QueryInterceptor] methods? In what context are the correct request headers guaranteed? (I used args.OperationContext for this currently.)

+6
source share
1 answer

I do not know about ProcessRequestArgs.OperationContext.ResponseHeaders, but I think I can explain why OperationContext.Current.OutgoingMessageHeaders does not work: in the headers there are SOAP headers (supposedly ignored for services other than SOAP), and not HTTP headers. In two other cases (HttpContext.Current.Response.AddHeader and WebOperationContext.Current.OutgoingResponse.Headers) mark "Http" and "Web" in the names to indicate that you are doing something HTTP-specific, that is, add HTTP headers.

By the way:

+8
source

All Articles