Исключение из EF, не перехваченное промежуточным программным обеспечениемC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Исключение из EF, не перехваченное промежуточным программным обеспечением

Сообщение Anonymous »


I have a global middleware handling the exceptions thrown by the application. It works as supposed to except for operations failing at the DB level. When I have a problem that Entity Framework reports (due to flawed config in Context definition or other DB related operations), the exception won't report, not even when I step through the execution during debugging.

Instead, this (to me confusing and frustrating) phenomenon arises. Executing the (correct and now working) code below, simply leaves the service, moving the yellow arrow to the controller's return statement and then exists the request reporting 200 OK to Swagger (although with no data).

Thing[] output = Context.Things .Include(a => a.SubThings) .Where(a => a.Id == id) .ToArray(); Please note that the sample above does work and there's no issue unless I make a mistake elsewhere. For instance, I made a boo-boo changing the domain model's List SubThings {get;set;}=[] to IEnumerable SubThings {get;set;}=[] (which eliminates the capacity of adding the elements to the set) and SubThing[] SubThings {get;set;} (isn't allowed for navigation collections). (NB. I know why those errors occur and I'm not asking how to resolve them. It's just background info for the question.)

First after (too extensive) debugging, I discovered the issue. Then, surrounding the failing operation by try-catch, exposed me to the error.

try { Thing[] output = Context.Things .Include(a => a.SubThings) .Where(a => a.Id == id) .ToArray(); } catch(Exception exception) { ... } It confuses me and I feel I'm missing something. My impression was that the middleware (registered as the outermost scope) will intercept everything. The failing operation is performed within the request, hence passing a bunch of others, "inner'er" middlewares. From my perspective, it looks like EF expcetions get a fast lane out of the application.

Googling the issue led to info on how to set up the middleware and how to correct incorrectly configured ones. I've checked them all just in case but nowhere did I notice any info other than confirmation that I followed the standard procedures.

It's also weird that Swagger reports 200 OK instead of 500 Internal Server Error on this

catch(Exception exception) { throw; } but handles perfectly well this

catch(Exception exception) { throw new DedicatedCustomException(); } However, my question is specifically if I actually must use the explicit try-catch strategy instead of having an implicit error management for all bad things. It has an impact on the code base size if I have to repeat the try-catch explicitly in every call to DB.


Источник: https://stackoverflow.com/questions/780 ... middleware
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»