C# INotificationHandler MediatR中的注入接口
我正在尝试在我的解决方案中实现一些中介化。 我的问题是,我不知道如何在一个INotificationHandler中注入接口“ISchemaRepository”。MediatR和下面的所有类都在我的ASP.NET核心项目中,ISchemaRepository位于不同的.NET核心类库中,“SchemaRepository”位于不同的ASP.NET核心项目中。基本上,我想要实现的是从一个数据库中获取数据,然后插入到另一个项目中的另一个数据库中 我已经读了很多关于MediatR的文章,但是没有找到任何关于如何实现这一点的例子。甚至可以将接口注入到“INotificationHandler”中吗? 我知道我可能陷入了困境,在编程方面我还是个新手 当我在以下状态下运行应用程序时,会出现异常: 激活λ:MediatR.INotificationHandler`1[[Facade.Application.Events.GetScheduleEvent,Facade,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]][]->Facade.Application.Events.ScheduleEventHandler->ServiceApi.Repository.SchemaRepository时引发异常 我在ApplicationModule和MediatorModule中尝试了两种不同的解决方案,因为我猜这与dependencyinjection有关。我最好的猜测是加上一句C# INotificationHandler MediatR中的注入接口,c#,.net,autofac,mediator,C#,.net,Autofac,Mediator,我正在尝试在我的解决方案中实现一些中介化。 我的问题是,我不知道如何在一个INotificationHandler中注入接口“ISchemaRepository”。MediatR和下面的所有类都在我的ASP.NET核心项目中,ISchemaRepository位于不同的.NET核心类库中,“SchemaRepository”位于不同的ASP.NET核心项目中。基本上,我想要实现的是从一个数据库中获取数据,然后插入到另一个项目中的另一个数据库中 我已经读了很多关于MediatR的文章,但是没有找到
builder.RegisterType<ScheduleEventHandler>()
.As<ISchemaRepository>()
.InstancePerLifetimeScope();
builder.RegisterType()
.As()
.InstancePerLifetimeScope();
到我的“ApplicationModule”,但在启动时会出现以下异常:
“System.ArgumentException
HResult=0x80070057
Message=类型“Facade.Application.Events.ScheduleEventHandler”不可分配给服务“service.InternalResourceRepository.ISchemaRepository”。
来源=自动传真
堆栈跟踪:
在C:\projects\Autofac\src\Autofac\Builder\RegistrationBuilder.CreateRegistration中的Autofac.Builder.RegistrationBuilder.CreateRegistration(Guid id、RegistrationData数据、IInstanceActivator激活器、服务[]服务、IComponentRegistration目标):第192行
"
这是我的INotificationHandler:
公共类ScheduleEventHandler:INotificationHandler
{
专用只读ISchemaRepository\u存储库;
公共ScheduleEventHandler(ISchemaRepository存储库)
{
_存储库=存储库;
}
公共任务句柄(GetScheduleEvent通知、CancellationToken CancellationToken)
{
_repository.SeedSchedule();
返回Task.CompletedTask;
}
}
“GetScheduleEvent”只是一个用“INotification”声明的空类
这是我的“调解模块”:
public class MediatorModule : Autofac.Module
{
/// <summary>
/// Implementing the Mediator
/// </summary>
/// <param name="builder"></param>
protected override void Load(ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly)
.AsImplementedInterfaces();
//Events
builder.RegisterAssemblyTypes(typeof(ScheduleEventHandler).GetTypeInfo().Assembly)
.AsClosedTypesOf(typeof(INotificationHandler<>));
builder.Register<ServiceFactory>(context =>
{
var componentContext = context.Resolve<IComponentContext>();
return t => { object o; return componentContext.TryResolve(t, out o) ? o : null; };
});
}
}
公共类中介模块:Autofac.Module
{
///
///实施调解人
///
///
受保护的覆盖无效负载(ContainerBuilder builder)
{
RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly)
.a实现接口();
//事件
RegisterAssemblyTypes(typeof(ScheduleEventHandler).GetTypeInfo().Assembly)
.AsClosedTypesOf(typeof(INotificationHandler));
builder.Register(上下文=>
{
var componentContext=context.Resolve();
return t=>{object o;return componentContext.TryResolve(t,out o)→o:null;};
});
}
}
如你所见,我正在使用AutoFac。这是我的应用程序模块:
public class ApplicationModule : Autofac.Module
{
public string QueriesConnectionString { get; }
public ApplicationModule(string qconstr)
{
QueriesConnectionString = qconstr;
}
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<SchemaRepository>()
.As<ISchemaRepository>()
.InstancePerLifetimeScope();
}
}
公共类应用程序模块:Autofac.Module
{
公共字符串QueriesConnectionString{get;}
公共应用程序模块(字符串qconstr)
{
QueriesConnectionString=qconstr;
}
受保护的覆盖无效负载(ContainerBuilder builder)
{
builder.RegisterType()
.As()
.InstancePerLifetimeScope();
}
}
如果这个问题能够得到解决,我将非常感谢所有能帮助我解决这个问题的人
谢谢大家! builder.RegisterType()
.As()
.InstancePerLifetimeScope();
这表示“当我解析ISchemaRepository
时,我希望您给我一个新的ScheduleEventHandler
,但是:
公共类ScheduleEventHandler:INotificationHandler
ScheduleEventHandler
没有实现ISchemaRepository
。这不是Autofac的事情,而是您没有实现接口
例如,这也会失败:
var s=new ScheduleEventHandler();
var r=(ischema)s;
您只能将类型注册为它们实现/派生的东西
如果要对原始异常进行故障排除,需要查看内部异常消息
An exception was thrown while activating λ:MediatR.INotificationHandler`1[[Facade.Application.Events.GetScheduleEvent, Facade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]][] -> Facade.Application.Events.ScheduleEventHandler -> ServiceApi.Repository.SchemaRepository.
这只是一个开始。继续阅读,您应该会看到该异常实际上是什么,并且可能会获得一些关于如何修复该异常的指导。也可以查看调试窗口,您可能会在其中获得一些日志。感谢您的回答。查看内部异常,我认为原因可能是我的“SchemaRepository”构造函数需要一个Db上下文作为参数,特定于我的ServiceApi,与我的FacadeApi(我配置的api)没有连接。因此,当我尝试将“SchemaRepository”作为“ISchemaRepository”注入时,它无法解析“SchemaRepository”,因为它对构造函数中的DbContext没有任何线索。这只是一个猜测,我将尝试更改一些内容并返回结果。
An exception was thrown while activating λ:MediatR.INotificationHandler`1[[Facade.Application.Events.GetScheduleEvent, Facade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]][] -> Facade.Application.Events.ScheduleEventHandler -> ServiceApi.Repository.SchemaRepository.