Note that there are some explanatory texts on larger screens.

plurals
  1. PONamedScopes Ninject bindings and async (threading)
    primarykey
    data
    text
    <p>My project is structured by services and repositories (all repositories share the db context). In one of my service layers, I have an asynchronous method that writes to the database using a repository. The web request will finish and dispose of the context before this method can get to use it. I tried to understand <a href="http://www.planetgeek.ch/2010/12/08/how-to-use-the-additional-ninject-scopes-of-namedscope/" rel="nofollow noreferrer">NamedScopes</a> as stated in this <a href="https://stackoverflow.com/a/10669539/400861">answer</a>. I still can't seem to understand how to implement it. I'll show how my project is structured and hope someone can help me at the code level.</p> <p><strong>Bindings</strong></p> <pre><code> private static void RegisterServices(IKernel kernel) { //dbcontext kernel.Bind&lt;EntityDatabaseContext&gt;().ToMethod(context =&gt; new EntityDatabaseContext()).InRequestScope(); //unit of work kernel.Bind&lt;IUnitOfWork&gt;().To&lt;UnitOfWork&gt;().InRequestScope(); //repositories kernel.Bind&lt;IRepository&lt;Account&gt;&gt;().To&lt;Repository&lt;Account&gt;&gt;().InRequestScope(); //services kernel.Bind&lt;IAuthenticationService&gt;().To&lt;AuthenticationService&gt;().InRequestScope(); } </code></pre> <p><strong>AuthenticationService uses constructor injection</strong></p> <pre><code>public AuthenticationService(UnitOfWork unitOfWork, IRepository&lt;Account&gt; accountRepository){} </code></pre> <p><strong>A method inside my AuthenticationService</strong></p> <pre><code> //this is a background process public Task SomeMethodAsync(string text) { //spin it off into a new task return Task.Factory.StartNew(() =&gt; SomeMethod(text)); } </code></pre> <p><code>SomeMethod</code> makes use of <code>accountRepository</code>. Please tell me if anymore information is needed. Please help me with the threading issue, if NamedScopes is the solution, how do I implement it in my case? </p> <p>Basically, a background process is being executed and it is using a context that is being disposed of by ninject due to the request scope.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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