ServiceStack, Structuremap 3 and disposing HybridHttpOrThreadLocalScoped intances

瀏覽次數:202 次
跳到第一則未讀訊息

Anton Kallenberg

未讀,
2014年6月30日 上午10:46:372014/6/30
收件者:servic...@googlegroups.com
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

RR

未讀,
2014年12月23日 下午4:31:332014/12/23
收件者:servic...@googlegroups.com
Anton,

Did you find the solution to this? I'm dealing with the exact same situation right now. I'm calling HttpContextLifecycle.DisposeAndClearAll(); in my Application_EndRequest() method and it's not calling Dispose on my objects that implement IDisposable. They are registered with StructureMap (3) as HttpContextLifecyle. Stepping through the code, executing DisposeAndClearAll() does not call the Dispose method on EfUnitOfWork. Thanks for any help you could offer.

      protected void Application_EndRequest(Object sender, EventArgs e)
        {
            // Will call dispose on IDisposable objects in IoC container that
            // have been scoped to the request.
            HttpContextLifecycle.DisposeAndClearAll();
        }

For<IUnitOfWork>().LifecycleIs(new HttpContextLifecycle())
                        .Use(new EfUnitOfWork(new DynamicDbContext(AppConfig.Instance[ConfigSettings.PEDB_CONNECTIONSTRING])));

 public class EfUnitOfWork : IUnitOfWork, IDisposable
...
private bool _disposed;        
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _dbContext.Dispose();
                }
            }
            _disposed = true;
回覆所有人
回覆作者
轉寄
0 則新訊息