Ninject范围和System.Threading.Timer [英] Ninject Scope and System.Threading.Timer

查看:96
本文介绍了Ninject范围和System.Threading.Timer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何调用一个定时器任务的新Ninject背景?
有一个很难配置ninject范围的UnitOfWork。如果我设置为InRequestScope()它不会在非请求的任务。所以我成立了类似下面这样我就可以得到InThreadScope的任务:

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));

在的DbContext不与新的数据库值刷新。

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).

所以...如何调用新Ninject背景,以反映当前的状态/分贝值?

So...How to invoke a new Ninject "context" to reflect current state / db values?

推荐答案

您真的不应该在ASP.NET应用程序中运行的后台任务。写服务来做到这一点。 <一href=\"http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx\" rel=\"nofollow\">http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx

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

通过使用Windows服务它变得非常容易。安装程序请求一个计时器 SomeProcessor ​​完成所有从内核的工作。现在,您可以使用NamedScope扩展,并定义了处理器对于像UOW

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");

这篇关于Ninject范围和System.Threading.Timer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆