Well, not 100% sure where to start .. you don’t need context because you are doing it wrong :-)
First, why do you call "configure the request container" in general and why do you create a child container? You do not do this :-) There are two scopes, the application scope configured by the ConfigureApplicationContainer override and the query scope configured by the ConfigureRequestContainer override, you do not name them yourself, you simply redefine them depending on how you want your objects to be owned.
Secondly, the default Nancy bootloader will be the “auto-registrar” of everything it can, in its standard implementation of ConfigureApplicationContainer. By calling the "base" after manual registration, you are actually copying your original registration with the auto-registrar. Either do not call the database, or call it before performing manual registration. And, again, do not call ConfigureRequestContainer from your ConfigureApplicationContainer :-)
If you don’t care about everything related to the application area (so that the singetons get the same instance for each request), then you do not need any of this, you can simply rely on the auto-recorder.
Currently, you create your objects manually and put them in a container, which seems like a rather strange way to do this. Usually you just register types and let the container handle be processed as needed.
You do not override ConfigureRequestContainer, you just create a new method (with a different signature).
So what you probably want is something like:
protected override void ConfigureApplicationContainer(TinyIoCContainer container) { base.ConfigureApplicationContainer(container); // Autoregister will actually do this for us, so we don't need this line, // but I'll keep it here to demonstrate. By Default anything registered // against an interface will be a singleton instance. container.Register<IRavenSessionManager, RavenSessionManager>(); } // Need to override this, not just make a new method protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context) { // Get our session manager - this will "bubble up" to the parent container // and get our application scope singleton var session = container.Resolve<IRavenSessionManager>().GetSession(); // We can put this in context.items and it will be disposed when the request ends // assuming it implements IDisposable. context.Items["RavenSession"] = session; // Just guessing what this type is called container.Register<IRavenSession>(session); container.Register<ISearchRepository, SearchRepository>(); container.Register<IResponseFactory, ResponseFactory>(); }
Steven robbins
source share