I came across articles below regarding when and where to use ConfigureAwait(false)
, but cannot get an answer.
You Don’t Need ConfigureAwait(false), But Still Use It in Libraries, and UI apps. (e.g. Xamarin, WinForms etc)
https://blog.stephencleary.com/2017/03/aspnetcore-synchronization-context.html
This link says opposite answer
Best practice to call ConfigureAwait for all server-side code
When correctly use Task.Run and when just async-await
My questions:
Scenario 1: The code below is running as background service.
My question: Is ConfigureAwait(false)
required for whenever await
is used like both A and B below:
[Service(Name = "com.MainApplicationService", Label = "Main Application Service", Exported = false)]
public class MainApplicationService : Android.App.Service
{
public override IBinder OnBind(Intent intent)
{
return null;
}
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
await InitAsync().ConfigureAwait(false); //line A
Task.Run(async () => await InitAsync().ConfigureAwait(false)); //line B
return StartCommandResult.Sticky;
}
}
Scenario 2: The code below is running as UI thread as opposed to background service
Same question: Is ConfigureAwait(false)
required for whenever await is used like both C and D below:
public class StartupActivity : Android.App.Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
await InitAsync().ConfigureAwait(false); //line C
Task.Run(async () => await InitAsync().ConfigureAwait(false)); //line D
Finish();
}
}
Xamarin Android ver 8, I think it is .net standard.
https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md
await
here:Task.Run(async () => await InitAsync().ConfigureAwait(false)); //line B
?async
modifier onOnStartCommand
andOnCreate
methods?void
methods like in your case, see stackoverflow.com/q/22629951/1768303 and linked Q/As.