Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>i've not ever looked at the unhaddins, but here's what i use for wcf stuff, should probably work for multithreaded general stuff too i imagine.</p> <p>this is the session context:</p> <pre><code>namespace Common.Infrastructure.WCF { public class NHibernateWcfSessionContext : ICurrentSessionContext { private readonly ISessionFactoryImplementor factory; public NHibernateWcfSessionContext(ISessionFactoryImplementor factory) { this.factory = factory; } /// &lt;summary&gt; /// Retrieve the current session for the session factory. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public ISession CurrentSession() { Lazy&lt;ISession&gt; initializer; var currentSessionFactoryMap = OperationContext.Current.InstanceContext.Extensions.Find&lt;NHibernateContextManager&gt;().SessionFactoryMaps; if (currentSessionFactoryMap == null || !currentSessionFactoryMap.TryGetValue(factory, out initializer)) { return null; } return initializer.Value; } /// &lt;summary&gt; /// Bind a new sessionInitializer to the context of the sessionFactory. /// &lt;/summary&gt; /// &lt;param name="sessionInitializer"&gt;&lt;/param&gt; /// &lt;param name="sessionFactory"&gt;&lt;/param&gt; public static void Bind(Lazy&lt;ISession&gt; sessionInitializer, ISessionFactory sessionFactory) { var map = OperationContext.Current.InstanceContext.Extensions.Find&lt;NHibernateContextManager&gt;().SessionFactoryMaps;; map[sessionFactory] = sessionInitializer; } /// &lt;summary&gt; /// Unbind the current session of the session factory. /// &lt;/summary&gt; /// &lt;param name="sessionFactory"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static ISession UnBind(ISessionFactory sessionFactory) { var map = OperationContext.Current.InstanceContext.Extensions.Find&lt;NHibernateContextManager&gt;().SessionFactoryMaps; var sessionInitializer = map[sessionFactory]; map[sessionFactory] = null; if (sessionInitializer == null || !sessionInitializer.IsValueCreated) return null; return sessionInitializer.Value; } } } </code></pre> <p>this is the context manager :</p> <pre><code>namespace Common.Infrastructure.WCF { class NHibernateContextManager : IExtension&lt;InstanceContext&gt; { public IDictionary&lt;ISessionFactory, Lazy&lt;ISession&gt;&gt; SessionFactoryMaps = new Dictionary&lt;ISessionFactory, Lazy&lt;ISession&gt;&gt;(); public void Attach(InstanceContext owner) { //We have been attached to the Current operation context from the ServiceInstanceProvider } public void Detach(InstanceContext owner) { } } } </code></pre> <h2>Edit:</h2> <p>to be clear, as the other answer states, the thread static context will work out of the box. the main advantage of what i have here is 1) you get to be in control, and 2) its a lazy implementation, so you don't have to start a session for each thread if its not necessary. less connection to the db is always better, imho.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload