Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net Core - do not break on await next.Invoke() ("green" breaks)

Recently I switched to ASP.NET Core and mainly I love it!

There is one annoying part though - when I have an Exception this bubbles up through all the parts of await next.Invoke() in my whole application. That means every custom Middleware or filters that use async/await.

This means I have to press continue / F5 about 8 times every time an Exception occurs. Especially while working on tricky code this is super annoying and a big waste of time and mental energy.

See this example below:

Annoying breaks

What I tried:

  • enabled Just my Code - does not solve - as this is happening in my code.
  • disable this type of exception in the Exception Settings - this does not solve my problem, because the first (yellow) I actually need.
  • fill my whole application with [DebuggerNonUserCode] - also something that I don't like to do - as there might be legit exceptions not related to some deeper child exceptions.
  • see for more information this question

Questions:

  • As Visual Studio seems to be able to differentiate between these two Exceptions (yellow and green) - is it possible to not break at all at the "green" Exceptions?
  • How is everyone else handling this? Or do most people not have 5+ await next.Invoke() in their code?
  • Any other workarounds?

UPDATE The workaround of @MichelleWang works with specific cases, but it has a lot of configuration and maintenance if you work with a lot of different or complicated projects. Usually the async / awaits are scattered over a lot of different classes. Some filters, some base projectcode, some domain code, etc.

In a way VS already distinguishes between these two types of breaks - how to just break on yellow in general?

UPDATE

Found an existing feature request - please help and upvote there:

https://developercommunity.visualstudio.com/content/idea/739876/exception-dialog-pops-up-multiple-times-for-same-e.html

like image 871
Dirk Boer Avatar asked Jul 02 '20 21:07

Dirk Boer


People also ask

What is async and await in ASP NET Core?

All of these leads to improved scalability of our application. These two keywords – async and await – play a key role in asynchronous programming in ASP.NET Core. We use the async keyword in the method declaration and its purpose is to enable the await keyword within that method.

What is the use of await keyword in ASP NET Core?

These two keywords – async and await – play a key role in asynchronous programming in ASP.NET Core. We use the async keyword in the method declaration and its purpose is to enable the await keyword within that method. So yes, you can’t use the await keyword without previously adding the async keyword in the method declaration.

Why doesn't ASP NET Core just work?

The one thing that didn't 'just work' was the async requirement of ASP.NET Core's output generation. The old code - which originally was written for .NET 1.1 and then moved through generations of .NET through 4.5+ - didn't use async output, so all the code at the middleware API 'seam' needed to be changed to async.

Does await call work without await in NET Core?

This was made even more insidious by the fact that the original code in .NET Core 2.1 worked just fine even without the await call, meaning this error cropped up a year or so after I had originally ported the code and had been running it successfully for that long.


1 Answers

Add a Module Name condition in the Exception Settings window so you won’t break on any exceptions thrown from others.

  • Open Debug->Windows->Exception Settings
  • Click on an exception type or category
  • Right click on that exception and choose “Edit Conditions” Or click the blue edit pencil in the toolbar

enter image description here

In my case, I set Module Name = "HomeController.cs" to enable exceptions here. enter image description here

enter image description here

Screenshot of Test

It will break here and press F5 to end debug as we expect. enter image description here

When you remove conditions of exception setting, it will recover as you. enter image description here


The microsoft doc about conditions to an exception

Break on Exceptions Thrown only from Specific Modules in Visual Studio 2017

like image 113
Michael Wang Avatar answered Sep 30 '22 18:09

Michael Wang