I am implementing CurrentUser Service in ASP.NET Core-6 Web API User Authentication Login. I am using IdentityDbContext:
ICurrentUserService:
public interface ICurrentUserService
{
public string UserId { get; }
public string UserName { get; }
bool IsAuthenticated { get; }
public string IpAddress { get; }
}
CurrentUserService:
using Microsoft.AspNetCore.Http;
using System.Security.Claims;
public class CurrentUserService : ICurrentUserService
{
public string UserId { get; }
public string UserName { get; }
public bool IsAuthenticated { get; }
public string IpAddress { get; }
public CurrentUserService(IHttpContextAccessor httpContextAccessor)
{
IpAddress = httpContextAccessor.HttpContext?.Connection?.RemoteIpAddress.ToString();
UserName = httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.Name);
UserId = httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
IsAuthenticated = UserId != null;
}
}
Then in the Program.cs I have this:
var builder = WebApplication.CreateBuilder(args);
// Register CurrentUserService
builder.Services.AddScoped<ICurrentUserService, CurrentUserService>();
var app = builder.Build();
But while I tried to run the Application, I go this error:
System.AggregateException
HResult=0x80131500
Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Services.ICurrentUserService Lifetime: Scoped ImplementationType: Services.CurrentUserService': Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'Services.CurrentUserService'.)
Source=Microsoft.Extensions.DependencyInjection
StackTrace:
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)
at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
at Program.<Main>$(String[] args) in C:\MyApp\WebApi\Program.cs:line 15
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: Services.ICurrentUserService Lifetime: Scoped ImplementationType: Services.CurrentUserService': Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'Services.CurrentUserService'.
Inner Exception 2:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'Services.CurrentUserService'.
var app = builder.Build()
is line 15
How do I get this resolved?
Thank you
CodePudding user response:
Register HttpContextAccessor
dependency.
builder.Services.AddHttpContextAccessor();
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpContextAccessor();
// Register CurrentUserService
builder.Services.AddScoped<ICurrentUserService, CurrentUserService>();
var app = builder.Build();
References