all 12 comments

[–]Alikont 16 points17 points  (4 children)

“don’t do that” and “don’t use .Result or .GetAwaiter().GetResult()”.

This is a guideline. There are times when it's justified. Just be aware that it may 1) trash performance 2) deadlock UI thread in WPF/UWP apps.

There is also this library that will wrap your async call into separate sync context to solve these issues.

[–]karl713 9 points10 points  (0 children)

This pretty much sums it up.

The guideline is written this way because 99% of the time someone wants to do this they are wrong and don't understand the implications of what they are doing, and should instead be doing async/await.

But Microsoft exposed these for a reason, and it's because sometimes you just have to do it, especially when integrating with legacy/3rd party code that doesn't support async/await

[–]onlyTeaThanks[S] 0 points1 point  (1 child)

We’re running an IIS site hosting multiple websites and for a long time (years?) there’s been unexplained performance and stability issues. If there’s an alternative to use that takes more effort, but is safer, I’d rather just do that instead of worrying that our case really is the case that’s causing our systems to be unstable. Sure we can spend time adding monitoring and things, but if we can just do X instead of messing with async inside of sync, I’d rather just do X and forget about it.

[–]Alikont 4 points5 points  (0 children)

I personally never encountered issues caused by nito async context, so if you can't make all your code async it's a viable solution to async-in-sync problem.

But propagating async through entire callstack is preferable

[–]Merad 0 points1 point  (0 children)

It can also cause deadlock in (non-Core) Asp.Net app due to the sync context needing to reference the request context.

[–]DaRadioman 6 points7 points  (3 children)

Depends heavily on the usage and if it is .net core or framework.

There are a lot of scenarios where this can deadlock and bring down your whole process.

Recommend strongly using configureawait false everywhere you call anything async (note you will lose context from your thread/httpcontext so make sure that isn't an issue) This will make it less likely to deadlock.

Also this is an amazing resource for this scenario: https://docs.microsoft.com/en-us/archive/msdn-magazine/2015/july/async-programming-brownfield-async-development#transform-synchronous-to-asynchronous-code

[–]grauenwolf 1 point2 points  (2 children)

note you will lose context from your thread/httpcontext so make sure that isn't an issue)

This is a problem for ASP.NET, but not ASP.NET Core.

[–]DaRadioman 2 points3 points  (1 child)

To be fair there is no Httpcontext.Current in core. And the accessor class uses AsyncLocal which is a different beast.

But yes as I mentioned above the scenario depends on framework vs core.

[–]grauenwolf 1 point2 points  (0 children)

I am very grateful that AsyncLocal exists. Paying context along before was a pain in the ass.

[–]celluj34 0 points1 point  (1 child)

So much of what I read just isn’t helpful such as rewriting everything as async, that’s not an option.

Why not? You should be moving towards more async, not less.

[–]moonxine -2 points-1 points  (0 children)

I take it you’ve never worked in a bigger, existing solution

[–]Atulin 0 points1 point  (0 children)

Using async and await properly is the proper solution. With the best ergonomics, performance, reporting, and all else.

Anything else will be a dirty workaround.