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

相关文章
相关标签/搜索