Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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# autofac中的寄存器处理程序_C#_Asp.net Core_Autofac_Asp.net Core 2.0_Mediatr - Fatal编程技术网

C# autofac中的寄存器处理程序

C# autofac中的寄存器处理程序,c#,asp.net-core,autofac,asp.net-core-2.0,mediatr,C#,Asp.net Core,Autofac,Asp.net Core 2.0,Mediatr,Autofac和MediatR正在使用中。 我试图在ContainerBuilder中注册处理程序,但总是出错 无法为服务类型“MediatR.IRequestHandler'2[DataSourceQuery'2[tenty,TModel],Kendo.Mvc.UI.DataSourceResult]”实例化实现类型“DataSourceHandler'2[tenty,TModel]” 我的问题是: public class DataSourceQuery<TEntity, TModel

Autofac和MediatR正在使用中。 我试图在ContainerBuilder中注册处理程序,但总是出错

无法为服务类型“MediatR.IRequestHandler'2[DataSourceQuery'2[tenty,TModel],Kendo.Mvc.UI.DataSourceResult]”实例化实现类型“DataSourceHandler'2[tenty,TModel]”

我的问题是:

public class DataSourceQuery<TEntity, TModel> : IRequest<DataSourceResult>
        where TEntity : class, IEntityBase
        where TModel : class
    {
        public DataSourceRequest Request { get; set; }
    }
公共类数据源查询:IRequest
其中tenty:class,IEntityBase
其中TModel:class
{
公共数据源请求{get;set;}
}
处理程序:

public class DataSourceHandler<TEntity, TModel> : IRequestHandler<DataSourceQuery<TEntity, TModel>, DataSourceResult>
        where TEntity : class, IEntityBase
        where TModel : class
    {
        private readonly ApplicationDbContext context;

        public DataSourceHandler(ApplicationDbContext context)
        {
            this.context = context;
        }

        public DataSourceResult Handle(DataSourceQuery<TEntity, TModel> message)
        {
            return context.Set<TEntity>()
                .ProjectTo<TModel>()
                .ToDataSourceResult(message.Request);
        }
    }
public类DataSourceHandler:IRequestHandler
其中tenty:class,IEntityBase
其中TModel:class
{
私有只读ApplicationDbContext上下文;
公共数据源处理程序(ApplicationDbContext上下文)
{
this.context=上下文;
}
公共数据源结果句柄(数据源查询消息)
{
返回context.Set()
.ProjectTo()
.ToDataSourceResult(message.Request);
}
}
我试着用不同的方式注册它,比如:

builder.RegisterGeneric(typeof(DataSourceHandler<,>)).As(typeof(IRequestHandler<,>))
                .AsImplementedInterfaces();

        builder.RegisterGeneric(typeof(DataSourceHandler<,>)).As(typeof(IRequestHandler<,>))
            .InstancePerDependency();

        builder.RegisterAssemblyTypes(typeof(DataSourceHandler<,>).GetTypeInfo().Assembly)
            .AsClosedTypesOf(typeof(DataSourceHandler<,>)).AsImplementedInterfaces();

        builder.RegisterGeneric(typeof(DataSourceHandler<,>))
            .As(typeof(IRequestHandler<,>));

        builder.RegisterAssemblyTypes(typeof(DataSourceHandler<,>).GetTypeInfo().Assembly)
            .AsClosedTypesOf(typeof(IRequestHandler<,>));
builder.RegisterGeneric(typeof(DataSourceHandler)).As(typeof(IRequestHandler))
.a实现接口();
builder.RegisterGeneric(typeof(DataSourceHandler)).As(typeof(IRequestHandler))
.InstancePerDependence();
RegisterAssemblyTypes(typeof(DataSourceHandler).GetTypeInfo().Assembly)
.AsClosedTypesOf(typeof(DataSourceHandler)).AsImplementedInterfaces();
builder.RegisterGeneric(typeof(DataSourceHandler))
.As(类型为(IRequestHandler));
RegisterAssemblyTypes(typeof(DataSourceHandler).GetTypeInfo().Assembly)
.AsClosedTypesOf(typeof(IRequestHandler));
但它不起作用


我该怎么办?

您发布的错误消息似乎表明
DataSourceHandler
类型已正确注册为
IRequestHandler
,因为该消息包含类型的名称


不过,看起来它无法实例化。由于此类型的唯一依赖项是
ApplicationDbContext
,您能否再次检查您在容器生成器中注册了该类型?

请参阅有关如何向autofac注册mediatR的示例:它也不起作用。我在Startup.cs:services.AddMediatR()中注册;