【问题标题】:How to resolve an aborted request on awaited call to HttpStatusCode?如何解决等待调用 HttpStatusCode 时中止的请求?
【发布时间】: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


【解决方案1】:

我认为您要检查的状态代码是HttpStatusCode.OK,我很确定您收到的错误:“请求被中止。连接意外关闭”是一个例外。您确定 AeroGear 服务器正在运行并且您不需要为您的 PushsClassLibrary 配置代理吗?

否则 AeroGear 项目也有您可以使用的 C# sender library

【讨论】:

  • 那是我已经在使用的库。代理配置在那里我已经隐藏了它并显示了代码的要点。
猜你喜欢
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-21
  • 1970-01-01
  • 2017-11-24
  • 2013-12-10
  • 1970-01-01
相关资源
最近更新 更多