59 questions
Best practices
0
votes
6
replies
130
views
Difference between await and .ConfigureAwait(true)
I understand that ConfigureAwait(false) prevents capturing the current synchronization context, allowing the continuation to run on any available thread.
However, I’m not clear about if there is any ...
2
votes
0
answers
186
views
Why is there a StackOverflowException with ConfigureAwait(false) in an ASP.NET app but not with ConfigureAwait(true)?
I have an ASP.NET app running on .NET 4.8. I am getting a StackOverflowException. I have an action method that calls another method CallBackMethod where using ConfigureAwait(false) in a statement that ...
1
vote
1
answer
257
views
Simplest way in C# to defer task execution without synchronization context [closed]
Inside an async method in C#, I need to defer the execution (return the execution to the caller), and also to clear the synchronization context before proceeding with the execution.
The statement ...
4
votes
1
answer
886
views
What does ConfigureAwait(false) in an async await foreach loop do?
I am trying to understand the impact of setting ConfigureAwait(false) in an async foreach loop?
await foreach (var item in data.ConfigureAwait(false))
{
//...
}
Exactly where do we lose the ...
1
vote
1
answer
268
views
Is this better than deciding whether to use ConfigureAwait(false)?
I recently saw this in code:
await QueryAsync.ConfigureAwait(SynchronizationContext.Current is not null || TaskScheduler.Current != TaskScheduler.Default)
My first thought was, "that's not right&...
2
votes
1
answer
342
views
ConfigureAwait(false) in long running task
I May have a misconception about something.
For me in an async method, having ConfigureAwait(false); will permit the task to continue being executed on the threadpool.
So if I Declare a LongRunning ...
0
votes
1
answer
150
views
ConfigureAwait(false) and Microsoft.EntityFrameworkCore.Storage.ContextTransaction
Is the following C# code guaranteed to have always sent a message if it is recorded as having been sent in the database, or does the ConfigureAwait(false) mean it is possible to have recorded ...
0
votes
1
answer
361
views
Why does NUnit lose its SynchronizationContext after await?
Why does this test fail?
[Test]
public async Task Test_Keeps_SynchronizationContext()
{
SynchronizationContext.Current.Should().NotBeNull(); // OK
await Task.Delay(1).ConfigureAwait(true);
...
4
votes
1
answer
4k
views
Does .NET 6's PeriodicTimer capture the current SynchronizationContext by default?
For PeriodicTimer (AsyncTimer at the time), regarding WaitForNextTickAsync, David Fowler mentioned "The execution context isn't captured" (here) via (here). However, given that was not ...
3
votes
2
answers
436
views
ConfigureAwait(false) and struct implementation of IAsyncDisposable
I have implemented IAsyncDisposable with an ActionOnAsyncDispose struct as shown below. My understanding is that the compiler will not box it when it is in an async using statement:
...
0
votes
1
answer
1k
views
UI thread waits for "await client.SendAsync(request).ConfigureAwait(false)" to finish before moving to another url
Client starts some action via button. In controller its async method:
async public Task<JsonResult> CreatePresentation
that contains a line of code
using (var response = await client.SendAsync(...
3
votes
2
answers
3k
views
C# Parallel.ForEach and Task.WhenAll sometimes returning less values then supposed
I have this:
Parallel.ForEach(numbers, (number) =>
{
var value = Regex.Replace(number, @"\s+", "%20");
tasks.Add(client.GetAsync(url + value));
});
await Task.WhenAll(...
0
votes
1
answer
2k
views
What is the difference between await and ConfigureAwait(false).GetAwaiter().GetResult(); [duplicate]
Let's say I got an async method in C# .Net Core:
public Task<ResultClass> Action() { ... }
What is the difference between invoking: ResultClass res = await Action();
and invoking: ResultClass ...
2
votes
1
answer
251
views
How can blocking async calls cause a deadlock if async calls aren't necessarily executed on a different thread?
I recently read Stephen Cleary's post regarding possible deadlocks that occur when we call async code in synchronous methods here: https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
...
0
votes
1
answer
585
views
ConfigureAwait , UI, await async
I have a small project - WinForms
On .net frameWork - just a small test :
private void button9_Click(object sender, EventArgs e)
{
string text = GetTitleAsync().Result;
button9.Text = text; ...
0
votes
0
answers
388
views
Consider Calling ConfigureAwait on the awaited task [duplicate]
I'm developing an web API using CQRS pattern. I have a command to Create Author.
Here is my Command Handler:
internal sealed class AddCommandHandler : ICommandHandler<CreateAuthorCommand, Author>...
2
votes
1
answer
852
views
Does an exception propagate using ConfigureAwait(false) with async Task and await?
I was feeling like I had quite a good grip on async await programming, but what happened today made me perplexed. I have been searching for an answer to this for a little while now, but was not able ...
0
votes
0
answers
152
views
Async ConfigureAwait(false) Exception Handling-Long Running Processes [duplicate]
I have two long processes that are inside a Task.Run block that I want to run concurrently and capture any exceptions that may occur. I don't want to wait for these tasks to complete and I want to ...
1
vote
1
answer
219
views
Task-Like and ConfigureAwait(false), is it possible?
I've been reading about task-like and awaitable types. I think I've got a good grasp on the basic mechanics of it (awaiter, asyncmethodbuilder...) but there's something amiss when I try to understand ...
79
votes
3
answers
84k
views
Usage of ConfigureAwait in .NET
I've read about ConfigureAwait in various places (including SO questions), and here are my conclusions:
ConfigureAwait(true): Runs the rest of the code on the same thread the code before the await ...
0
votes
1
answer
4k
views
ConfigureAwait(false) in .Net Core application
I am creating a cron job using .Net Core which will fetch data from API and insert data into database.
Should I use ConfigureAwait(false) while calling api in asynchronous mode?
I am confused after ...
9
votes
2
answers
2k
views
Make AsyncLocal changes propagate to the calling function
DotNet Fiddle link https://dotnetfiddle.net/GqA32R
I have the following sample code to demonstrate the async local functionality
static AsyncLocal<string> _asyncLocalString = new AsyncLocal<...
4
votes
1
answer
3k
views
The current thread is not associated with the Dispatcher. Use InvokeAsync() to switch execution to the Dispatcher when triggering rendering
I am working on Dotnet Core Blazor and getting below error while using EventCallBack to bind the parent grid after delete events. Below the code for using Child component
<tbody>
@foreach ...
2
votes
0
answers
631
views
S4462 issue: Calls to "async" methods should not be blocking. How to call asynchronous method from synchronous method?
I am trying to call an asynchronous method from a synchronous method.
For instance: Let methodAsync be an asynchronous method.
static void methodSync()
{
object1.methodAsync().Wait();
...
4
votes
3
answers
1k
views
ConfigureAwait(false) doesn't make the continuation code run in another thread
I have a code which runs on thread1.
I call a function in a synchronic way (using async method but it shouldn't disturb me - async method doesn't turn code to be asynchronic).
I have an await with ...
1
vote
0
answers
223
views
Can you use ConfigureAwait(false) with Task.WhenAny()? [duplicate]
I'm trying to implement a task based interleaving pattern similar to below.
This logic is in a library and so I'm trying to make use of ConfigureAwait(false) as I don't see a reason that the task ...
0
votes
1
answer
260
views
Cascading ConfigureAwait(false), is it necessary?
I am developing an MVC5 ASP.NET Application where I have the following question.
If I have an action in a controller defined this way:
public async Task<ActionResult> MyAction()
{
await ...
1
vote
1
answer
106
views
Is it really necessary to add .ConfigureAwait call when calling an awaitable method?
I am developing an application using Visual Studio 2019 with code validation.
Some code validation hints are important, however, I am getting a lot of hints in my awaitable method calls, such as
...
4
votes
1
answer
957
views
Can code after 'await' run in different thread in ASP.NET?
I've read here https://blog.stephencleary.com/2009/10/synchronizationcontext-properties.html that ASP.NET applications' execution context does not have specific associated thread. Does it mean code ...
0
votes
1
answer
2k
views
Using ConfigureAwait() without await [duplicate]
I have a method called Load which is being invoked from a synchronous method:
private async Task LoadAsync()
{
await Task.Run(() => // stuff....).ConfigureAwait(false);
}
public void HelloWorld(...
-1
votes
1
answer
369
views
GetAwaiter not working to wait for the completion of Quartz library's Clear method - is there a bug in Quartz or a gap in my understanding?
The latest version of the Quartz library switched everything to use async/await. The calling application has synchronous calls that are very embedded in the framework, so I am stuck calling ....
0
votes
1
answer
256
views
ConfigureAwait(true) in library
I've got an ASP.NET WebForms application which has pages using Async=True and I'm using RegisterAsyncTask(new PageAsyncTask(InitialiseAsync)); in my OnLoad method to call business logic asynchronously....
1
vote
2
answers
150
views
Trivial exit from a method that may perform an asynchronous call
I've got an issue calling a method that may return a Task<T> or null depending on the result of an initial synchronous lookup call (this itself might be an anti-pattern so please let me know).
...
2
votes
2
answers
4k
views
Should I use ConfigureAwait(false) in places like Repository?
Just read this article about ConfigureAwait and it made me think on an issue I haven't been able to come to peace with for some time now.
Consider the code below. Each dependency uses await to make ...
0
votes
0
answers
56
views
Is it necessary to use ConfigureAwait in BusinessLogic and WebApi? or just enough in one?
If I set ConfigureAwait (false) in the logic I also need to specify it in the WebApi? ASP.NET
My Business Logic
Using ConfigureAwait
My WebApi
Here you also have to set ConfigureAwait?
4
votes
1
answer
4k
views
ConfigureAwait warning in .net core 3.1
I am developing a server in ASP NET Core 3.1 preview edition and now i see that for all my async methods when calling await i get CA2007 warning (as it is when some task is running asynchronous).
Is ...
2
votes
0
answers
409
views
ConfigureAwait(false) not giving response and app get stuck
I am using refit in my xamarin.forms project to connect to the server and download some data. The code below works properly if server is online but when server is offline, I got stuck and I am not ...
4
votes
2
answers
2k
views
Is ConfigureAwait(false) required on all levels of the async chain when no real I/O call is involved?
Implementing a reusable adaptor type of library on top of Azure Document Db Client SDK.
The library can run anywhere, not only in an ASP.NET Core web service, but in a command line app, ASP.NET Web ...
4
votes
0
answers
150
views
ConfigureAwait in .Net Core console/worker - NOT Asp.Net Core [duplicate]
I have a question about using ConfigureAwait in .NET Core apps. I know that ASP.NET Core has so SynchronizationContext (http://blog.stephencleary.com/2017/03/aspnetcore-synchronization-context.html) ...
0
votes
2
answers
2k
views
Should I use configure await in all methods or only in the first method?
I have a library with async methods and I have read that for libraries, it is recommended to use ConfigureAwait(false).
For example, if I have something like:
public async Task myMethod01()
{
...
0
votes
1
answer
406
views
Why doesn't this async code continue on the same context as before it was awaited?
I'm working on trying to understand how Task.ContinueWith works. Consider the following code:
private async void HandleButtonClick(object sender, EventArgs e)
{
Console.WriteLine($"...
2
votes
1
answer
748
views
Why HttpContext.Current is not null in async/await with ConfigureAwait
I have a library async function called from controller. I expected HttpContext.Current to be null after await with ConfigureAwait(false) everywhere, but in controller it is not null. Can somebody ...
6
votes
1
answer
17k
views
Still confused on ConfigureAwait(false) used with GetAwaiter and GetResult in C#. Getting a deadlock or method not returning
I have read: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html and
the accepted answer at deadlock even after using ConfigureAwait(false) in Asp.Net flow but am just too dense to see ...
1
vote
0
answers
146
views
Custom Code Inspection for ConfigureAwait
I have created my own way to essentially set ConfigureAwait(false) just once at the top of each async method which allows me to not have to append ConfigureAwait(false) at the end of each await call. ...
3
votes
1
answer
1k
views
When is the Usage of ConfigureAwait(false) in ViewModels problematic?
I wonder in which situations I will run into problems when using
ConfigureAwait(false)
in my (Xamarin) MVVM approach. This is mainly because I do not fully understand the synchronization context a ...
0
votes
0
answers
279
views
Awaited call loses context in Unit Test
We have the following ASP.NET code (I've cut it down for clarity):
var httpContent = new StringContent(postData, Encoding.UTF8);
using (var client = this.GetClient(url, contentType))
{
using (var ...
5
votes
1
answer
5k
views
Correct way to return an async task within a non-async method
What is the best practice when returning the following Task:
public async Task<Command> BuildCommunicationCommand
As an object:
public Command BuildCommand
I have the following:
public ...
1
vote
0
answers
81
views
async Task<T>() not returning straight away when not using await
If I have the following function
public async Task<bool> Foo()
{
// call many async functions
await Bar1().ConfigureAwait(false);
await Bar2().ConfigureAwait(false);
return await Bar3()....
0
votes
1
answer
1k
views
Alternative to ConfigureAwait
I have some C# code that Im actually porting to VB.Net. Now I noticed ConfigureAwait(false) is used everywhere. A requirement is that the code be dependant on .Net 4.0
As you might know... ...
5
votes
2
answers
5k
views
Is ConfigureAwait(true) always get back to the orignial thread, even when the callee method does ConfigureAwait(false) internally? [duplicate]
I was expecting to get back to Thread#1 at location 1.2, but I did not. Is there a way to get back to UI thread after making the async call? Thanks
Also I cannot make the top level method async. Not ...