0

我有一个类似于下面的 ASP.NET MVC 控制器方法:

    public JsonResult GenerateNotifications()
    {
        Task.Factory.StartNew(() => MyService.GenerateNotifications());
        return Json(new { success = true }, JsonRequestBehavior.AllowGet);
    }

我的服务方法如下所示:

    public async void GenerateNotifications()
    {
        NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(AppHelper.AzureNotificationHubConnectionString, "myhub");

        for (int i = 0; i < 10; i++)
        {
            var notification = new
            {
                aps = new
                {
                    alert = string.Format("Awesome Notification {0}", i),
                    sound = "default"
                }
            };

            string notificationJSON = JsonConvert.SerializeObject(notification);
            NotificationOutcome result = await hub.SendAppleNativeNotificationAsync(notificationJSON, "mytag");
        }
    }

我遇到的问题是,在我应该收到的 10 个通知中,我通常只收到 5-6 个。

我处理async电话的方式有问题吗?

我应该以不同于控制器的方式生成通知吗?

4

1 回答 1

2

您可能会遇到一些被吞没的异常,因为您的GenerateNotification方法是async void. 您应该返回 aTask并确保您await使用该方法,以便观察并重新抛出异常。

一旦你有了它,而不是使用Task.Factory.StartNew(() => MyService.GenerateNotifications())你可以只是await MyService.GenerateNotifications(). 有关使用的一些注意事项,请参阅异步编程中的最佳实践async/await

我会将您的代码更改为:

    public async Task<JsonResult> GenerateNotifications()
    {
        await MyService.GenerateNotifications();
        return Json(new { success = true }, JsonRequestBehavior.AllowGet);
    }

    public async Task GenerateNotifications()
    {
        NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(AppHelper.AzureNotificationHubConnectionString, "myhub");

        for (int i = 0; i < 10; i++)
        {
            var notification = new
            {
                aps = new
                {
                    alert = string.Format("Awesome Notification {0}", i),
                    sound = "default"
                }
            };

            string notificationJSON = JsonConvert.SerializeObject(notification);
            NotificationOutcome result = await hub.SendAppleNativeNotificationAsync(notificationJSON, "mytag");
        }
    }

目前,您也在连续发送通知。您可以通过将生成的任务添加到列表然后使用它们完成来并行发送await Task.WhenAll它们await

public async Task GenerateNotifications()
{
    NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(AppHelper.AzureNotificationHubConnectionString, "myhub");

    List<Task<NotificationOutcome>> notificaitonTasks = new List<Task<NotificationOutcome>>();

    for (int i = 0; i < 10; i++)
    {
        var notification = new
        {
            aps = new
            {
                alert = string.Format("Awesome Notification {0}", i),
                sound = "default"
            }
        };

        string notificationJSON = JsonConvert.SerializeObject(notification);
        notificaitonTasks.Add(hub.SendAppleNativeNotificationAsync(notificationJSON, "mytag"));
    }

    await Task.WhenAll(notificaitonTasks);
}
于 2014-10-02T02:05:53.877 回答