NamedScopes Ninject and async bindings (threading)

My project is structured by services and repositories (all repositories share the db context). In one of my service levels, I have an asynchronous method that writes to the database using the repository. The web request will complete and delete the context before this method can use it. I tried to understand NamedScopes as indicated in this. I still do not understand how to implement this. I will show how my project is structured and hope someone can help me at the code level.

Handcuffs

    private static void RegisterServices(IKernel kernel)
    {
        //dbcontext
        kernel.Bind<EntityDatabaseContext>().ToMethod(context => new EntityDatabaseContext()).InRequestScope();

        //unit of work
        kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();

        //repositories
        kernel.Bind<IRepository<Account>>().To<Repository<Account>>().InRequestScope();

        //services
        kernel.Bind<IAuthenticationService>().To<AuthenticationService>().InRequestScope();
    }

Authentication Service uses constructor injection

public AuthenticationService(UnitOfWork unitOfWork, IRepository<Account> accountRepository){}

Method inside my AuthenticationService

    //this is a background process
    public Task SomeMethodAsync(string text)
    {
        //spin it off into a new task
        return Task.Factory.StartNew(() => SomeMethod(text));
    }

SomeMethod accountRepository. , , . , , NamedScopes , ?

, , ninject - .

+5
1

, . IIS , ( ), .

http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx

- Windows async Windows, . MSMQ.

, HostingEnvironment.RegisterObject IRegisteredObject, .

Ninject . , . MyJobProcessor . INotifyWhenDisposed. - DisposeNotifyingObject.

public class MyJobProcessor : DisposeNotifyingObject, IRegisteredObject
{
    public void Execute() { ... }
    public void Stop(bool immediate) { ... }
}

, .

Task.Factory.StartNew(() => 
    { 
        try 
        { 
            processor.Execute(); 
        } 
        finally 
        { 
            processor.Dispose); 
        }
    });

, .

Bind<MyJobProcessor>().ToSelf().Named("MyJobProcessor").DefinesNamedScope("MyJobProcessorScope");
Bind<IUnitOfWork>().To<UnitOfWork>().WhenAnyAnchestorNamed("MyJobProcessor").InNamedScope("MyJobProcessorScope");
+6

All Articles