Asp.net core 如何在使用Aspnet Core Identiy实现CQRS时注册处理程序

Asp.net core 如何在使用Aspnet Core Identiy实现CQRS时注册处理程序,asp.net-core,asp.net-identity,mediatr,Asp.net Core,Asp.net Identity,Mediatr,我正在尝试使用CQR注册我的用户 我正在注册MediatR: services.AddMediatR(typeof(MyCommand).GetTypeInfo().Assembly); public class Handler : IRequestHandler<RegisterCommand, object> { private readonly MyDbContext _context; private rea

我正在尝试使用CQR注册我的用户

我正在注册MediatR:

services.AddMediatR(typeof(MyCommand).GetTypeInfo().Assembly);


public class Handler : IRequestHandler<RegisterCommand, object>
        {
            private readonly MyDbContext _context;
            private readonly IMediator _mediator;
            private readonly UserManager<User> _userManager;

            public Handler(IYawaMVPDbContext context, IMediator mediator, UserManager<User> userManager)
            {
                _context = context;
                _mediator = mediator;
                _userManager = userManager;
            }
}
services.AddMediatR(typeof(MyCommand).GetTypeInfo().Assembly);
公共类处理程序:IRequestHandler
{
私有只读MyDbContext\u context;
专用只读IMediator\u中介;
私有只读用户管理器_UserManager;
公共处理程序(IYawaMVPDbContext上下文、IMediator中介、UserManager UserManager)
{
_上下文=上下文;
_调解人=调解人;
_userManager=userManager;
}
}
我得到以下例外情况:

InvalidOperationException:无法解析类型的服务 “MyDbContext”正在尝试激活 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore
6[MyCore.Domain.Entities.User,MyCore.MyApp.Persistence.MyDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String]]'。 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(类型 serviceType,Type implementationType,CallSiteChain CallSiteChain, ParameterInfo[]参数,bool throwIfCallSiteNotFound)

InvalidOperationException:为的请求构造处理程序时出错 类型 MediatR.IRequestHandler`2[MyCore.MyApp.Application.Users.Commands.Register.RegisterCommand,System.Object]

向容器注册处理程序。有关示例,请参阅GitHub中的示例


感谢您的帮助。

添加服务以解析启动文件中的DbContext

services.AddDbContext<IYawaMVPDbContext, YourDbContextImplementation>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("yourConnectionString")));
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“yourConnectionString”));

添加服务以解析启动文件中的DbContext

services.AddDbContext<IYawaMVPDbContext, YourDbContextImplementation>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("yourConnectionString")));
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“yourConnectionString”));

我认为以下代码没有问题

services.AddMediatR(typeof(MyCommand).GetTypeInfo().Assembly);
处理所有MediatR IRequest和iRequestHandler

但是您创建了一个IYawaMVPDbContext接口及其实现类,它不能由该
MediatR.Extensions.Microsoft.DependencyInjection处理

DBContext在应用程序启动期间使用依赖项注入进行注册,以便可以自动将它们提供给使用服务的组件—手动注册如下


services.AddDbContext(options=>
options.UseSqlServer(Configuration.GetConnectionString(“ConnectionString”));
我认为下面的代码没有问题

services.AddMediatR(typeof(MyCommand).GetTypeInfo().Assembly);
处理所有MediatR IRequest和iRequestHandler

但是您创建了一个IYawaMVPDbContext接口及其实现类,它不能由该
MediatR.Extensions.Microsoft.DependencyInjection处理

DBContext在应用程序启动期间使用依赖项注入进行注册,以便可以自动将它们提供给使用服务的组件—手动注册如下


services.AddDbContext(options=>
options.UseSqlServer(Configuration.GetConnectionString(“ConnectionString”))你的答案已经发布了,在回答一个答案之前,如果你认为以前的答案可以被改进,编辑它,并为社区提供更好的体验,@ Zachdev说,答案是一样的。所以我会写同样的评论。我在启动文件<代码>服务。(options=>options.UseMySql(Configuration.GetConnectionString(“YawaMVPDatabase”))你的答案已经发布了,在回复之前,如果你认为以前的答案可以被改进,编辑它,并为社区提供更好的体验,@ Zachdev说,答案是一样的。所以我会写同样的评论。我在启动文件<代码>服务。(options=>options.UseMySql(Configuration.GetConnectionString(“YawaMVPDatabase”));
我实际上在启动文件中有这个。显然,我使用的是MySql
services.AddDbContext(options=>options.UseMySql(Configuration.GetConnectionString(“YawaMVPDatabase”))
我实际上在启动文件中有这个。显然我使用MySql
services.AddDbContext(options=>options.UseMySql(Configuration.GetConnectionString(“YawaMVPDatabase”));