WinForm中Auth0Client.LoginAsync异步死锁及线程模式错误求助
WinForms中二次调用Auth0 LoginAsync出现异步死锁/无法弹出认证窗口问题
主Windows窗体中已实现Auth0登录功能,认证窗口可正常弹出并成功返回Token,代码如下:
var oidClient = new Auth0Client(options); LoginResult result = await oidClient.LoginAsync();
当主认证流程获取的Token过期后,尝试在另一个Windows窗体中复用类似逻辑实现Auth0登录时遇到问题:代码陷入无响应状态,疑似异步死锁。尝试了三种方案均未解决:
- 方案1返回“Cannot change thread mode after it is set”错误
- 所有方案均无法弹出Auth0认证窗口
尝试的代码如下:
public LoginResult AuthenticateViaAuth0() { try { if (SynchronizationContext.Current == null) { SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext()); } var options = new Auth0ClientOptions { ClientId = WConstants.AUTH0_CLIENT_ID, Domain = WConstants.AUTH0_DOMAIN, EnableTelemetry = true, RedirectUri = WConstants.AUTH0_REDIRECT_URI }; //try 1 var oidClient = new Auth0Client(options); var result = Task.Run(async () => await oidClient.LoginAsync()); //cannot change threadmode after it is set error return result.Result; //try 2 //loginResult = await oidClient.LoginAsync(); //running nonstop //try3 //Task<LoginResult> rr = oidClient.LoginAsync(); //var result = rr.ConfigureAwait(false); //bool state = true; //do //{ // if (rr.IsCompleted) // { // state = false; // this.loginResult = result.GetAwaiter().GetResult(); // } //} while (state = true); //return this.loginResult; //Running not stopping } catch (Exception ex) { //exception handling } }
注:同一窗体中曾遇到GraphQL API请求的异步死锁问题,已通过以下代码解决且运行正常:
var graphQLResponse = Task.Run(async () => await graphQLClient.SendQueryAsync<Template>(graphQLRequest)).Result;
提前感谢各位的帮助。
内容的提问来源于stack exchange,提问作者ApsSanj
相关产品推荐
相关产品推荐

