Hello Guys,
I want to use one single open IDbConnection through each request and by the end of each request close and dispose the connection. To keep the code DRY if want StrucureMap to do this for me.
When configuring ServiceStack I register my repositories as HybridHttpOrThreadLocalScoped:
container.Configure(x => x.For(typeof(IRepository<>)).HybridHttpOrThreadLocalScoped().Use(typeof(Repository<>)));
In my app host I override OnEndRequest and call DisposeAndClearAll to make sure that all HybridHttpOrThreadLocalScoped-instances are disposed:
public override void OnEndRequest()
{
if (HttpContextLifecycle.HasContext())
{
HttpContextLifecycle.DisposeAndClearAll();
}
base.OnEndRequest();
}
My repositories implements IDisposable and disposes the IDbConnection:
public virtual void Dispose()
{
if (db == null)
{
return;
}
db.Dispose();
}
but unfortunately the IDbConnection is not disposed and I'm running out of the connection quite fast :)
Am I missing something or do you suggest a different approach?
Regards,
Anton