Is there a way to pass the child container to the NserviceBus pipeline?

Problem:
A request sent to the WCF service, the caller IBus.SendLocal, creates two instances of the object configured asInstancePerLifetimeScope()

Background:
I use Autofac.Integration.WCF(which makes everything resolved "per request", providing a new child scope for each WCF request)

IBus.SendLocal invokes a message mutator that receives the same resource type as the wcf service

However, two different instances are created. One is entered into the service, the other instance is entered into the message mutator.

I assume this is because the NSB creates its own child region based on the root container.

Any ideas / pointers on how to solve this problem (i.e. create only one instance created for each WCF request)?

Edit: using NSB 4.3.2 and Autofac 3.5.2

Simplified configuration code

  Autofac.IContainer container = ConfigureIoc();

  Configure
    .With(AllAssemblies.Matching("this.dll").And("that.dll"))
    .DefineEndpointName("endpoint name here")
    .AutofacBuilder(container)
    .MsmqSubscriptionStorage()
    .UnicastBus()
    .CreateBus()
    .Start();

  ServiceHost host = CreateHost();
  host.AddDependencyInjectionBehavior(typeof(ISomeContract), container);
  host.Open();
+4
source share
2 answers

Changed the NServiceBus.Autofac object builder to allow the permission of the child using factory.

ILifetimeScopefactory returns AutofacInstanceContext.Current.OperationLifetime, which is the child container used by WCF.

https://github.com/Particular/NServiceBus.Autofac/pull/2

0
source

A child container is created when a message is received by NServiceBus. In your scenario, the call / message does not receive NServiceBus, but WCF. All calls you make will use the root container that is created when NServiceBus is initialized.

Registration through Autofac

builder.RegisterType<MyMutator>().AsImplementedInterfaces().AsSelf().InstancePerLifetimeScope();

Registration through NServiceBus

busConfiguration.RegisterComponents(x => x.ConfigureComponent<MyMutator>(DependencyLifecycle.InstancePerUnitOfWork));

(v5) Bus.SendLocal, NService.

, WCF . NServiceBus, / ( ).

WCF NServiceBus, :

https://github.com/ramonsmits/wcfnsbtest

+2

All Articles