繁体   English   中英

Mediatr无法解析ASP.Net Core中的UserManager

[英]Mediatr unable to resolve UserManager in ASP.Net Core

我正在构建ASP.Net Core App,这取决于使用MediatR执行命令的干净架构示例。 我想在我的应用程序中使用ASP.Net Core Identity,所以在我的CreateUserCommandHandler中我想使用UserManager添加新用户,但是当我将UserManager添加到Command承包商时,MediatR无法创建处理程序并因此异常而失败:

System.InvalidOperationException: Error constructing handler for request of type MediatR.IRequestHandler`2[GoGYM.Application.Identity.Commands.CreateUser.CreateUserCommand,MediatR.Unit]. Register your handlers with the container. See the samples in GitHub for examples. ---> System.InvalidOperationException: Unable to resolve service for type 'GoGYM.Persistence.GoGYMDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[GoGYM.Domain.Entities.ApplicationUser,GoGYM.Domain.Entities.ApplicationRole,GoGYM.Persistence.GoGYMDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim`1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole`1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin`1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken`1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim`1[System.String]]'.

在配置服务中,我注册我的DBContext和MediatR,如下所示:

// Add AutoMapper
        services.AddAutoMapper(new Assembly[] { typeof(AutoMapperProfile).GetTypeInfo().Assembly });
        // Add MediatR
        services.AddMediatR(typeof(GetUsersListQueryHandler).GetTypeInfo().Assembly);
        // Add DbContext using SQL Server Provider
        services.AddDbContext<IGoGYMDbContext, GoGYMDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("NorthwindDatabase")));

        services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddDefaultTokenProviders()
             .AddEntityFrameworkStores<GoGYMDbContext>();
        services.AddMvc();
....

这是我的命令处理程序代码:

public class CreateUserCommandHandler : IRequestHandler<CreateUserCommand, Unit>
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly IGoGYMDbContext _context;

    public CreateUserCommandHandler(IGoGYMDbContext context, UserManager<ApplicationUser> userManager)
    {
        _context = context;
        _userManager = userManager;
    }
    public Task<Unit> Handle(CreateUserCommand request, CancellationToken cancellationToken)
    {
        throw new NotImplementedException();
    }
}

而我的控制器

[HttpPost]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesDefaultResponseType]
    public async Task<IActionResult> Create(string values)
    {
        await Mediator.Send(new CreateUserCommand(values));

        return NoContent();
    }

我已经尝试了很多东西而且没有任何效果,只有当我从命令处理程序中删除UserManager时才会执行它。

您使用DI注册IGoGYMDbContext但将GoGYMDbContext传递给AddEntityFrameworkStores GoGYMDbContext未在DI中注册,因此在ASP.NET Core Identity框架请求时无法解析。

以下更改允许您注册接口和实现,但使用相同的实现实例,无论是通过接口还是实现请求:

  1. 从调用AddDbContext删除接口:

     services.AddDbContext<GoGYMDbContext>(...); 
  2. 从接口向GoGYMDbContext实现添加一个passthrough:

     services.AddScoped<IGoGYMDbContext>(sp => sp.GetRequiredService<GoGYMDbContext>()); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM