C# Azure通知中心.net api无法异步工作
我按照教程为IOS应用程序创建了推送通知中心 当我运行代码时C# Azure通知中心.net api无法异步工作,c#,ios,.net,azure,async-await,C#,Ios,.net,Azure,Async Await,我按照教程为IOS应用程序创建了推送通知中心 当我运行代码时 private static async void SendNotificationAsync() { NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(host, "sideview", false); var alert = "{\"aps\":{\"alert\":\"Hello\"}}";
private static async void SendNotificationAsync()
{
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(host, "sideview", false);
var alert = "{\"aps\":{\"alert\":\"Hello\"}}";
await hub.SendAppleNativeNotificationAsync(alert);
}
从控制台程序的静态void Main(string[]args)中,没有发生任何事情,控制台只是停止
如果我使用
private static void SendNotificationAsync()
{
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(host, "sideview", false);
var alert = "{\"aps\":{\"alert\":\"Hello\"}}";
hub.SendAppleNativeNotificationAsync(alert).Wait();
}
一切正常
更新
正如Yuval Itzhakov在下面的回答中所说,控制台应用程序的主方法不能标记为异步,因此异步方法不会被等待控制台应用程序的主方法不能标记为
async
。因此,您需要在异步操作上显式使用Task.Wait
或Task.Result
,以确保consoles主方法不会终止,从而关闭整个过程
我假设该电话大致如下:
public static void Main(string[] args)
{
// Do some stuff
SendNotificationAsync();
}
你需要做两件事:
SendNotificationAsync
更改为async Task
,而不是async void
,以便可以等待返回的任务。注意async void
仅用于异步事件处理程序的兼容性:
private static async Task SendNotificationAsync()
{
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(host, "sideview", false);
var alert = "{\"aps\":{\"alert\":\"Hello\"}}";
await hub.SendAppleNativeNotificationAsync(alert);
}
Task.Wait
:
public static void Main(string[] args)
{
// Do some stuff
SendNotificationAsync().Wait();
}
这在控制台应用程序中可以正常工作。除了默认的
ThreadPoolSynchronizationContext
之外,对于任何具有自定义SynchronizationContext
的应用程序,都不建议使用此方法。在这些类型的应用程序中始终使用wait
。控制台应用程序的主方法不能标记为async
。因此,您需要在异步操作上显式使用Task.Wait
或Task.Result
,以确保consoles主方法不会终止,从而关闭整个过程
我假设该电话大致如下:
public static void Main(string[] args)
{
// Do some stuff
SendNotificationAsync();
}
你需要做两件事:
SendNotificationAsync
更改为async Task
,而不是async void
,以便可以等待返回的任务。注意async void
仅用于异步事件处理程序的兼容性:
private static async Task SendNotificationAsync()
{
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(host, "sideview", false);
var alert = "{\"aps\":{\"alert\":\"Hello\"}}";
await hub.SendAppleNativeNotificationAsync(alert);
}
Task.Wait
:
public static void Main(string[] args)
{
// Do some stuff
SendNotificationAsync().Wait();
}
这在控制台应用程序中可以正常工作。除了默认的
ThreadPoolSynchronizationContext
之外,对于任何具有自定义SynchronizationContext
的应用程序,都不建议使用此方法。在这些类型的应用程序中始终使用await
。您需要展示如何调用这些方法-将代码包括在您的问题中。我要打赌,您是从一个无法等待此方法的方法调用此方法,因为它是异步无效的,因此,没有办法等待方法完成。你需要展示你是如何调用这些方法的-在你的问题中包含代码。我要打赌,你是从一个不能等待此方法的方法调用此方法的,因为它是一个异步无效,因此,没有办法等待方法完成。因此,如果我在web api“生态系统”中使用异步方法,我应该对私有静态异步任务SendNotificationAsync()有任何问题,{NotificationHubClient hub=NotificationHubClient.CreateClientFromConnectionString(主机,“侧视图”,false);var alert=“{\'aps\”:{\'alert\”:\'Hello\“}”wait hub.SendAppleNativeNotificationAsync(alert);}?为什么你认为你会有任何问题?我想说我不应该有问题,你应该很好。所以如果我在web api“生态系统”中使用异步方法,我应该对私有静态异步任务SendNotificationAsync()有任何问题。{NotificationHubClient hub=NotificationHubClient.CreateClientFromConnectionString(主机,“侧视图”,false);var alert=“{aps\”:{“alert\”:“Hello\”};wait hub.SendAppleNativeNotificationAsync(alert);}你为什么认为你会有问题?我想说我不应该有问题,你应该很好。