Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DI IHttpContextAccessor.HttpContext is null after upgrade to 2.2.0 #6080

Closed
petttro opened this issue Dec 21, 2018 · 3 comments
Closed

DI IHttpContextAccessor.HttpContext is null after upgrade to 2.2.0 #6080

petttro opened this issue Dec 21, 2018 · 3 comments
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions feature-http-abstractions

Comments

@petttro
Copy link

petttro commented Dec 21, 2018

DI IHttpContextAccessor.HttpContext is null after upgrade to 2.2.0

General

After upgrade aspnet core nuget packages from version 2.1.0 to 2.2.0 I've got issue IHttpContextAccessor.HttpContext unexpectedly became null during dependency injection.
I've investigated problem and found the cause of the problem was override HttpContext.TraceIdentifier in middleware. For 2.1.1 it was working well.

Details

Middleware:

public class DistinctTraceIdMiddleware
{
    private readonly RequestDelegate _next;
    public DistinctTraceIdMiddleware(RequestDelegate next)
    {
        _next = next;
    }
    public async Task Invoke(HttpContext context)
    {
        // !!! OVERRIDE cause of HttpContex = null !!!
        context.TraceIdentifier = Guid.NewGuid().ToString("N");
        await _next(context);
    }
}

Resolving dependency in Startup.cs

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    ConfigureDefaultServices(services);
    services.AddOptions();

    services.AddTransient<ConfigurationResponse>(serviceProvider =>
    {
        var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
        if (httpContextAccessor?.HttpContext == null)
        {
            // !!! it should not be null but it is
            throw new Exception("Why HttpContext is null?");
        }

        return new ConfigurationResponse();
    });

    return services.BuildServiceProvider();
}

To Reproduce

I've created sample app to reproduce this issue.
Start solution and make get http request on http:\\localhost:5000/api/configuration

Source code with sample app: https://github.com/petttro/core2.2_HttpContext
Also attached source code zip file here
Sample.zip

@Eilon
Copy link
Member

Eilon commented Dec 21, 2018

@davidfowl / @Tratcher - ideas on this?

@davidfowl
Copy link
Member

Dupe of #5144. It's a bug in 2.2 that's being fixed in a patch. See the other issue for details.

@alexsandro-xpt
Copy link

alexsandro-xpt commented Jul 20, 2019

Is this a bug at 2.2.301 yet?
I have an integration test with WebApplicationFactory and it works fine with dotnet 2.2.101. And anothe machine with dotnet 2.2.301 this test fail with System.NullReferenceException at IHttpContextAccessor.HttpContext.

So then to confirm that, I update this first machine with dotnet 2.2.301 and then, my test with IHttpContextAccessor.HttpContext become fail too.

More stranger is because I have a global.json with 2.2.101 SDK reference.
My output was:

# dotnet --version
2.2.101
# dotnet --list-sdks
2.1.302 [C:\Program Files\dotnet\sdk]
2.1.400 [C:\Program Files\dotnet\sdk]
2.1.403 [C:\Program Files\dotnet\sdk]
2.1.500 [C:\Program Files\dotnet\sdk]
2.1.502 [C:\Program Files\dotnet\sdk]
2.1.602 [C:\Program Files\dotnet\sdk]
2.1.700 [C:\Program Files\dotnet\sdk]
2.2.100 [C:\Program Files\dotnet\sdk]
2.2.101 [C:\Program Files\dotnet\sdk]
2.2.301 [C:\Program Files\dotnet\sdk]

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions feature-http-abstractions
Projects
None yet
Development

No branches or pull requests

5 participants