NServiceBus Ninject could not find the NinjectObjectBuilder scope when using ToMethod

2

I have a messaging context object defined to be unique per incoming message processing:

Bind<MessagingContext>().ToSelf().InUnitOfWorkScope();

Also I registered a custom interface to be resolved dynamically via Ninject ToMethod extension:

Bind<IBus>().ToMethod(c =>
            {
                var messagingContext = c.Kernel.Get<MessagingContext>();
                return new Bus(messagingContext);
            });

Currently while resolving MessagingContext object instance I'm getting an exception:

Ninject.Extensions.NamedScope.UnknownScopeException
  HResult=0x80131500
  Message=Error activating MessagingContext
The scope NinjectObjectBuilder is not known in the current context.
No matching scopes are available, and the type is declared InNamedScope(NinjectObjectBuilder).

Is there any way to correctly resolve the object registered as InUnitOfWorkScope inside the ToMethod?

I'm using:

  • NServiceBus 6.4.2
  • NServiceBus.Ninject 6.0.1
  • Ninject 3.2.2
c#
ninject
nservicebus
asked on Stack Overflow Mar 22, 2018 by alex.dev

1 Answer

0

Seems like this might be a bug with child containers and named scoping.

As a work around in the meantime, you can change your MessagingContext registration to be:

Bind<MessagingContext>().ToSelf().DefinesNamedScope("NinjectObjectBuilder");

Which will at least let Ninject know that the MessagingContext type is intended to be used within the child container.

answered on Stack Overflow Mar 27, 2018 by WilliamBZA

User contributions licensed under CC BY-SA 3.0