Sharing the same DI container between NSB and .net core application

We have a .net core application that hosts a NSB endpoint. We are using Autofac container to share dependencies between both .net core controllers and NSB pipeline. When registering services to their implementation, there are some services that are configured using Autofac’s InstancePerRequest scope. I want the resolve the types registered with InstancePerRequest scope (scoped to HTTPContext object that is part of .net core request pipeline) inside a custom NSB Behavior (a class inheriting from Behavior<IOutgoingLogicalMessageContext>). My current implementation always return a new instance of the service (transient scope) when I try to access the instance using the context argument of Invoke method of the behavior.

context.Builder.TryBuild<IMyService>()

Do you know how I can resolve this issue?

Thank you!

Hi Syaragat,

I’m not sure I fully understand what you are trying to achieve. There is a difference between a request scope that is defined by the WebApi or MVC pipeline and the request scope that is defined by the message handling pipeline. These two are two distinct concepts. A request scope from a controller perspective gets spawned whenever a new webapi or MVC request comes in. A request scope for NServiceBus gets spawned whenever a message comes in.

What type of behaviors are you writing? Are these behaviors for messages that go out? Or for messages that come in (then the above description applies).

Could you elaborate a bit more your scenario so that we can have a closer look at it and potentially even share a repro (that would definitely speed up things). You can also share a repro to support at particular dot net if you can’t paste it here for some reasons.

Regards
Daniel

1 Like

Hi Syaragat,

Was the above information helpful? Have you been able to fix it?

Regards
Daniel

Hi Daniel, I was able to resolve the issue. It is totally unrelated to the question I posted. It would be nice if we can get rid of my question.

Thank you for your help.

~Sid.

1 Like