1

I need to access the HTTP response headers that are to be returned to the client from a WCF Service. Accessing the HTTPContext is easy(through HttpContext.Current.Response), but what is the event/extension/behavior that is executed lastly, when the StatusCode is already set (for ex. if the status is 500)?

EDIT: Message Inspectors don't seem to be a good solution here, because at the time they run, the status code isn't set yet. (At least in my trial that was the case)

jaraics
  • 4,239
  • 3
  • 30
  • 35

2 Answers2

1

You can access all headers on WebOperationContext.Current.IncomingRequest, like this:

IncomingWebRequestContext request = WebOperationContext.Current.IncomingRequest;
WebHeaderCollection headers = request.Headers;

Console.WriteLine("-------------------------------------------------------");    
foreach (string headerName in headers.AllKeys)
{
  Console.WriteLine(headerName + ": " + headers[headerName]);
}
Console.WriteLine("-------------------------------------------------------");

See here

Community
  • 1
  • 1
JwJosefy
  • 730
  • 7
  • 12
0
  1. Simplest way for having control on the Headers is to use Message contracts.

  2. Use Message Inspectors to monitor the message right after receiving it at the Service end.

  3. In an extreme case, where you are not satisfied with any other standard routes, you can go for POX (Plain Old XML) type operations where you would be dealing with raw XML message.

SaravananArumugam
  • 3,680
  • 6
  • 33
  • 45
  • Thanks for the Message Inspectors tip, my problem is that the HTTP status code might not be set when the message inspector is called. (I think it's never set, as it's set after the message inspectors run) – jaraics Jul 25 '11 at 19:21