在net4的mvc4中,尝试在同步方法中调用异步方法的结果,发生死锁

问题描述 投票:0回答:1

在net4的mvc4中,尝试在同步方法中调用异步方法的结果,发生死锁。

1。我希望它同步运行

 public  RespData PushMsg(string tel, int push_type, int push_type_sub, string alert, string title, string content, string href){

    var result = Push.HMSPushV3(tel, push_type, push_type_sub, alert, title, content, 
             href).ConfigureAwait(false).GetAwaiter().GetResult();

    return reuslt;

}

2。这是被调用的异步方法

  public async static Task<RespData> HMSPushV3(string tel, int push_type, int push_type_sub, string alert, string title, string content, string href)
        {
            RespData r = new RespData();
            string[] arr = tel.Split(',');
            try
            {
                var device_token_list = Push.GetPolicy().GetHMSPushDeviceToken(arr);
                var data = new PushModel() { alert = alert, content = content, href = href, push_type = push_type, title = title, push_type_sub_enum = push_type_sub };

                {
                     //Locked up in this place
                    var messageId = await HMSPush.SendToData(device_token_list, JsonConvert.SerializeObject(data));
                    r.Msg = $"透传:msgId {messageId}";
                }
                {
                    var message = HMSPush.CreateAndroidMessage(data);
                    var messageId = await HMSPush.SendToNotification(device_token_list, message);
                    r.Msg += $"  通知栏:msgId {messageId}";
                }
                r.State = 1;
                r.Data = JsonConvert.SerializeObject(data);

            }
            catch (Exception e)
            {
                DB.FileLog.Error(e.Message, e);
                r.Msg = e.Message;
                r.State = 0;
            }
            return r;
        }
c# asp.net .net-4.0
1个回答
0
投票

尝试将ConfigureAwait(false)添加到正在等待的异步方法中。

此方法并不意味着将不等待该调用,只是不必在同一线程上继续进行该调用:

var messageId = await HMSPush.SendToData(device_token_list, JsonConvert.SerializeObject(data))
                                 .ConfigureAwait(false);
© www.soinside.com 2019 - 2024. All rights reserved.