如何释放HybridHttpOrThreadLocalScoped对象StructureMap?对象、HybridHttpOrThreadLocalScoped、StructureMap

由网友(星星熬成糖浆)分享简介:当在Windows服务我用 HybridHttpOrThreadLocalScoped 存储的NHibernate的iSessions的intances执行后台任务。 When performing background tasks in a Windows Service I used HybridHttpOrTh...

当在Windows服务我用 HybridHttpOrThreadLocalScoped 存储的NHibernate的iSessions的intances执行后台任务。

When performing background tasks in a Windows Service I used HybridHttpOrThreadLocalScoped for storing intances of NHibernate ISessions.

由于在Windows Server中还没有一个HttpContext的,我想知道如果只调用 ReleaseAndDisposeAllHttpScopedObjects()是足以释放ISession实例为主题?

Since within a Windows Server there isn't a HTTPContext, I'm wondering if only calling the ReleaseAndDisposeAllHttpScopedObjects() is enough to release the ISession instance for that thread?

推荐答案

我发现了如何回答这个问题。该 ReleaseAndDisposeAllHttpScopedObjects()法的的ObjectFactory暴露的是真的关心的HttpContext,因此Web应用程序。

I found out how to answer this question. The ReleaseAndDisposeAllHttpScopedObjects() method exposed by the ObjectFactory is really concerned with the HttpContext and therefore web applications.

从Structuremap.Pipeline命名空间的HybridLifeCycle类允许直接访问缓存的对象ThreadLocal的内部存储和处理它们。下面是一个例子:

The HybridLifeCycle class from the Structuremap.Pipeline namespace allows to directly access cached objects inside the ThreadLocal storage and dispose them. Here is an example:

Action.For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use<UnitOfWork>();

以上code寄存器提供的类型和缓存中的HttpContext或ThreadLocal中存储的实例。它总是以自IDisposable inherite这些类型是个好主意。因此,在本实施例中的UnitOfWork也是IDisposable的

Above code registers the supplied type and caches its instances in the HttpContext or the ThreadLocal storage. It's always a good idea to inherite those types from IDisposable. Thus in this example UnitOfWork is also an IDisposable.

new HybridLifecycle().FindCache().DisposeAndClear();

现在处置缓存对象的Web应用程序或窗口服务regardsless上面的行足以处置的 ReleaseAndDisposeAllHttpScopedObjects()法的UnitOfWork代替。我希望这可以帮助别人。

Now to dispose cached objects regardsless of a web application or windows service the above line is sufficient to dispose the UnitOfWork instead of the ReleaseAndDisposeAllHttpScopedObjects() method. I hope this helps someone.

阅读全文

相关推荐

最新文章