7

I'm currently writing a GZIP compression for my selfhosted WCF REST application. I have a custom implementation of the .NET 'MessageEncoder' class and a custom implementation of the 'WebContentTypeMapper' class .

How can I retrieve the http headers in the 'ReadMessage' function and in the 'GetMessageFormatForContentType' function? I'd like to check the incoming request for the 'Content-Encoding' header before decompressing the input.

Thank You.

2
  • Doesn't IIS already provide GZIP compression out of the box ? Commented Sep 4, 2012 at 11:24
  • @Alex: My Application needs to run IIS independent in a self hosted scenario Commented Sep 10, 2012 at 13:35

3 Answers 3

3

This is what you can do

if (WebOperationContext.Current.IncomingRequest.Headers["Content-Encoding"] == WHAT YOU WANT)
{
            // Do what you like to do here
}

Hope this helps.

Thanks.

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

1 Comment

'WebOperationContext.Current' seems to be nothing. In the function 'System.ServiceModel.Channels.MessageEncoder.ReadMessage' in a self hosted scenario
1

You could try with WebOperationContext.Current or OperationContext.Current (depending of your binding). But unfortunately i think you cannot do this within the MessageEncoder implementation itself because it's too late in the process because by the time the MessageEncoder is asked to write the message contents the message frame, in this case the HTTP headers, has already been written. So, you would also need additional behavior, in the form of an IOperationBehavior, applied to your operations that sets the headers accordingly. In one of my personnal implementation, i have solved this by adding a GzipExtension in OperationContext with a custom message inspector. As Alex said, IIS already have a feature called dynamic compression that can compression any configured content type.

2 Comments

There is a misunderstanding. I'd like to READ the Http headers.
@Mimefilt Of course, but you have to add an inspector for reading http headers. Your encoder must not have the content type in the constructor (or something like this). There is a custom encoder in the WCF samples from MSDN.
1

I don't believe you will be able to get directly to the header from the CustomMessageEncoder. What you may be able to do is leverage the updated .NET 4.5 WCF BinaryMessageEncoderBindingElement. This now allows you to specify the compression type (such as Gzip) and automatically detects if the message body is compressed before attempting to decompress. See Whats's New in Windows Communication Foundation 4.5 for more details.

If you want to get to the header, one way you could try is to leverage HttpRequestMessageProperty in an implementation of IDispatchMessageInspector.

Simple example:

    public class MyDispatchMessageInspector : IDispatchMessageInspector
    {
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            object obj;
            if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out obj))
            {
                 var httpRequestMessageProperty = obj as HttpRequestMessageProperty;

                 if (httpRequestMessageProperty != null 
                         && !string.IsNullOrEmpty(httpRequestMessageProperty.Headers["content-encoding"]))
                 {
                    ...
                 }
            }
            return null;
     }
     ...
}

Another option is to access the OperationContext using the following:

int index = System.ServiceModel.OperationContext.Current.IncomingMessageHeaders.FindHeader("content-encoding", "");
string contentEncodeHeaderValue = System.ServiceModel.OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(index);

1 Comment

Unfortunatelly 'AfterReceiveRequest' is called after 'System.ServiceModel.Channels.MessageEncoder.ReadMessage'. 'System.ServiceModel.OperationContext.Current' is nothing at this point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.