2

I am using a WCF client proxy to call a web service. I am adding logging to each request using IClientMessageInspector. I want to get info out of the underlying HTTP message, and found out I can get it by the following:

public class WCFLoggingInspector : IClientMessageInspector
{        
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        var httpRequest = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
        var method = httpRequest.Method;
        return null;
    }
}

While stepping through in the debugger, this works fine. However, running normally, I get an error from the Properties indexer: "A property with the name 'httpRequest' is not present"

Can anyone explain whats going on here, or an alternate way to get at the HttpRequest? (I'm also doing the same thing for AfterReceiveReply)

TheSean
  • 4,516
  • 7
  • 40
  • 50
  • So, WCF tracing and message logging is not enough? – John Saunders Dec 18 '14 at 18:41
  • Do you really need to pull out various bits of information from the `Message`? The only time I've use a message inspector, a simple `request.ToString()` for the log message was more than adequate. – Cᴏʀʏ Dec 18 '14 at 18:46
  • Look at this StackOverflow answer, see if it does what you expect http://stackoverflow.com/questions/1365295/wcf-content-length-http-header-on-outbound-message – TYY Dec 18 '14 at 19:01
  • @JohnSaunders Yes I tried tracing, but does fit what I need. I need to log a combination of my own data (about the request) and the network details(msg size, time, http method, etc.). This is to fit into an existing logging system. – TheSean Dec 18 '14 at 19:20

1 Answers1

1

It seems that Visual Studio creates this when the debugger is attached.

if (!properties.ContainsKey(HttpRequestMessageProperty.Name))
                properties.Add("httpRequest", new HttpRequestMessageProperty());

Source: Property httpRequest' not present

Community
  • 1
  • 1
gerleim
  • 1,389
  • 2
  • 16
  • 28