Ninject Scope and System.Threading.Timer

别来无恙 提交于 2019-12-24 18:41:05

问题


How to invoke a new Ninject context on a Timer task? Having a hard time configuring ninject scope to unitofwork. If I set as InRequestScope() it doesn't work on non request tasks. So I set up like below so that I can get InThreadScope for tasks:

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

But that way, when I set up a timer

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

the dbContext doesn't refresh with the 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 to invoke a new Ninject "context" to reflect current state / db values?


回答1:


You really shouldn't run background tasks in an ASP.NET application. Write a service to do this. http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx

By using a windows service it gets really easy. Setup a timer that requests a SomeProcessor that does all the work from the kernel. Now you can use the NamedScope extension and define that the processor is the scope for some objects like the UoW

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


来源:https://stackoverflow.com/questions/11444454/ninject-scope-and-system-threading-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!