我正在尝试为我的消费者实现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)
任何帮助,将不胜感激。