【发布时间】:2016-08-10 14:04:53
【问题描述】:
我正在等待使用Aero Gear .Net client 发送推送通知的电话。 push 调用的返回值是System.Net.HttpStatusCode。推送成功后的预期代码为“Accepted”。
但是当我等待调用 SendPushNotification 方法时,它会在从该方法返回 HttpStatusCode 之前抛出一个"The request was aborted. The connection was closed unexpectedly"error:
System.Net.HttpStatusCode status = await client.Send(unifiedMessage);
我将控制器对 push 方法的调用标记为异步,并在调用异步方法的地方添加了 await。
问题:
如何解决等待调用 HttpStatusCode 时中止的请求?
这是从控制器中的初始调用到 push 类方法的调用要点:
控制器:
//In Controller Post action that calls SendPushNotification
[HttpPost]
public async Task<ActionResult> Index(Order exec_order)
{
try
{
//Send a push notification with the order details
try
{
MyLogger.FileLogger.Info("Before Push Notification");
System.Net.HttpStatusCode status = await PushsClassLibrary.PushNotification.SendPushNotification(exec_order.Application", "Order Summary");
MyLogger.FileLogger.Info("Push Notification Status: " + status);
}
catch (Exception ex) //request aborted error caught here
{
MyLogger.FileLogger.Error("Push Notification Exception -" + ex.Message);
}
}
推送类:
//In PushNotification class containing SendPushNotification
public static async Task<System.Net.HttpStatusCode> SendPushNotification(string messageToPush, string serviceName )
{
string[] listUsers = users.ToArray();
unifiedMessage.criteria.alias = listUsers;
System.Net.HttpStatusCode status = await client.Send(unifiedMessage);
return status;
}
【问题讨论】:
-
你确定你的方法
SendPushNotification()没有抛出任何异常吗?
标签: c# asp.net-mvc async-await http-status-codes aerogear