diff --git a/src/RestSharp/AsyncHelpers.cs b/src/RestSharp/AsyncHelpers.cs
index 5d3db9db4..ea4e5f77d 100644
--- a/src/RestSharp/AsyncHelpers.cs
+++ b/src/RestSharp/AsyncHelpers.cs
@@ -14,29 +14,9 @@
//
// Adapted from Rebus
-using System.Collections.Concurrent;
-using System.Runtime.ExceptionServices;
-
namespace RestSharp;
static class AsyncHelpers {
- ///
- /// Executes a task synchronously on the calling thread by installing a temporary synchronization context that queues continuations
- ///
- /// Callback for asynchronous task to run
- static void RunSync(Func task) {
- var currentContext = SynchronizationContext.Current;
- var customContext = new CustomSynchronizationContext(task);
-
- try {
- SynchronizationContext.SetSynchronizationContext(customContext);
- customContext.Run();
- }
- finally {
- SynchronizationContext.SetSynchronizationContext(currentContext);
- }
- }
-
///
/// Executes a task synchronously on the calling thread by installing a temporary synchronization context that queues continuations
///
@@ -44,84 +24,8 @@ static void RunSync(Func task) {
/// Return type for the task
/// Return value from the task
public static T RunSync(Func> task) {
- T result = default!;
- RunSync(async () => { result = await task(); });
- return result;
- }
-
- ///
- /// Synchronization context that can be "pumped" in order to have it execute continuations posted back to it
- ///
- class CustomSynchronizationContext : SynchronizationContext {
- readonly ConcurrentQueue> _items = new();
- readonly AutoResetEvent _workItemsWaiting = new(false);
- readonly Func _task;
- ExceptionDispatchInfo? _caughtException;
- bool _done;
-
- ///
- /// Constructor for the custom context
- ///
- /// Task to execute
- public CustomSynchronizationContext(Func task) =>
- _task = task ?? throw new ArgumentNullException(nameof(task), "Please remember to pass a Task to be executed");
-
- ///
- /// When overridden in a derived class, dispatches an asynchronous message to a synchronization context.
- ///
- /// Callback function
- /// Callback state
- public override void Post(SendOrPostCallback function, object? state) {
- _items.Enqueue(Tuple.Create(function, state));
- _workItemsWaiting.Set();
- }
-
- ///
- /// Enqueues the function to be executed and executes all resulting continuations until it is completely done
- ///
- public void Run() {
- Post(PostCallback, null);
-
- while (!_done) {
- if (_items.TryDequeue(out var task)) {
- task.Item1(task.Item2);
- if (_caughtException == null) {
- continue;
- }
- _caughtException.Throw();
- }
- else {
- _workItemsWaiting.WaitOne();
- }
- }
-
- return;
-
- async void PostCallback(object? _) {
- try {
- await _task().ConfigureAwait(false);
- }
- catch (Exception exception) {
- _caughtException = ExceptionDispatchInfo.Capture(exception);
- throw;
- }
- finally {
- Post(_ => _done = true, null);
- }
- }
- }
-
- ///
- /// When overridden in a derived class, dispatches a synchronous message to a synchronization context.
- ///
- /// Callback function
- /// Callback state
- public override void Send(SendOrPostCallback function, object? state) => throw new NotSupportedException("Cannot send to same thread");
-
- ///
- /// When overridden in a derived class, creates a copy of the synchronization context. Not needed, so just return ourselves.
- ///
- /// Copy of the context
- public override SynchronizationContext CreateCopy() => this;
+ var t = Task.Run(async () => await task().ConfigureAwait(false));
+
+ return t.GetAwaiter().GetResult();
}
}
\ No newline at end of file