Microsoft.AspNet.WebApi.MessageHandlers.Compression icon indicating copy to clipboard operation
Microsoft.AspNet.WebApi.MessageHandlers.Compression copied to clipboard

HttpContext null exception

Open navnitmehta opened this issue 9 years ago • 9 comments

I just hooked up this library and I am getting "System.ArgumentNullException: Value cannot be null.Parameter name: httpContext" exception. I am using StructureMap as dependency injection which is using HttpContext.Current.

Here is the stack trace:

image

Any ideas?

navnitmehta avatar Nov 11 '16 00:11 navnitmehta

Oddly enough I just ran into the same issue. I believe it is due to the .ConfigureAwait(false) in several places on the compression handlers. Based on https://msdn.microsoft.com/en-us/magazine/jj991977.aspx they suggest to ConfigureAwait(false) when you can but not on methods that require context. I think HttpContext falls under this category. @azzlack what are your thoughts on removing the ConfigureAwait(false)? I removed it from a pull I have and it seems to have resolved the HttpContext null references I was receiving.

shaneholder avatar Nov 16 '16 19:11 shaneholder

I found exactly the same thing. I am not sure why ConfigureAwait would ever be false in a Web API environment we cant live without HttpContext.Current as much as we may want to.

navnitmehta avatar Nov 16 '16 19:11 navnitmehta

So, I found the following: http://www.wearesicc.com/getting-started-with-umbraco-7-and-structuremap-v3/

There are some suggestions for changing how HttpContext and CurrentNestedContainer are retrieved, they seem to work in my case. That being said I am still looking for an OWIN compliant solution. The code is just looking for a place to hang a per request retrievable nested container, there's got to be a better solution than going back to HttpContext.

shaneholder avatar Nov 16 '16 19:11 shaneholder

I created a pull request that allows passing in a bool that is used in the configureawait method. This solved the problem of httpcontext being null and allows the developer to decide if they need httpcontext or not. Also should work with the owin support.

CharlesNRice avatar Feb 07 '17 19:02 CharlesNRice

@navnitmehta Hello! Please publish new version of NuGet package to be able to use this new parameter. Thank you.

decease avatar May 26 '17 08:05 decease

@navnitmehta Sorry, I mean @azzlack :)

decease avatar May 26 '17 08:05 decease

Hi there, also awaiting a new Nuget package with a fix for this, as I'm running into the same issue. Thanks

64Soft avatar Jun 20 '17 10:06 64Soft

Any update on the above issue, HttpContext is still null with the latest version v 2.06. Thanks for the great nugget package.

richieferns avatar Oct 02 '17 10:10 richieferns

For those running into this issue and unwilling to build from source, i've devised a workaround

namespace Test
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new FixedServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));
            GlobalConfiguration.Configuration.MessageHandlers.Insert(1, new HelperHandler());
        }
    }

    class HelperHandler : DelegatingHandler
    {
        public Task<HttpResponseMessage> PublicSendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            return SendAsync(request, cancellationToken);
        }
    }

    class FixedServerCompressionHandler : ServerCompressionHandler
    {
        public FixedServerCompressionHandler(params ICompressor[] compressors) : base(compressors)
        {
        }

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            request = await HandleRequest(request, cancellationToken);

            var response = await ((HelperHandler)InnerHandler).PublicSendAsync(request, cancellationToken);

            return await HandleResponse(request, response, cancellationToken);
        }
    }
}

Suchiman avatar Mar 27 '19 13:03 Suchiman