Skip to main content
added 617 characters in body
Source Link
Jiří Herník
  • 2.5k
  • 1
  • 25
  • 27

After hours of trying different methods, with more or less success, this is whatWell I ended with. It doesn't end in a deadlock while getting result and itwas using this approach for years, which also gets and throws the original exceptionhandles and notpropagates exceptions from the wrapped oneunderlying async task. Which works flawlessly.

private ReturnTypestring RunSync() {   var task = Task.Run(async () => await myMethodAsyncGenerateCodeService.GenerateCodeAsync(agency));   if (task.IsFaulted && task.Exception != null)   {   throw task.Exception;   }   return task.Result; } 

But since that Microsoft created this async helper: https://github.com/aspnet/AspNetIdentity/blob/main/src/Microsoft.AspNet.Identity.Core/AsyncHelper.cs

Here is also their source:

public static void RunSync(Func<Task> func) { var cultureUi = CultureInfo.CurrentUICulture; var culture = CultureInfo.CurrentCulture; _myTaskFactory.StartNew(() => { Thread.CurrentThread.CurrentCulture = culture;   Thread.CurrentThread.CurrentUICulture = cultureUi; return func(); }).Unwrap().GetAwaiter().GetResult(); } 

After hours of trying different methods, with more or less success, this is what I ended with. It doesn't end in a deadlock while getting result and it also gets and throws the original exception and not the wrapped one.

private ReturnType RunSync() { var task = Task.Run(async () => await myMethodAsync(agency)); if (task.IsFaulted && task.Exception != null) { throw task.Exception; } return task.Result; } 

Well I was using this approach for years, which also handles and propagates exceptions from the underlying async task. Which works flawlessly.

private string RunSync() {   var task = Task.Run(async () => await GenerateCodeService.GenerateCodeAsync());   if (task.IsFaulted && task.Exception != null)   {   throw task.Exception;   }   return task.Result; } 

But since that Microsoft created this async helper: https://github.com/aspnet/AspNetIdentity/blob/main/src/Microsoft.AspNet.Identity.Core/AsyncHelper.cs

Here is also their source:

public static void RunSync(Func<Task> func) { var cultureUi = CultureInfo.CurrentUICulture; var culture = CultureInfo.CurrentCulture; _myTaskFactory.StartNew(() => { Thread.CurrentThread.CurrentCulture = culture;   Thread.CurrentThread.CurrentUICulture = cultureUi; return func(); }).Unwrap().GetAwaiter().GetResult(); } 
added 54 characters in body
Source Link
Jiří Herník
  • 2.5k
  • 1
  • 25
  • 27

After hours of trying different methods, with more or less success, this is what I ended with. It doesn't end in a deadlock while getting result and it also gets and throws the original exception and not the wrapped one.

private ReturnType RunSync() { var task = Task.Run(async () => await myMethodAsync(agency)); if (task.IsFaulted && task.Exception != null) { throw task.Exception; } return task.Result; } 

After hours of trying different methods, with more or less success, this is what I ended with. It also gets and throws the original exception and not the wrapped one.

private ReturnType RunSync() { var task = Task.Run(async () => await myMethodAsync(agency)); if (task.IsFaulted && task.Exception != null) { throw task.Exception; } return task.Result; } 

After hours of trying different methods, with more or less success, this is what I ended with. It doesn't end in a deadlock while getting result and it also gets and throws the original exception and not the wrapped one.

private ReturnType RunSync() { var task = Task.Run(async () => await myMethodAsync(agency)); if (task.IsFaulted && task.Exception != null) { throw task.Exception; } return task.Result; } 
added 4 characters in body
Source Link
Jiří Herník
  • 2.5k
  • 1
  • 25
  • 27

After hours of trying different methods, with more or less success, this is what I ended with. It also gets and throws the original exception and not the wrapped one.

private ReturnType RunSync() { var task = Task.Run(async () => await myMethodAsync(agency)); if (task.IsFaulted && task.Exception != null) { throw task.Exception; } return task.Result; } 

After hours of trying different methods, with more or less success, this is what I ended with. It also gets throws the original exception and not the wrapped one.

private ReturnType RunSync() { var task = Task.Run(async () => await myMethodAsync(agency)); if (task.IsFaulted && task.Exception != null) { throw task.Exception; } return task.Result; } 

After hours of trying different methods, with more or less success, this is what I ended with. It also gets and throws the original exception and not the wrapped one.

private ReturnType RunSync() { var task = Task.Run(async () => await myMethodAsync(agency)); if (task.IsFaulted && task.Exception != null) { throw task.Exception; } return task.Result; } 
Source Link
Jiří Herník
  • 2.5k
  • 1
  • 25
  • 27
Loading