question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HttpContext property of HttpContextAccessor is null

See original GitHub issue

httpcontextaccessor_problem I have a REST API web application which worked perfectly with ASP.Net Core 2.1. Now the constructor injected IHttpContextAccessor returns a null value for HttpContext property.

In Startup.cs class, in ConfigureServices method I have:

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();          
services.AddScoped<WorklistPagedTasksHalRepresentationConverter>();

This is the WorklistPagedTasksHalRepresentationConverter class

public class WorklistPagedTasksHalRepresentationConverter : TypeConverter<WorklistWithPagedTasks, WorklistPagedTasksHalRepresentation>
    {
        private readonly IHttpContextAccessor httpContextAccessor;

        private readonly IMapper mapper;

        public WorklistPagedTasksHalRepresentationConverter(IHttpContextAccessor httpContextAccessor, IMapper mapper)
        {
            this.httpContextAccessor = httpContextAccessor;
            this.mapper = mapper;
        }

        public WorklistPagedTasksHalRepresentation Convert(WorklistWithPagedTasks source, WorklistPagedTasksHalRepresentation destination, ResolutionContext context)
        {
            var tasks = mapper.Map<IList<TaskSummaryHalRepresentation>>(source.Tasks.ResultPage.ToList());
            var worklistPagedTasksHalRepresentation = new WorklistPagedTasksHalRepresentation(
                new PaginatedResult<TaskSummaryHalRepresentation>
                {
                    ResultPage = tasks.AsQueryable(),
                    TotalCount = source.Tasks.TotalCount,
                    PageNumber = source.Tasks.PageNumber
                },
                httpContextAccessor.HttpContext.Request)
            {
                Id = source.Id,
                OwnerId = source.OwnerId,
                OwnerName = source.OwnerName
            };
            return worklistPagedTasksHalRepresentation;
        }
    }

The Convert method is called by a controller method.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:47 (19 by maintainers)

github_iconTop GitHub Comments

10reactions
kieronlanningcommented, Dec 17, 2018

@davidfowl / @muratg We 100% need this fix back ported, please!

10reactions
davidfowlcommented, Dec 17, 2018

Yes this is a change made in 2.2. It’s fixed in 3.0, maybe we should backport that’s change to 2.2.x.

cc @muratg

Read more comments on GitHub >

github_iconTop Results From Across the Web

Returning null on httpContextAccessor.HttpContext
HttpContext is only valid during a request. When .NET Core creates an ApplicationDbContext class for the call to Configure there is no valid ......
Read more >
Error when checking if HttpContextAccessor.HttpContext. ...
Hi there I'm using VS 2019 community, in my application that using .net core MVC I'm trying to check if user signed in...
Read more >
HttpContextAccessor.HttpContext is null in blocks and in @ ...
I've debugged the decompiled code and in ContentRenderingContext HttpContextAccessor exists but the HttpContext inside it is null.
Read more >
[Solved]-IHttpContextAccessor is null in class-C#
Coding example for the question IHttpContextAccessor is null in class-C#. ... get null UserId most of the time instead create httpContextAccessor field ......
Read more >
HttpContextAccessor in Asp.NET Core Web API
In ASP.NET Core Web API, the HttpContextAccessor class is a component that provides access to the current HTTP request and response context.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found