Asp.net mvc Windows Azure服务总线通知中心仅接收部分通知
我有一个类似于以下内容的ASP.NET MVC控制器方法:Asp.net mvc Windows Azure服务总线通知中心仅接收部分通知,asp.net-mvc,azure,apple-push-notifications,async-await,azure-notificationhub,Asp.net Mvc,Azure,Apple Push Notifications,Async Await,Azure Notificationhub,我有一个类似于以下内容的ASP.NET MVC控制器方法: public JsonResult GenerateNotifications() { Task.Factory.StartNew(() => MyService.GenerateNotifications()); return Json(new { success = true }, JsonRequestBehavior.AllowGet); } 我的服务方法如下所示:
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");
}
}
public异步void GenerateNotifications()
{
NotificationHubClient hub=NotificationHubClient.CreateClientFromConnectionString(AppHelper.AzureNotificationHubConnectionString,“myhub”);
对于(int i=0;i<10;i++)
{
var通知=新建
{
aps=新
{
alert=string.Format(“Awesome通知{0}”,i),
sound=“默认值”
}
};
string notificationJSON=JsonConvert.SerializeObject(通知);
NotificationOutput结果=wait hub.SendAppleNativeNotificationAsync(notificationJSON,“mytag”);
}
}
我遇到的问题是,在我应该收到的10份通知中,我通常只收到5-6份
我处理async
调用的方式是否有问题
我是否应该以与控制器不同的方式生成通知?您可能会遇到一些异常,这些异常正在被吞没,因为您的
生成通知
方法是异步无效
。您应该返回一个任务
,并确保等待该方法
,以便观察异常并重试
一旦有了它,就不用使用Task.Factory.StartNew(()=>MyService.GenerateNotifications())
而只需等待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");
}
}
您是否尝试引入延迟,并查看是否收到所有通知?这可能是因为他们被你的想法扼杀了!我已经实现了这些(包括Task.WhenAll),但它似乎仍然没有发送所有通知(例如,6个通知中只有4个)。还有其他想法吗?总是收到通知的是同一个客户端,还是它的行为是随机的?
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);
}