user981375
user981375

Reputation: 647

Web API, DI and object lifetime via nested containers?

OK, I wired up StructureMap to my Web Api application (plenty of samples how to do this here on StackOverflow and elsewhere on the Internet). My understanding of object lifetime maintenance in Web Api is that it's done via nested containers.

For the whole idea of object lifetime maintenance via nested containers to work, Raven's session object would have to be created in child container as a transient singleton. Right now I have IDocumentStore defined in in parent container and scoped as singleton. IDocumentSession is also defined in parent container but scoped as HttpContext. When request comes in, BeginScope() is called, nested container is created but IDocumentSession object in it is inherited from the parent instead of being created.

I don't understand how this is supposed to work. If object is not created in child container as a transient singleton but is inherited from the parent instead then it will go back to the parent when nested container is disposed. This then defeats the whole idea. If on the other hand session is to be created in nested container as a transient singleton then how this can be accomplished? How can I verify the correctness of such setup?

Upvotes: 0

Views: 408

Answers (1)

Steven
Steven

Reputation: 172666

If I set IDocumentSession as Per Web Request then why the need for nested containers in the first place?

There is no need for nested containers in that scenario.

The need for nested containers by itself in general is even doubtful. Even the designer of Castle Windsor (a container that supports child containers) is considering to remove support for child containers in the next version of Castle Windsor all together.

Upvotes: 1

Related Questions