I am trying to configure something verify similar to Tranaction,
trying to create middleware except I dont have access to the lifetimescope that is wrapped around the consumer.
public class UnitOfWorkFilter<T> : IFilter<T>
where T : class, PipeContext
{
public void Probe(ProbeContext context)
{
context.CreateFilterScope("unit-of-work");
}
public async Task Send(T context, IPipe<T> next)
{
var scope = context.GetPayload<ILifetimeScope>();
var uow = scope.Resolve<IUnitOfWork>();
try
{
await next.Send(context);
await uow.Complete();
}
catch
{
await uow.Abandon();
throw;
}
}
}
At this point looks like I would have to fork MassTransit.AutofacIntegration and alter the AutofacConsumerFactory
public async Task Send<TMessage>(ConsumeContext<TMessage> context, IPipe<ConsumerConsumeContext<TConsumer, TMessage>> next)
where TMessage : class
{
using (ILifetimeScope innerScope = context.GetOrAddPayload(() => _scope.BeginLifetimeScope(_name)))
{
var uow = innerScope.Resolve<IUnitOfWork>();
var consumer = innerScope.Resolve<TConsumer>();
if (consumer == null)
{
throw new ConsumerException($"Unable to resolve consumer type '{TypeMetadataCache<TConsumer>.ShortName}'.");
}
try
{
await next.Send(context.PushConsumer(consumer)).ConfigureAwait(false);
await uow.Complete().ConfigureAwait(false);
}
catch
{
await uow.Abandon().ConfigureAwait(false);
throw;
}
}
}