如何解决等待调用 HttpStatusCode 时中止的请求?

How to resolve an aborted request on awaited call to HttpStatusCode?

我正在等待使用 Aero Gear .Net client 发送推送通知的呼叫。推送调用的 return 值是 System.Net.HttpStatusCode。推送成功的预期代码是 "Accepted".

但是当我等待对 SendPushNotification 方法的调用时,它会在 HttpStatusCode 被 return 从该行的方法编辑之前抛出一个 "The request was aborted. The connection was closed unexpectedly" 错误:

System.Net.HttpStatusCode status = await client.Send(unifiedMessage);    

我将控制器对 push 方法的调用标记为异步,并在调用异步方法的地方添加了 await。

问题:

如何解决等待调用 HttpStatusCode 时中止的请求?

这是从控制器中的初始调用到 push class 方法的调用要点:

控制器:

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


}

推送Class:

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

我认为您要检查的状态代码是 HttpStatusCode.OK 并且我很确定您得到的错误:"The request was aborted. The connection was closed unexpectedly" 是一个例外。您确定 AeroGear 服务器是 运行 并且您的 PushsClassLibrary 不需要代理配置吗?

否则,AeroGear 项目还有一个 C# sender library 您可以使用。