首页 > 解决方案 > MassTransit 消费者 IContainer 有效负载未由 StructureMapConsumerScopeProvider 设置

问题描述

我正在尝试为我的消费者实现 MassTransit 中间件,我需要从 IoC 容器中获取一个对象的实例(使用 StructureMap),但该容器似乎没有在有效负载上设置。

public class MyMessageFilter : IFilter<ConsumeContext<T>> where T : class {
  public void Probe(ProbeContext context) { /* Do nothing */ }
  public async Task Send(ConsumeContext<T> context, IPipe<ConsumeContext<T>> next)
  {
    _logger.Information("IContainer: " + context.TryGetPayload<IContainer>(out var container)); // logs "IContainer: False" where I was expecting it to have been added to the payload
    ...
    await next.Send(context);
  }
}

我已经配置了以下内容,我认为这可以解决问题。我已经确认正在创建和使用这些类的实例。

For(typeof(IConsumerFactory<>)).Use(typeof(ScopeConsumerFactory<>));
For<IConsumerScopeProvider>().Use<StructureMapConsumerScopeProvider>();

我的 MassTransit 配置如下所示

cfg.UseTransaction();
cfg.ReceiveEndpoint("some-queue", e => {                        
  e.UseMyMessageFilter();
  e.Consumer<MyConsumer>(container);
});

更新将上面的代码从 e.Consumer(container.GetInstance>) 更改为 e.Consumer(container)

对此的任何帮助将不胜感激。

标签: structuremapmasstransit

解决方案


推荐阅读