我有一个async方法:
public async Task<string> GenerateCodeAsync()
{
string code = await GenerateCodeService.GenerateCodeAsync();
return code;
}
我需要从一个同步方法调用这个方法。
我如何才能做到这一点,而不必复制GenerateCodeAsync方法,以使其同步工作?
更新
但没有找到合理的解决方案。
但是,我看到HttpClient已经实现了这个模式
using (HttpClient client = new HttpClient())
{
// async
HttpResponseMessage responseAsync = await client.GetAsync(url);
// sync
HttpResponseMessage responseSync = client.GetAsync(url).Result;
}
Microsoft Identity具有同步调用异步方法的扩展方法。
例如,有GenerateUserIdentityAsync()方法和CreateIdentity()方法
如果你查看UserManagerExtensions.CreateIdentity()
它是这样的:
public static ClaimsIdentity CreateIdentity<TUser, TKey>(this UserManager<TUser, TKey> manager, TUser user,
string authenticationType)
where TKey : IEquatable<TKey>
where TUser : class, IUser<TKey>
{
if (manager == null)
{
throw new ArgumentNullException("manager");
}
return AsyncHelper.RunSync(() => manager.CreateIdentityAsync(user, authenticationType));
}
现在让我们看看AsyncHelper。RunSync确实
public static TResult RunSync<TResult>(Func<Task<TResult>> func)
{
var cultureUi = CultureInfo.CurrentUICulture;
var culture = CultureInfo.CurrentCulture;
return _myTaskFactory.StartNew(() =>
{
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = cultureUi;
return func();
}).Unwrap().GetAwaiter().GetResult();
}
这是async方法的包装器。
请不要从Result中读取数据——它可能会在ASP中阻塞你的代码。
还有另一种方法——对我来说有点可疑,但你也可以考虑一下
Result r = null;
YourAsyncMethod()
.ContinueWith(t =>
{
r = t.Result;
})
.Wait();
有些扩展方法异步等待异步操作的完成,然后设置ManualResetEvent来指示完成。
注意:你可以使用Task.Run(),但是扩展方法是一个更清晰的接口来表达你真正想要的东西。
显示如何使用扩展的测试:
[TestClass]
public class TaskExtensionsTests
{
[TestMethod]
public void AsynchronousOperationWithNoResult()
{
SampleAsynchronousOperationWithNoResult().AwaitResult();
}
[TestMethod]
public void AsynchronousOperationWithResult()
{
Assert.AreEqual(3, SampleAsynchronousOperationWithResult(3).AwaitResult());
}
[TestMethod]
[ExpectedException(typeof(Exception))]
public void AsynchronousOperationWithNoResultThrows()
{
SampleAsynchronousOperationWithNoResultThrows().AwaitResult();
}
[TestMethod]
[ExpectedException(typeof(Exception))]
public void AsynchronousOperationWithResultThrows()
{
SampleAsynchronousOperationWithResultThrows(3).AwaitResult();
}
private static async Task SampleAsynchronousOperationWithNoResult()
{
await Task.Yield();
}
private static async Task<T> SampleAsynchronousOperationWithResult<T>(T result)
{
await Task.Yield();
return result;
}
private static async Task SampleAsynchronousOperationWithNoResultThrows()
{
await Task.Yield();
throw new Exception();
}
private static async Task<T> SampleAsynchronousOperationWithResultThrows<T>(T result)
{
await Task.Yield();
throw new Exception();
}
[TestMethod]
public void AsynchronousValueOperationWithNoResult()
{
SampleAsynchronousValueOperationWithNoResult().AwaitResult();
}
[TestMethod]
public void AsynchronousValueOperationWithResult()
{
Assert.AreEqual(3, SampleAsynchronousValueOperationWithResult(3).AwaitResult());
}
[TestMethod]
[ExpectedException(typeof(Exception))]
public void AsynchronousValueOperationWithNoResultThrows()
{
SampleAsynchronousValueOperationWithNoResultThrows().AwaitResult();
}
[TestMethod]
[ExpectedException(typeof(Exception))]
public void AsynchronousValueOperationWithResultThrows()
{
SampleAsynchronousValueOperationWithResultThrows(3).AwaitResult();
}
private static async ValueTask SampleAsynchronousValueOperationWithNoResult()
{
await Task.Yield();
}
private static async ValueTask<T> SampleAsynchronousValueOperationWithResult<T>(T result)
{
await Task.Yield();
return result;
}
private static async ValueTask SampleAsynchronousValueOperationWithNoResultThrows()
{
await Task.Yield();
throw new Exception();
}
private static async ValueTask<T> SampleAsynchronousValueOperationWithResultThrows<T>(T result)
{
await Task.Yield();
throw new Exception();
}
}
扩展
/// <summary>
/// Defines extension methods for <see cref="Task"/> and <see cref="ValueTask"/>.
/// </summary>
public static class TaskExtensions
{
/// <summary>
/// Synchronously await the results of an asynchronous operation without deadlocking; ignoring cancellation.
/// </summary>
/// <param name="task">
/// The <see cref="Task"/> representing the pending operation.
/// </param>
public static void AwaitCompletion(this ValueTask task)
{
new SynchronousAwaiter(task, true).GetResult();
}
/// <summary>
/// Synchronously await the results of an asynchronous operation without deadlocking; ignoring cancellation.
/// </summary>
/// <param name="task">
/// The <see cref="Task"/> representing the pending operation.
/// </param>
public static void AwaitCompletion(this Task task)
{
new SynchronousAwaiter(task, true).GetResult();
}
/// <summary>
/// Synchronously await the results of an asynchronous operation without deadlocking.
/// </summary>
/// <param name="task">
/// The <see cref="Task"/> representing the pending operation.
/// </param>
/// <typeparam name="T">
/// The result type of the operation.
/// </typeparam>
/// <returns>
/// The result of the operation.
/// </returns>
public static T AwaitResult<T>(this Task<T> task)
{
return new SynchronousAwaiter<T>(task).GetResult();
}
/// <summary>
/// Synchronously await the results of an asynchronous operation without deadlocking.
/// </summary>
/// <param name="task">
/// The <see cref="Task"/> representing the pending operation.
/// </param>
public static void AwaitResult(this Task task)
{
new SynchronousAwaiter(task).GetResult();
}
/// <summary>
/// Synchronously await the results of an asynchronous operation without deadlocking.
/// </summary>
/// <param name="task">
/// The <see cref="ValueTask"/> representing the pending operation.
/// </param>
/// <typeparam name="T">
/// The result type of the operation.
/// </typeparam>
/// <returns>
/// The result of the operation.
/// </returns>
public static T AwaitResult<T>(this ValueTask<T> task)
{
return new SynchronousAwaiter<T>(task).GetResult();
}
/// <summary>
/// Synchronously await the results of an asynchronous operation without deadlocking.
/// </summary>
/// <param name="task">
/// The <see cref="ValueTask"/> representing the pending operation.
/// </param>
public static void AwaitResult(this ValueTask task)
{
new SynchronousAwaiter(task).GetResult();
}
/// <summary>
/// Ignore the <see cref="OperationCanceledException"/> if the operation is cancelled.
/// </summary>
/// <param name="task">
/// The <see cref="Task"/> representing the asynchronous operation whose cancellation is to be ignored.
/// </param>
/// <returns>
/// The <see cref="Task"/> representing the asynchronous operation whose cancellation is ignored.
/// </returns>
public static async Task IgnoreCancellationResult(this Task task)
{
try
{
await task.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
}
/// <summary>
/// Ignore the <see cref="OperationCanceledException"/> if the operation is cancelled.
/// </summary>
/// <param name="task">
/// The <see cref="ValueTask"/> representing the asynchronous operation whose cancellation is to be ignored.
/// </param>
/// <returns>
/// The <see cref="ValueTask"/> representing the asynchronous operation whose cancellation is ignored.
/// </returns>
public static async ValueTask IgnoreCancellationResult(this ValueTask task)
{
try
{
await task.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
}
/// <summary>
/// Ignore the results of an asynchronous operation allowing it to run and die silently in the background.
/// </summary>
/// <param name="task">
/// The <see cref="Task"/> representing the asynchronous operation whose results are to be ignored.
/// </param>
public static async void IgnoreResult(this Task task)
{
try
{
await task.ConfigureAwait(false);
}
catch
{
// ignore exceptions
}
}
/// <summary>
/// Ignore the results of an asynchronous operation allowing it to run and die silently in the background.
/// </summary>
/// <param name="task">
/// The <see cref="ValueTask"/> representing the asynchronous operation whose results are to be ignored.
/// </param>
public static async void IgnoreResult(this ValueTask task)
{
try
{
await task.ConfigureAwait(false);
}
catch
{
// ignore exceptions
}
}
}
/// <summary>
/// Internal class for waiting for asynchronous operations that have a result.
/// </summary>
/// <typeparam name="TResult">
/// The result type.
/// </typeparam>
public class SynchronousAwaiter<TResult>
{
/// <summary>
/// The manual reset event signaling completion.
/// </summary>
private readonly ManualResetEvent manualResetEvent;
/// <summary>
/// The exception thrown by the asynchronous operation.
/// </summary>
private Exception exception;
/// <summary>
/// The result of the asynchronous operation.
/// </summary>
private TResult result;
/// <summary>
/// Initializes a new instance of the <see cref="SynchronousAwaiter{TResult}"/> class.
/// </summary>
/// <param name="task">
/// The task representing an asynchronous operation.
/// </param>
public SynchronousAwaiter(Task<TResult> task)
{
this.manualResetEvent = new ManualResetEvent(false);
this.WaitFor(task);
}
/// <summary>
/// Initializes a new instance of the <see cref="SynchronousAwaiter{TResult}"/> class.
/// </summary>
/// <param name="task">
/// The task representing an asynchronous operation.
/// </param>
public SynchronousAwaiter(ValueTask<TResult> task)
{
this.manualResetEvent = new ManualResetEvent(false);
this.WaitFor(task);
}
/// <summary>
/// Gets a value indicating whether the operation is complete.
/// </summary>
public bool IsComplete => this.manualResetEvent.WaitOne(0);
/// <summary>
/// Synchronously get the result of an asynchronous operation.
/// </summary>
/// <returns>
/// The result of the asynchronous operation.
/// </returns>
public TResult GetResult()
{
this.manualResetEvent.WaitOne();
return this.exception != null ? throw this.exception : this.result;
}
/// <summary>
/// Tries to synchronously get the result of an asynchronous operation.
/// </summary>
/// <param name="operationResult">
/// The result of the operation.
/// </param>
/// <returns>
/// The result of the asynchronous operation.
/// </returns>
public bool TryGetResult(out TResult operationResult)
{
if (this.IsComplete)
{
operationResult = this.exception != null ? throw this.exception : this.result;
return true;
}
operationResult = default;
return false;
}
/// <summary>
/// Background "thread" which waits for the specified asynchronous operation to complete.
/// </summary>
/// <param name="task">
/// The task.
/// </param>
private async void WaitFor(Task<TResult> task)
{
try
{
this.result = await task.ConfigureAwait(false);
}
catch (Exception exception)
{
this.exception = exception;
}
finally
{
this.manualResetEvent.Set();
}
}
/// <summary>
/// Background "thread" which waits for the specified asynchronous operation to complete.
/// </summary>
/// <param name="task">
/// The task.
/// </param>
private async void WaitFor(ValueTask<TResult> task)
{
try
{
this.result = await task.ConfigureAwait(false);
}
catch (Exception exception)
{
this.exception = exception;
}
finally
{
this.manualResetEvent.Set();
}
}
}
/// <summary>
/// Internal class for waiting for asynchronous operations that have no result.
/// </summary>
public class SynchronousAwaiter
{
/// <summary>
/// The manual reset event signaling completion.
/// </summary>
private readonly ManualResetEvent manualResetEvent = new ManualResetEvent(false);
/// <summary>
/// The exception thrown by the asynchronous operation.
/// </summary>
private Exception exception;
/// <summary>
/// Initializes a new instance of the <see cref="SynchronousAwaiter{TResult}"/> class.
/// </summary>
/// <param name="task">
/// The task representing an asynchronous operation.
/// </param>
/// <param name="ignoreCancellation">
/// Indicates whether to ignore cancellation. Default is false.
/// </param>
public SynchronousAwaiter(Task task, bool ignoreCancellation = false)
{
this.manualResetEvent = new ManualResetEvent(false);
this.WaitFor(task, ignoreCancellation);
}
/// <summary>
/// Initializes a new instance of the <see cref="SynchronousAwaiter{TResult}"/> class.
/// </summary>
/// <param name="task">
/// The task representing an asynchronous operation.
/// </param>
/// <param name="ignoreCancellation">
/// Indicates whether to ignore cancellation. Default is false.
/// </param>
public SynchronousAwaiter(ValueTask task, bool ignoreCancellation = false)
{
this.manualResetEvent = new ManualResetEvent(false);
this.WaitFor(task, ignoreCancellation);
}
/// <summary>
/// Gets a value indicating whether the operation is complete.
/// </summary>
public bool IsComplete => this.manualResetEvent.WaitOne(0);
/// <summary>
/// Synchronously get the result of an asynchronous operation.
/// </summary>
public void GetResult()
{
this.manualResetEvent.WaitOne();
if (this.exception != null)
{
throw this.exception;
}
}
/// <summary>
/// Background "thread" which waits for the specified asynchronous operation to complete.
/// </summary>
/// <param name="task">
/// The task.
/// </param>
/// <param name="ignoreCancellation">
/// Indicates whether to ignore cancellation. Default is false.
/// </param>
private async void WaitFor(Task task, bool ignoreCancellation)
{
try
{
await task.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
this.exception = exception;
}
finally
{
this.manualResetEvent.Set();
}
}
/// <summary>
/// Background "thread" which waits for the specified asynchronous operation to complete.
/// </summary>
/// <param name="task">
/// The task.
/// </param>
/// <param name="ignoreCancellation">
/// Indicates whether to ignore cancellation. Default is false.
/// </param>
private async void WaitFor(ValueTask task, bool ignoreCancellation)
{
try
{
await task.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
this.exception = exception;
}
finally
{
this.manualResetEvent.Set();
}
}
}
}