Closed navnitmehta closed 2 years ago
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.
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.
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.
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.
@navnitmehta Hello! Please publish new version of NuGet package to be able to use this new parameter. Thank you.
@navnitmehta Sorry, I mean @azzlack :)
Hi there, also awaiting a new Nuget package with a fix for this, as I'm running into the same issue. Thanks
Any update on the above issue, HttpContext is still null with the latest version v 2.06. Thanks for the great nugget package.
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);
}
}
}
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:
Any ideas?