发布到api ASP.NET API时收到错误InvalidOperationException

时间:2018-06-11 20:43:25

标签: c# asp.net asp.net-web-api dotnet-httpclient

当我调用发布到api的方法并从我的api提供推送通知时,我收到了错误消息。我收到错误" InvalidOperationException:在异步操作仍处于挂起状态时完成异步模块或处理程序。"这是我发给api的代码

public static async void postToPushNotifcationAsync(String deviceId, String message, String title)
{
    var fcm = new FCMJsonModel();
    var fcmNotification = new FCMNotificationModel();

    fcmNotification.body = message;
    fcmNotification.title = title;
    fcmNotification.sound = "sound.caf";
    fcm.to = deviceId;
    fcm.notification = fcmNotification;

    var json = fcm.ToJSON();

    var content = new StringContent(json);
    content.Headers.ContentType = new
    System.Net.Http.Headers.MediaTypeHeaderValue("application/json");


    //client.Timeout = TimeSpan.FromMinutes(2);

    var cts = new CancellationTokenSource();
    cts.CancelAfter(TimeSpan.FromMinutes(1));

    var response = await
    client.PostAsJsonAsync("https://fcm.googleapis.com/fcm/send", content,
    cts.Token);

    var responseString = await response.Content.ReadAsStringAsync();
}

我将我的httpclient声明为全局类变量,因为我必须多次使用此方法。

private static readonly HttpClient client = new HttpClient();

0 个答案:

没有答案