Comments (11)
Why should we? It's not a cure-all, it's a tool for addressing a specific problem that we have not needed thus far.
from aspnetkatana.
Where? The code base is more than 10 lines of codes.
from aspnetkatana.
Almost every await lacks it with very few exceptions.
from aspnetkatana.
My understanding of Awaitable
and ConfigureAwait
is a bit far but I'll trust @Tratcher on this.
If there is a performance problem, it's always possible to run a benchmark and prove that a certain piece of code runs better with ConfigureAwait(false)
.
from aspnetkatana.
Nice SO thread that also talk about it that I found helpful:
http://stackoverflow.com/a/26681648/24975
from aspnetkatana.
Actually it is quite the opposite - every await should have it unless the caller needs marshalling to the original context. Please cosider advise 6 from here https://blogs.msdn.microsoft.com/lucian/2013/11/22/talk-async-best-practices/.
from aspnetkatana.
6. Libraries should consider Task.ConfigureAwait(false). There’s another micro-optimization you can use to reduce your methods’ impact on the UI thread. Consider this if your library routine might be called from the UI thread, and has chatty awaits inside it. This technique is used throughout the .NET Framework because it also avoids deadlock in certain poor-practice user code.
I'm wary of micro-optimization
and the mention of the UI thread. Is there such a thing as UI thread in Katana? I haven't contributed to this project yet but the fact that there is no bug opened for this means that it was probably already evaluated and rejected.
from aspnetkatana.
Indeed, we do not performance micro-optimizations unless something shows up in a performance profile. Feel free to do some profiling and let us know if you see any hot spots.
Closing this issue pending actionable data.
from aspnetkatana.
Hi,
Agree with @letrec, there should be .ConfigureAwait(false)
in the code. A few articles below may help to shed the light on ConfigureAwait and SynchronizationContext.
- https://particular.net/blog/async-await-tips-and-tricks
- https://msdn.microsoft.com/magazine/gg598924.aspx
ConfigureAwait
Microsoft's async/await best practices state that you should use
ConfigureAwait(false)
almost anytime youawait
something. This way, the TaskScheduler knows that it can use any available thread when the method resumes after the await. The only exception to the rule is when execution must continue on a specific thread, such as the UI thread in a client application.When you use
ConfigureAwait
in your code, it often looks something like this, with the.ConfigureAwait(false)
call tacked on to the end of the method that returns a Task:public async Task AlreadyAsyncMethod() { await DoSomethingAsync().ConfigureAwait(false); }The reason
ConfigureAwait
is used here is to enable tasks to continue on any available thread. For more information on why this is important, check out the article Context Matters. Suffice it to say, for library code that doesn't care which thread it executes on, you should useConfigureAwait(false)
every time you useawait
. Eventually, you will find that adding ConfigureAwait all over the place becomes tiresome and is easy to forget.
There are two ways you can deal with this. You can use a Roslyn-based tool to make sure you never forget to add ConfigureAwait, or you can use IL weaving so you don't have to type it manually at all. Either way, you'll come out ahead.
from aspnetkatana.
from aspnetkatana.
That article mention that ConfigureAwait(false) is to be used extensively in GUI but ASP.NET does not have a UI thread per se. So it's invalid.
Aside from performance, ConfigureAwait has another important aspect: It can avoid deadlocks.
Deadlocks will only happen if you cross async code and sync code (eg. Task.Wait(...)
) kind of way. If you do it properly, you don't have 2 threads waiting on each other through the SynchronizationContext
.
This technique is particularly useful if you need to gradually convert an application from synchronous to asynchronous.
Again... not really meaningful.
You should not use ConfigureAwait when you have code after the await in the method that needs the context. For GUI apps, this includes any code that manipulates GUI elements, writes data-bound properties or depends on a GUI-specific type such as Dispatcher/CoreDispatcher. For ASP.NET apps, this includes any code that uses HttpContext.Current or builds an ASP.NET response, including return statements in controller actions.
I do think that this is for ASP.NET proper. Not the OWIN stack. @Tratcher could probably explain to us why it doesn't matter. My gut feeling is that there is no more use of global states stored in static and everything.
To summarize this third guideline, you should use ConfigureAwait when possible. Context-free code has better performance for GUI applications and is a useful technique for avoiding deadlocks when working with a partially async codebase. The exceptions to this guideline are methods that require the context.
This is the only recommendation to explicitly use it everywhere. But that article is from 2011.
I would recommend reading about ASP.NET Core SynchronizationContext.
from aspnetkatana.
Related Issues (20)
- /signin-oidc is not found HOT 7
- 'OwinStartupAttribute' is inaccessible due to its protection level HOT 3
- GetExternalLoginInfoAsync() return null for facebook only HOT 6
- Having vulnerabilities on Microsoft.Owin.Security.OpenIdConnect, 4.2.2 HOT 2
- ClockSkew Ignored by OAuthBearerAuthenticationHandler HOT 2
- Requests are run on IOCP threads by default in OwinHttpListener HOT 5
- Attempt to access method System.Management.Instrumentation.InstrumentedAttribute..ctor(System.String) in violation of security transparency rules failed. HOT 2
- Question: Why does the 4.2.2 package specifically require .NET Framework 4.5? HOT 2
- System.NullReferenceException: Object reference not set to an instance of an object HOT 9
- App redirects to different Auth Type refresh token URL. HOT 4
- Strange behavior on port 50000 for Owin self hosting.... HOT 8
- OpenIdConnectProtocolValidator cannot validate state HOT 4
- Should OIDC middleware be refreshing a `code id_token` when close to expiry? HOT 3
- Session in ASP.NET WebForm is cleared after authenticated by Entra ID with CookieAuthentication and WsFederationAuthentication HOT 2
- Always a object reference error when trying to host a webapi in owin self hosted HOT 3
- owin.RequestId always empty guid string HOT 3
- Microsoft.Owin.Security.WsFederation has a vulnerable Newtonsoft.Json package dependency HOT 10
- how do you remove additional query parameters HOT 1
- Migrate to 1ES pipelines HOT 2
- RedeemCode sends request from server HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aspnetkatana.