aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

System.NullReferenceException at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Cookies()

Open czd890 opened this issue 3 years ago • 4 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

Sometime I got the error System.NullReferenceException at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Cookies() for several GET API.

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

{
    "exception": {
        "Type": "System.NullReferenceException",
        "TargetSite": "Microsoft.AspNetCore.Http.IRequestCookieCollection get_Cookies()",
        "Message": "Object reference not set to an instance of an object.",
        "Data": {},
        "Source": "Microsoft.AspNetCore.Http",
        "HResult": -2147467261,
        "StackTrace": "   at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Cookies()"
    }
}

.NET Version

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base

Anything else?

StackTrace:

at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Cookies()
at MyCode.DoWorkAsync
at MyHealthCheckCode.CheckHealthAsync
at Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService.RunCheckAsync(HealthCheckRegistration registration, CancellationToken cancellationToken)
services.AddScoped<MyCode>();
services.AddHealthChecks().AddCheck<MyHealthCheckCode>()

MyHealthCheckCode:IHealthCheck
{
    ctor(MyCode mycode){}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) => await mycode.DoWorkAsync();
}

MyCode(IHttpContextAccessor accessor)
{
     Task DoWorkAsync(){
         accessor.HttpContext..Request.Cookies.....
         //  other codes
    }
}

czd890 avatar Aug 09 '22 09:08 czd890

See https://github.com/dotnet/aspnetcore/issues/42040 for why this happens. If you could provide a repro or a better stack trace I might be able to help you identify how you can fix your code to avoid this.

davidfowl avatar Aug 09 '22 15:08 davidfowl

@davidfowl "HttpContext is accessed concurrently" makes sense. Health checks are run concurrently.

https://github.com/dotnet/aspnetcore/blob/50aa2fd929750ff4e46df0150d96fbbb7788e5d0/src/HealthChecks/HealthChecks/src/DefaultHealthCheckService.cs#L54-L57

czd890 avatar Aug 10 '22 02:08 czd890

Perfect! What is your health check checking?

davidfowl avatar Aug 10 '22 04:08 davidfowl

Hi @czd890. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Aug 10 '22 22:08 ghost

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

ghost avatar Aug 15 '22 00:08 ghost