Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Mediatr无法解析ASP.Net核心中的UserManager_C#_Asp.net Core_Asp.net Identity_Mediatr_Usermanager - Fatal编程技术网

C# Mediatr无法解析ASP.Net核心中的UserManager

C# Mediatr无法解析ASP.Net核心中的UserManager,c#,asp.net-core,asp.net-identity,mediatr,usermanager,C#,Asp.net Core,Asp.net Identity,Mediatr,Usermanager,我正在构建ASP.Net核心应用程序,它依赖于这个干净的示例,该示例使用MediatR执行命令。 我想在我的应用程序中使用ASP.Net核心标识,所以在CreateUserCommandHandler中,我想使用UserManager添加新用户,但是当我将UserManager添加到命令承包商MediatR时,无法创建处理程序,并且失败,但出现以下异常: System.InvalidOperationException: Error constructing handler for reques

我正在构建ASP.Net核心应用程序,它依赖于这个干净的示例,该示例使用MediatR执行命令。 我想在我的应用程序中使用ASP.Net核心标识,所以在CreateUserCommandHandler中,我想使用UserManager添加新用户,但是当我将UserManager添加到命令承包商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]]'.
在Configure services中,我注册了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();
....
//添加自动映射器
AddAutoMapper(新程序集[]{typeof(AutoMapperProfile.GetTypeInfo().Assembly});
//添加MediatR
AddMediatR(typeof(GetUsersListQueryHandler.GetTypeInfo().Assembly);
//使用SQL Server提供程序添加DbContext
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“NorthwindDatabase”));
服务.额外性()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores();
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();
    }
}
公共类CreateUserCommandHandler:IRequestHandler
{
私有只读用户管理器_UserManager;
私有只读上下文\u上下文;
公共CreateUserCommandHandler(IGoGYMDbContext上下文,UserManager UserManager)
{
_上下文=上下文;
_userManager=userManager;
}
公共任务句柄(CreateUserCommand请求、CancellationToken CancellationToken)
{
抛出新的NotImplementedException();
}
}
还有我的控制器

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

        return NoContent();
    }
[HttpPost]
[产品响应类型(StatusCodes.Status204NoContent)]
[ProductsDefaultResponseType]
公共异步任务创建(字符串值)
{
wait Mediator.Send(新的CreateUserCommand(值));
返回NoContent();
}

我已经尝试了很多方法,但没有任何效果,除非我从命令处理程序中删除UserManager,然后它就会被执行。

您使用DI注册
IGoGYMDbContext
,但将
GoGYMDbContext
传递到
AddEntityFrameworkStores
GoGYMDbContext
未向DI注册,因此在ASP.NET核心身份框架请求时无法解析

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

services.AddScoped<IGoGYMDbContext>(sp => sp.GetRequiredService<GoGYMDbContext>());
  • 从对
    AddDbContext
    的调用中删除接口:

    services.AddDbContext<GoGYMDbContext>(...);
    

  • CreateUserCommandHandler
    GetUsersListQueryHandler
    是否在同一程序集中?是的,在同一程序集中,GetUsersListQueryHandler工作正常如果我删除UserManager,CreateUserCommandHandler也工作正常