views:

345

answers:

7

Hello Geeks, Can some one give me a best way to implement a daily job with .NET technology. I have an asp.net application with the sqlserver database hosted in shared hosting, GODaddy in my instance. My application is used to add / change the data in the database which is performing quite fairly at this time. I got a new requirement to send some email alerts daily based on some data criteria that were stored in the database.

Initially I thought to write a windows service, but godaddy is not allowing to access the database other than its hosted applications. Does someone has any idea to send alerts daily at 1:00AM?

Thanks in advance

A: 

I haven't used GoDaddy for anything other than domain registration, so I have no experience with what you can or cannot do on their hosting platform. I also don't know what their support or knowledge base is like, but I'd say your best option is to ask GoDaddy what they recommend. Otherwise, you might keep implementing something that's technically feasible, but is blocked by the hosting company.

If it's not something that's a prime-time application, one quick and dirty thing to do is to have some kind of external bot calling a (secure) web page on the server that fires off the notification process. Not a real solution, but if this site is just a hobby of yours, it could get you by until you find something the host will allow.

Might also be a good time to find a new host, if this one is not meeting your requirements. There are lots of good ASP.NET hosts available these days.

goheen
A: 

You can use windows scheduler from the web server to schedule a stored procedure call that can send mail based on particular criteria.

osql.exe -S servername -d database -U username -P password -Q "EXEC spAlertOnCriteria"

References:

CodeToGlory
A: 

Use either System.Timers, System.Threading to create a instance that is run at a predetermined time. Have that thread execute whatever the task is that you want... Make sure the code is thread safe!

Aaron Saunders
What guarantees that the appdomain is running at 1:00 am? Wouldn't be unloaded for no activity?
Remus Rusanu
why would the thread be unloaded? It is initialized at startup and runs in the background until checking to see if there is any tasks to execute? If you did not understand.. then I apologize for the lack of detailIf you notices, the suggestion I made is basically what he has implemented
Aaron Saunders
A: 

Many hosting providers can request a URL for you every X minutes. I don't know if GoDaddy does, but if so, you could create an ASMX page that kicks off the job, and tell them to execute it automatically.

If they don't, one solution might be to fire off the job in a background thread at every page request. If you do that, make sure you put in code that limits it to running every X minutes or more (perhaps using a static variable or a database table) - read this story

SLaks
A: 

If you can expose a service on the website hosting the application and database -- authenticated service, of course -- then you can hit that service remotely from any box with credentials, pull down the data, and send the mail that way.

This could be an automated process written as a Windows service, an application that is run under the Scheduler, or some button you push at 1:00 AM. Your pick.

Just because the app is the only thing that can access the database doesn't mean you can't expose the data in other ways.

andymeadows
A: 

See Easy Background Tasks in ASP.NET by Jeff Atwood.

Copy/paste from the link:

private static CacheItemRemovedCallback OnCacheRemove = null;

protected void Application_Start(object sender, EventArgs e)
{
    AddTask("DoStuff", 60);
}

private void AddTask(string name, int seconds)
{
    OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
    HttpRuntime.Cache.Insert(name, seconds, null,
        DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
        CacheItemPriority.NotRemovable, OnCacheRemove);
}

public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
    // do stuff here if it matches our taskname, like WebRequest
    // re-add our task so it recurs
    AddTask(k, Convert.ToInt32(v));
}
Pavel Chuchuva
Thank you for your reply. Is there any performance gain that I am going to achieve with the above piece of code instead ofprotected void Application_Start(object sender, EventArgs e){ Thread invAlertThread = new Thread(newThreadStart(Alerts.SendAlerts)); invAlertThread.Name = "Invoice Alerts"; invAlertThread.Start();}where Alerts.SendAlerts is a static methodpublic class Alerts{ public static SendAlerts() { // send alerts here // 5 Hours Thread.Sleep(18000000); SendAlerts(); }}
krishna
@krishna I don't think starting new thread from Application_Start is a good idea.
Pavel Chuchuva
Can anyone confirm this to actually work on the GoDaddy windows shared host environment?
Josh
A: 

Here is a nice explanation of two possible ways http://www.beansoftware.com/ASP.NET-Tutorials/Scheduled-Tasks.aspx

AbdullahArslan