HttpContext property of HttpContextAccessor is null
See original GitHub issue
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:
- Created 5 years ago
- Comments:47 (19 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@davidfowl / @muratg We 100% need this fix back ported, please!
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