【发布时间】:2012-11-16 02:39:48
【问题描述】:
我有一个 asp.net Web 应用程序,它可以通知业务客户有关可用约会的信息。 场景如下。
- 页面按钮单击事件处理程序将新约会保存到数据库
- 注册客户的偏好与创建的新约会相匹配
- 首选项匹配的客户将通过 Twilio 发送一条带有约会详细信息的 SMS 消息。
我的问题是我需要将第 2 步和第 3 步与页面的执行分开。因此,一旦创建了约会,以下步骤将异步传递给帮助类,该类在后台线程上处理匹配和 SMS 消息,同时页面控件返回并将用户重定向到另一个页面。后台类方法不返回任何值(public void)。
很抱歉没有早点发布代码。
createappointment.aspx.cs
private void SaveAppointment()
{
using (var db = new EntitiesModel())
{
//code to create a new appointment
//Sending SMS messages to multiple clients
SMSHelper myhelper = new SMSHelper();
myhelper.SendApptSMS(newappt);
//control returned to the page redirect to dashboard
Response.Redirect("~/authuser/default.aspx");
}
}
smshelper.cs
public async void SendApptSMS(Appointment newappointment)
{
using (var db = new EntitiesModel())
{
//retrieve multiple clients that match preferences
foreach (var item in clients)
{
//for each client send SMS message using Twilio's REST API
}
//Thread.Sleep(10000);
}
}
我希望页面在调用异步方法后继续处理,然后在后台线程上执行。 这不会发生。 在 SMSHelper 发送完所有消息之前,不会到达 response.redirect。
希望这能解决问题。
【问题讨论】:
-
这么多选项 - 使用线程、事件/mq/pubsub 等。
-
背景踏板在 asp.net 中存在问题。在 Azure 中,您可以使用 Web 作业。见curah.microsoft.com/52143/…
标签: c# asp.net asynchronous twilio