Ninject scope and System.Threading.Timer

How to invoke a new Ninject context in a timer task? It is not easy to set up a ninject scope for unitofwork. If I installed as InRequestScope (), it does not work on tasks without a request. Therefore, I am set up as below so that I can get InThreadScope for tasks:

kernel.Bind<IUnitOfWork>().To<myEntities>().InScope(ctx => HttpContext.Current ?? StandardScopeCallbacks.Thread(ctx));

But so when I set the timer

Timer timer = new Timer(_ => DelayedDisconnect(chatUser.User.AuthorizationId), null, TimeSpan.FromSeconds(10), TimeSpan.FromMilliseconds(-1));

dbContext is not updated with new database values.

public void DelayedDisconnect(string authorizationId)
    {
        var myChatUser = GetChatUserByClaimedIdentifier(authorizationId);

        if (!myChatUser.ChatClients.Any()) // old state (doesn't reflect database changes from 10 seconds ago).

So ... How do I invoke a new Ninject "context" to reflect the current state values โ€‹โ€‹of / db?

+1
source share
1 answer

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

Windows, . , SomeProcessor, . NamedScope , , UoW

kernel.Bind<SomeProcessor>().ToSelf().DefinesNamedScope("Processor");
kernel.Bind<IUoW>().To<UoW>().InNamedScope("Processor");
+1

All Articles