如何在 ASP.NET Core 的多项目解决方案中使用 Identity UserManager

问题描述 投票:0回答:1

我是 ASP.NET Core 的新手。
我在

IdentityUserService
中有一个名为
Infrastructure layer
的服务类,该类实现了
IUserService
中的
Application layer
接口。

当我调试解决方案时,我收到下面详细说明的错误。我如何在

UserManager
中使用
Infrastructure layer


IUserService接口

namespace JobSearch.Application.Contracts.Infrastructure.Services;

public interface IUserService
{
    public ICollection<AppUser> GetAllUsers();
}

身份用户服务

namespace JobSearch.Infrastructure.Services.User;

public class IdentityUserService : IUserService
{
    private readonly UserManager<AppUser> _userManager;

    public IdentityUserService(UserManager<AppUser> userManager)
        => _userManager = userManager;

    public ICollection<AppUser> GetAllUsers()
    {
        return _userManager.Users.ToList();
    }
}

基础设施服务注册扩展类

namespace JobSearch.Infrastructure;

public static class InfrastructureServiceRegistration
{
    public static IServiceCollection ConfigureInfrastructureServices(this IServiceCollection services)
    {
        services.AddSingleton<IUserService, IdentityUserService>();

        return services;
    }
}

我在

program.cs

中调用了这个方法

用户控制器

namespace JobSearch.WebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        private readonly IUserService _userService;

        public UsersController(IUserService userService)
            => _userService = userService;

        [HttpGet("/[action]", Name = "UsersGetAll")]
        public IActionResult GetAll()
        {
            return Ok(_userService.GetAllUsers());
        }
    }
}

异常详情

"C:\Program Files\JetBrains\JetBrains Rider 2023.1.3\plugins\dpa\DotFiles\JetBrains.DPA.Runner.exe" --handle=16508 --backend-pid=19472 --etw-collect-flags=67108622 --detach-event-name=dpa.detach.16508 C:/Users/ayazz/Projects/JobSearch/src/Presentation/JobSearch.WebAPI/bin/Debug/net8.0/JobSearch.WebAPI.exe
Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: JobSearch.Application.Contracts.Infrastructure.Services.IUserService Lifetime: Singleton ImplementationType: JobSearch.Infrastructure.Services.User.IdentityUserService': Cannot consume scoped service 'Microsoft.AspNetCore.Identity.UserManager`1[JobSearch.Domain.Entities.Identity.AppUser]' from singleton 'JobSearch.Application.Contracts.Infrastructure.Services.IUserService'.)
 ---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: JobSearch.Application.Contracts.Infrastructure.Services.IUserService Lifetime: Singleton ImplementationType: JobSearch.Infrastructure.Services.User.IdentityUserService': Cannot consume scoped service 'Microsoft.AspNetCore.Identity.UserManager`1[JobSearch.Domain.Entities.Identity.AppUser]' from singleton 'JobSearch.Application.Contracts.Infrastructure.Services.IUserService'.
 ---> System.InvalidOperationException: Cannot consume scoped service 'Microsoft.AspNetCore.Identity.UserManager`1[JobSearch.Domain.Entities.Identity.AppUser]' from singleton 'JobSearch.Application.Contracts.Infrastructure.Services.IUserService'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitScopeCache(ServiceCallSite scopedCallSite, CallSiteValidatorState state)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitConstructor(ConstructorCallSite constructorCallSite, CallSiteValidatorState state)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateCallSite(ServiceCallSite callSite)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)        
   at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
   at Program.<Main>$(String[] args) in C:\Users\ayazz\Projects\JobSearch\src\Presentation\JobSearch.WebAPI\Program.cs:line 44

Process finished with exit code -532,462,766.
c# asp.net-core dependency-injection asp.net-identity clean-architecture
1个回答
0
投票

问题解决了。
在这种情况下,在将 IServiceCollection 添加到 IoC 时使用

AddScoped
而不是
AddSingleton
可以修复错误。


导致错误的行

services.AddSingleton<IUserService, IdentityUserService>();

修复错误

services.AddScoped<IUserService, IdentityUserService>();
© www.soinside.com 2019 - 2024. All rights reserved.