I'm building a Nancy web app, and using OrmLite for DB access. I noticed that every request opens up a new DB connection and doesn't close it. I thought that registering the OrmLiteConnection class in the Application container would make it application-scoped, but it looks like I'm missing something.
Here's my code (in ConfigureApplicationContainer
):
container.Register<OrmLiteConnectionFactory>(new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));
container.Register<OrmLiteConnection>(
(cContainer, overloads) => (OrmLiteConnection) cContainer.Resolve<OrmLiteConnectionFactory>().Open());
You need to add scope to your registration(s):
AFAIK, this will ensure the instances are disposed at the scope's end. Therefore, if you need to do more than
Dispose()
then you might need to find some way of supplying a delegate that can executed at that time.I moved registration of
OrmLiteConnection
toConfigureRequestContainer
. Then I overrodeRequestStartup
and added: