在ASP .NET中重复执行的任务 [英] Recurring tasks in ASP .NET

查看:379
本文介绍了在ASP .NET中重复执行的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP .NET网站在一个共享的环境中运行GoDaddy的。该应用程序是为定期结算给用户选项的基于订阅的服务。

I have an ASP .NET website running on GoDaddy in a shared environment. The application is a subscription-based service with options for recurring billing to users.

每隔一小时,我们需要的用户数据与我们的支付处理器同步更新谁升级或取消其帐户的用户。支付处理器,没有一个机制来调用一个URL或以其他方式通知我们的变化。

Every hour, we need to synchronize user data with our payment processor to update users who have upgraded or cancelled their accounts. The payment processor, does not have a mechanism for calling a URL or otherwise notifying us of changes.

问题:我们需要创建一个运行在predefined间隔约code进行后台线程。有大约在.NET后台任务的一些好文章,但我相信,有可能是解决这个简单的方法。也许一个应用程序范围内计时器,可以调用函数等。

The problem: We need to create a background thread that runs some code at a predefined interval. There are some good articles about background tasks in .NET but I am sure, there could be a simpler way around this. Maybe an application-wide timer that can call a function, etc.

的限制:共享的环境不允许Windows服务,外部应用程序,完全信任,等

The limitation: Shared environment does not allow windows services, external applications, full-trust, etc.

由于这是一个生产应用程序,我想用最安全的方法可能而不是胳膊扭IIS。

Since this is a production application, I would like to use the safest approach possible rather than arm-twisting IIS.

推荐答案

我有一个类似的问题,我正在开发一个概念证明ASP和使用,将执行可能需要几个小时的任务的后台线程。问题是,ASP.Net可以回收随时在AppDomain(杀害我的后台线程)。

I had a similar problem, I'm developing a ASP proof of concept and use a background thread that performs a task that could take several hours. Problem is, ASP.Net can recycle the AppDomain at anytime (killing my background thread).

要prevent这一点,您可以注册后台线程ASP.Net所以它会通知你的线程关闭。要做到这一点实现以下接口:

To prevent this, you can register your background thread to ASP.Net so it will notify your thread to shut down. To do this implement the following interface:

public interface IRegisteredObject
{
    void Stop(bool immediate);
}

和使用下面的静态方法注册对象ASP:

And register your object to ASP using the following static method:

HostingEnvironment.RegisterObject(this);

在ASP.NET眼泪就下来了的AppDomain中,它会首先尝试调用的方法停止所有注册对象。在大多数情况下,它会立即设置为false两次调用此方法一次。这使你的code一点时间来完成它在做什么。 ASP.NET提供了IRegisteredObject的所有实例共30秒就可以完成自己的工作,每个在30秒。那段时间跨度之后,如果有任何离开注册的对象,它会再次立即设置为true打电话给他们。

When ASP.NET tears down the AppDomain, it will first attempt to call Stop method on all registered objects. In most cases, it’ll call this method twice, once with immediate set to false. This gives your code a bit of time to finish what it is doing. ASP.NET gives all instances of IRegisteredObject a total of 30 seconds to complete their work, not 30 seconds each. After that time span, if there are any registered objects left, it will call them again with immediate set to true.

通过$ P $无法返回(通过锁定时,工人正忙于一个字段)pventing Stop方法,我们停止从ASP到我们的工作完成后关闭的AppDomain。

By preventing the Stop method from returning (by locking a field when the worker is busy), we stop ASP from shutting down the AppDomain until our work is finished.

public void Stop(bool immediate)
{
    lock (_lock)
    {
        _shuttingDown = true;
    }
    HostingEnvironment.UnregisterObject(this); 
}

public void DoWork(Action work)
{
    lock (_lock)
    {
        if (_shuttingDown)
        {
            return;
        }
        work();
    }
}

使用一个任务,而不是行动从取消选项中受益。为您的特定情况下,你可以启动执行这样的任务,一个计时器。

Use a Task instead of action to benefit from cancellation options. For your specific case you could start a timer that executes tasks like this.

PS。这是一个黑客和ASP并不意味着运行后台任务,使尽可能使用Windows服务或WCF服务!我用这个,因为它简化了开发,维护和安装。

PS. This is a hack and ASP isn't meant to run background tasks so use a windows service or WCF service when possible! I use this since it simplifies development, maintenance and installation.

有关详细信息,请参阅我的源代码:<一href=\"http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx\">http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx

For more information see my source: http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx

这篇关于在ASP .NET中重复执行的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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