Note that there are some explanatory texts on larger screens.

plurals
  1. PONinject + Poller: Get Session based on Tenant
    primarykey
    data
    text
    <p>I'm using NInject in a multitenant setup where I'm setting up <i>n</i> amount of pollers. </p> <p>For each thread in the service class, I want to have each my bound classes to get an NHibernate session dependent on the tenant being used in the thread(the created poller). I've got something similar already in use for the web side of things where I've bound it to a session variable in the request and let the ISession. </p> <p>Sample code follows:</p> <p>In the service init class.... </p> <pre><code> ////Tenant is a static ThreadLocal, Helper gets session for right tenant... kernel.bind&lt;ISession&gt;().ToMethod(Helper.getSession(Poller.Tenant).WhenClassHas&lt;UseTenant&gt;(); //for each tenant, start thread{ this.Poller = new Poller(kernel, s.tenantKey); //// Clean up from an unsafe shutdown Task.Factory.StartNew(() =&gt; { this.Poller.CleanUpFromShutdown(); }) .ContinueWith(t =&gt; { this.PollerThread = Thread.CurrentThread; this.Poller.StartPolling(); }); } </code></pre> <p>Simplified poller class</p> <pre><code>public class Poller{ public static ThreadLocal&lt;tenant&gt; ten = new ThreadLocal&lt;tenant&gt;(); public static Tenant Tenant{ get{ return ten.Value(); } set{ ten.Value = value; } } public IKernel kernel; public Poller(Kernel kernel, int tenantKey) { Tenant = kernel.Get&lt;TenantService&gt;().getTenantbykey(tenantKey); this.kernel = kernel; } public void StartPolling() { do{ //Attaches FooService each respective tenant session factory var kernel.Get&lt;FooService&gt;().GetAllFoos(); //do business logic //then sleep until next poll interval }while(NotCanceled) } } </code></pre> <p>EDIT:Noticing I didn't really use a question. </p> <p>Is seems like there would be a better way to do this than binding to ThreadLocal. </p> <p>Also, in the StartPolling() and CleanUpFromShutDown(), I need to reset the tenant before using any of their respective sessions. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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