Recurring tasks in ASP .NET
Asked Answered
L

3

5

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.

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.

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.

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

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

Lodie answered 29/12, 2011 at 11:4 Comment(4)
check this one: https://mcmap.net/q/274120/-how-to-use-quartz-net-with-asp-net-for-scheduling-jobs-to-trigger-mailHolo
Yes, I'm aware of Quartz. I'm looking for a custom solution without depending on external licenses, commercial or open source.Lodie
How do you get the data from your provider? You said "does not have a mechanism for calling a URL". Are you saying they can't call you, or you can't call them, or both?Unders
I mean, every time a transaction is made with the payment processor, they don't notify us. We have to poll their database. In order to poll them, we need some kind of a scheduled task.Lodie
S
6

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

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

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

HostingEnvironment.RegisterObject(this);

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.

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

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

Solvolysis answered 13/6, 2013 at 12:52 Comment(1)
A year late but I like the solution and will try it in a current project. Thanks.Lodie
P
3

To update for 2018 - The Hangfire NuGet package is perfect for this

Patrilineal answered 29/5, 2018 at 13:1 Comment(0)
L
0

Since there were no answers, I thought I'd post my solution in case it helps others.

Not the ideal approach by any means but for those who might gain from it, I created a cron job on another Linux hosting account we had to call the required ASP .NET url. Management horror but does the job.

Lodie answered 11/1, 2012 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.