I'm trying to use dependency injection with TinyIOC in NancyFX.
What I intend to do, is have one SqlConnection per request which should be closed automatically. What I have done so far, is create a CustomBootstrapper, which overrides the ConfigureRequestContainer method and registers my IDbConnectionService with the AsPerRequestSingleton() method.
This should make sure that the SqlConnectionService exists per request and should be destroyed after the request. At least, that is what I think it should do after reading the documentation.
The custom bootstrapper:
https://github.com/mschlechter/NancyFxTutorial/blob/master/NancyFxTutorial.Web/CustomBootstrapper.cs
And the SqlConnectionService:
This service implements the IDisposable interface and should dispose the underlying SqlConnection (IDbConnectionService inherits from IDisposable).
Unfortunately, this Dispose() method is never called. So as things stand right now, it will keep connections to the database open. Which is a (really) bad thing :-)
My question is how to fix this using dependency injection.
I'm trying to emulate a pattern I know from Spring MVC on Java. Every service can call getCurrentSession() to get a Hibernate session. A request scoped SessionFactory is injected and every service can do sessionFactory.getCurrentSession() to get the current session.
I would love to do the same kind of thing using NancyFX and TinyIOC.
Getting back to my original question: why is the Dispose method not called on an object created with AsPerRequestSingleton() and what can I do to get the intended result? (an object which lives during the duration of the request / response scope, and will be automatically disposed when done)
Thanks in advance and with best regards, Marc Schlechter