【问题标题】:Method to Send Automatic Report in Webhosting在虚拟主机中发送自动报告的方法
【发布时间】:2014-05-20 23:36:51
【问题描述】:
我有在 Visual Studio 2010 中创建的 Web 项目,并使用 SQL Server 2008 R2 作为数据库,
通常我使用 SQL Server 代理将报告(从 SQL 数据库)发送到我的客户电子邮件,现在我想将我的应用程序移动到其他网络托管。我的问题是有什么方法可以在虚拟主机中按计划自动生成报告并发送到电子邮件?
感谢您的建议。
【问题讨论】:
标签:
c#
asp.net
vb.net
scheduled-tasks
web-hosting
【解决方案1】:
使用Revalee 开源项目可以在网络项目中安排任务。
Revalee 是一项服务,可让您安排 Web 回调到您的 Web 应用程序。在您的情况下,您将安排一个回调,该回调将生成您的报告并自动发送电子邮件。 Revalee 非常适合处理离散事务操作的任务,例如更新数据库值或发送自动电子邮件消息(阅读:运行时间不长)。生成报告和发送电子邮件的代码将驻留在您的 Web 应用程序中。更具体地说,在 Revalee 将在您的预定时间调用的目标网页上。当您的应用程序第一次启动时,您将安排第一个 Web 回调。当您的应用程序被回调以生成报告时,您将安排下一次回调。
要使用 Revalee,您应该:
在您的服务器上安装 Revalee 服务,这是一项 Windows 服务。 Windows 服务以源代码(您可以自己编译)或预编译版本的形式提供,可在Revalee website(上图)获得。
在您的 Visual Studio 项目中使用 Revalee 客户端库。客户端库在源代码中提供(同样,您将自己编译)或通过NuGet 提供的预编译版本。
-
当您的应用程序通过ScheduleMidnightCallback() 方法(见下文)启动时,您将注册一个未来的回调(例如,在特定时间)。
private DateTimeOffet? previousCallbackTime = null;
private void ScheduleMidnightCallback()
{
// Schedule your callback for midnight tomorrow
var tomorrow = DateTimeOffset.Now.AddDays(1.0);
var callbackTime = new DateTimeOffset(tomorrow.Year,
tomorrow.Month,
tomorrow.Day,
0, // Hour
0, // Minute
0, // Second
tomorrow.Offset);
// Your web service's Uri, including any query string parameters your app might need
Uri callbackUrl = new Uri("http://yourwebapp.com/Callback.aspx");
// Register the callback request with the Revalee service
RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl);
previousCallbackTime = callbackTime;
}
-
当网络计划任务激活并回调您的应用程序时,您将在午夜执行您需要执行的任何操作并且您也计划下一个回调。为此,您可以将以下方法调用 (CallbackMonitor()) 添加到您的 Callback.aspx 页面处理程序中。
private DateTimeOffset? mostRecentCallback = null;
private void CallbackMonitor()
{
var midnight = new DateTimeOffset(DateTimeOffset.Now.Year,
DateTimeOffset.Now.Month,
DateTimeOffset.Now.Day,
0, // Hour
0, // Minute
0, // Second
DateTimeOffset.Now.Offset);
if (!mostRecentCallback.HasValue
|| mostRecentCallback.Value < midnight)
{
mostRecentCallback = midnight;
// Perform your report generation & send email-related tasks
// ...do your work here...
// Schedule subsequent callback
ScheduleMidnightCallback();
}
}
我希望这会有所帮助。
最后一件事:Revalee 也有一个 ASP.NET MVC 特定库,以防您将 Web 应用程序从 ASP.NET 迁移到 ASP.NET MVC。
免责声明:我是参与 Revalee 项目的开发人员之一。然而,需要明确的是,Revalee 是免费的开源软件。源代码在GitHub 上提供。