我正在尝试使用IdentityServer身份验证创建WPF客户端。我正在使用他们的 OidcClient
进行登录。它完全是异步的,而我的应用程序是同步的,并且如果不付出巨大的努力就无法重构。调用
var result = await _oidcClient.LoginAsync();
不等待结果。调用 Wait()
或 .Result
会导致死锁。将其包装到其他 Task.Run
会提示该方法未在 UI 线程上运行(它会通过登录对话框打开浏览器)。
您知道如何解决这个问题吗?我需要编写自定义同步OidcClient
吗?
最佳答案
与其他类似的情况一样,您需要在不进行太多重构的情况下向遗留应用程序引入异步,我建议使用简单的“请稍候...”模式对话框。该对话框启动异步操作并在操作完成后自行关闭。
Window.ShowDialog
是一个同步 API,它会阻止主 UI,并且仅在模式对话框关闭时才返回给调用者。但是,它仍然运行嵌套消息循环并泵送消息。因此,异步任务继续回调仍然会被抽取和执行,而不是使用容易死锁的 Task.Wait()
。
这是一个基本但完整的 WPF 示例,使用 Task.Delay()
模拟 _oidcClient.LoginAsync()
并在 UI 线程上执行它,请参阅WpfTaskExt.Execute
了解详细信息。
取消支持是可选的;如果无法取消实际的 LoginAsync
,则可以防止对话框过早关闭。
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
var button = new Button() { Content = "Login", Width = 100, Height = 20 };
button.Click += HandleLogin;
this.Content = button;
}
// simulate _oidcClient.LoginAsync
static async Task<bool> LoginAsync(CancellationToken token)
{
await Task.Delay(5000, token);
return true;
}
void HandleLogin(object sender, RoutedEventArgs e)
{
try
{
var result = WpfTaskExt.Execute(
taskFunc: token => LoginAsync(token),
createDialog: () =>
new Window
{
Owner = this,
Width = 320,
Height = 200,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Content = new TextBox
{
Text = "Loggin in, please wait... ",
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center
},
WindowStyle = WindowStyle.ToolWindow
},
token: CancellationToken.None);
MessageBox.Show($"Success: {result}");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
public static class WpfTaskExt
{
/// <summary>
/// Execute an async func synchronously on a UI thread,
/// on a modal dialog's nested message loop
/// </summary>
public static TResult Execute<TResult>(
Func<CancellationToken, Task<TResult>> taskFunc,
Func<Window> createDialog,
CancellationToken token = default(CancellationToken))
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(token);
var dialog = createDialog();
var canClose = false;
Task<TResult> task = null;
async Task<TResult> taskRunner()
{
try
{
return await taskFunc(cts.Token);
}
finally
{
canClose = true;
if (dialog.IsLoaded)
{
dialog.Close();
}
}
}
dialog.Closing += (_, args) =>
{
if (!canClose)
{
args.Cancel = true; // must stay open for now
cts.Cancel();
}
};
dialog.Loaded += (_, __) =>
{
task = taskRunner();
};
dialog.ShowDialog();
return task.GetAwaiter().GetResult();
}
}
}
关于c# - 在 UI 线程上调用异步方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53508160/