Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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
.net 如何向Masstransit中的消费者注入依赖项_.net_Dependency Injection_Asp.net Core_Masstransit - Fatal编程技术网

.net 如何向Masstransit中的消费者注入依赖项

.net 如何向Masstransit中的消费者注入依赖项,.net,dependency-injection,asp.net-core,masstransit,.net,Dependency Injection,Asp.net Core,Masstransit,我的消费者代码如下所示。发布事件时,会引发“未为此对象定义无参数构造函数”异常(请参阅下面的详细信息)。 我只使用Microsoft.Extensions.DependencyInjection容器。如何注入依赖项?是否有这方面的代码示例 public class UserAddedConsumer : IConsumer<IUserCreated> { IUserNotification notificationManager; ILogger<UserAdd

我的消费者代码如下所示。发布事件时,会引发“未为此对象定义无参数构造函数”异常(请参阅下面的详细信息)。 我只使用Microsoft.Extensions.DependencyInjection容器。如何注入依赖项?是否有这方面的代码示例

public class UserAddedConsumer : IConsumer<IUserCreated>
{
    IUserNotification notificationManager;
    ILogger<UserAddedConsumer> logger;
    public UserAddedConsumer(IUserNotification notificationManager, ILogger<UserAddedConsumer> logger)
    {
        this.notificationManager = notificationManager;
        this.logger = logger;
    }

    public Task Consume(ConsumeContext<IUserCreated> context)
    {
        logger.LogTrace("UserAddedConsumer - Userid: " + context.Message.Id);
        return notificationManager.UserCreated(context.Message.User);
    }
}
public类UserAddedConsumer:IConsumer
{
IUSER通知经理;
ILogger记录器;
public UserAddedConsumer(IUserNotification notificationManager、ILogger记录器)
{
this.notificationManager=notificationManager;
this.logger=记录器;
}
公共任务消费(消费上下文)
{
LogTrace(“UserAddedConsumer-Userid:+context.Message.Id”);
返回notificationManager.UserCreated(context.Message.User);
}
}
没有为此对象定义无参数构造函数,System.MissingMethodException:没有为此对象定义无参数构造函数。
在System.RuntimeTypeHandle.CreateInstance(RuntimeType类型、Boolean publicOnly、Boolean&canBeCached、RuntimeMethodHandleInternal&ctor)
位于System.RuntimeType.CreateInstanceSlow(布尔publicOnly、布尔skipCheckThis、布尔fillCache、StackScrawlMark和stackMark)
位于System.Activator.CreateInstance(类型,布尔非公共)
位于System.Activator.CreateInstance(类型)
在MassTransit.ConsumerConfigurators.UntypedConsumerConfigurator1c\uuuu DisplayClass2\u0.b\uu0()中
在MassTransit.Pipeline.ConsumerFactorys.DelegateConsumerFactory1.d_u21.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在MassTransit.Pipeline.Filters.ConsumerMessageFilter`2.d_u4.MoveNext()中

这是一个实施问题。我用下面的方法来注册消费者,解决了这个问题

conf.Consumer(consumer, serviceProvider.GetService);
serviceProvider is IServiceProvider

instead of 
conf.Consumer(consumer, Activator.CreateInstance);

至少显示您的端点配置代码。
conf.Consumer(consumer, serviceProvider.GetService);
serviceProvider is IServiceProvider

instead of 
conf.Consumer(consumer, Activator.CreateInstance);