I have a SingleTon
Service that receives an HttpContext
. The service receives the HttpContext as a parameters and whenever I try to access the HttpContext
I am getting an error: ObjectDisposedException
.
When I make use of a Dependency Injection (second approach) in order to get the HttpContext
, then the HttpContext is null.
On both occasions the method is accessed from a Controller action method.
Stack Trace HttpConext
passed as a parameter:
Exception has occurred: CLR/System.ObjectDisposedException Exception thrown: 'System.ObjectDisposedException' in Microsoft.AspNetCore.Http.Features.dll: 'IFeatureCollection has been disposed.' at Microsoft.AspNetCore.Http.Features.FeatureReferences
1.ThrowContextDisposed() at Microsoft.AspNetCore.Http.Features.FeatureReferences
1.ContextDisposed() at Microsoft.AspNetCore.Http.Features.FeatureReferences1.Fetch[TFeature,TState](TFeature& cached, TState state, Func
2 factory) at Microsoft.AspNetCore.Http.Features.FeatureReferences1.Fetch[TFeature](TFeature& cached, Func
2 factory) at Microsoft.AspNetCore.Http.DefaultConnectionInfo.get_RemoteIpAddress() at myapp.Areas.Auth.Service.AuthorizeService.d__5.MoveNext() in
Startup SingleTon added
// snippet
services.AddHttpContextAccessor();
services.AddSingleton<IAuthorizeService, AuthorizeService>();
The Service Implementation
// The Service Implementation
public interface IAuthorizeService
{
public Task AuthorizeUserAsync(UserModel user, HttpContext context);
}
public async Task AuthorizeUserAsync(UserModel user, HttpContext context)
{
await some Database operation, retrieving user roles
// do something with the context
// e.g.
context.Response.Cookies.Append("access_token", token, GetCookieOptions());
}
Updated: Controller using the Service
private readonly IAuthorizeService authorizeService;
public LoginController(IAuthorizeService authorizeService)
{
this.authorizeService = authorizeService;
}
public async Task<IActionResult> LogIn(LogInModel model, string actionType)
{
// snip (model and userService not shown)
var user = await userService.Authenticate(model.Email, model.Password, model.OTP);
await authorizeService.AuthorizeUserAsync(user, HttpContext);
return RedirectToAction("Index", "Home", new { area = "" });
}
Another approach: using Dependency Injection (DI), by injecting IHttpContextAccessor
also does not work:
In the default constructor I have this in the AuthorizeService
:
private readonly IHttpContextAccessor contextAccessor;
public AuthorizeService(IHttpContextAccessor contextAccessor, {
this.contextAccessor = contextAccessor;
}
In the Startup.cs
I add the IHttpContextAccessor
services.AddHttpContextAccessor();
When I inspect the values of contextAccessor
:
default constructor = HttpContext is not null
AuthorizeUserAsync method = HttpContext is null
I am expecting to have a non null value for HttpContext.
public async Task AuthorizeUserAsync(UserModel user)
{
if (contextAccessor.HttpContext == null) {
throw new Exception("ContextAccessor.HttpContext");
}
// await some Database operation, retrieving user roles
contextAccessor.HttpContext.Response.Cookies.Append("access_token", token, GetCookieOptions());
}