ABP .Net Core 調用異步方法拋異常A second operation started on this context before a previous asynchronous ope

一、  問題描述git

最近使用ABP .Net Core框架作一個微信開發,同時採用了一個微信開發框架集成到ABP,在微信用戶關注的推送事件裏調用了一個async 方法,因爲沒有返回值,也沒作任何處理,本地調試也OK,但一發布到線上就有問題,微信公衆號關注成功,也有推送消息過來,但微信用戶一直保存不上,查看日誌會有個異常信息:github

System.NotSupportedException: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.微信

 

二、  問題分析和解決方案微信開發

經過分析,主要問題是在asyncawait需成對出現,但我這裏是集成第三方微信框架,推送事件的方法並不能直接改爲異步方法,同時保存用戶數據的方法沒有返回結果,不能使用await。最後在官方github issue列表找到了解決方法,同步方法調用異步方法AsyncHelper.RunSync,修改發佈後,該問題解決。app

 

修改代碼以下:框架

public override void Subscribe(RequestMessageEvent_Subscribe requestMessage)
{
            //獲取微信用戶信息
            var wechatUser = Senparc.Weixin.MP.AdvancedAPIs.UserApi.Info(appId, requestMessage.FromUserName);
 
            //關注公衆號
            AsyncHelper.RunSync(() => _wChatUserAppService.SubscribeAsync(requestMessage.FromUserName, wechatUser.nickname, wechatUser.headimgurl, requestMessage.EventKey, requestMessage.Ticket) );
}

  

參考官方github issue連接:異步

https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3324async

相關文章
相關標籤/搜索