简体   繁体   English

在托管Web服务器中安排作业

[英]Schedule a job in hosted web server

Can some one give me a best way to implement a daily job with .NET technology. 有人可以给我一个使用.NET技术实现日常工作的最佳方法。 I have an asp.net application with the sqlserver database hosted in shared hosting, GODaddy in my instance. 我有一个asp.net应用程序,其中sqlserver数据库托管在我的实例中的共享主机GODaddy中。 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. 最初我想写一个Windows服务,但godaddy不允许访问除托管应用程序之外的数据库。 Does someone has any idea to send alerts daily at 1:00AM? 有人有任何想法每天凌晨1点发送警报吗?

Thanks in advance 提前致谢

See Easy Background Tasks in ASP.NET by Jeff Atwood. 请参阅Jeff Atwood的ASP.NET中简易后台任务

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

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. 我没有将GoDaddy用于域名注册以外的任何其他内容,因此我对您在其托管平台上可以做或不可以做的事情没有任何经验。 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. 我也不知道他们的支持或知识基础是什么样的,但我认为你最好的选择是向GoDaddy询问他们的建议。 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. 这些天有很多好的ASP.NET主机可用。

You can use windows scheduler from the web server to schedule a stored procedure call that can send mail based on particular criteria. 您可以使用Web服务器中的Windows计划程序来计划可以根据特定条件发送邮件的存储过程调用。

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

References: 参考文献:

Many hosting providers can request a URL for you every X minutes. 许多托管服务提供商可以每隔X分钟为您请求一个URL。 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. 我不知道GoDaddy是否会这样做,但如果是这样,你可以创建一个启动工作的ASMX页面,并告诉他们自动执行它。

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 如果你这样做,请确保你输入的代码限制它每隔X分钟或更长时间运行一次(可能使用静态变量或数据库表) - 阅读这个故事

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. 这可以是作为Windows服务编写的自动化过程,在调度程序下运行的应用程序,或者在凌晨1:00推送的某个按钮。 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. 仅仅因为应用程序是唯一可以访问数据库的东西并不意味着您不能以其他方式公开数据。

Use either System.Timers, System.Threading to create a instance that is run at a predetermined time. 使用System.Timers,System.Threading创建在预定时间运行的实例。 Have that thread execute whatever the task is that you want... Make sure the code is thread safe! 让该线程执行您想要的任何任务...确保代码是线程安全的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM