Search code examples
wcfidispatchmessageinspector

WCF AfterReceiveRequest get headers


I just got started intercepting requests to my WCF service.

I'm calling the web service with java code that looks like this ( short version )

connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Username", "Testname");

I'm receiving the request but I cant get/find the headers in the message request. I've tried something like this:

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
    int headerIndex = request.Headers.FindHeader("Username", string.Empty);
    var username = request.Headers["Username"]

    return null;
}

But I always end up with -1 or exceptions. What is the right way to do this? Am I doing it wrong on the Java side as well?


Solution

  • The Headers property in the Message class will give you the SOAP headers; what you're looking for are the HTTP headers. To get to those, you should use the HttpRequestMessageProperty:

        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            var prop = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
            var userName = prop.Headers["Username"];
    
            return null;
        }