7

I have an ASP.Net Core 2 web api project. I have added the following to the Configure method in my Startup.cs file app.UseExceptionHandler();

I noticed in my Postman tests that I was getting an "Unable to get response" result. Server-side logging shows that the error has to do with Tables being missing from my Database. Which is fine, I can resolve that. But my question is why would the server not be returning a 500 Internal Server Error? Why is it dying, and returning no response at all to Postman?

So, in my Controller, I purposely throw an Exception to test the handler, and call the URL from Postman, and indeed, I get back a 500 Internal server error response, as expected.

Why are the "deeper down" errors being thrown from EFCore not being handled by the ExceptionHandler middleware, and crashing my app? Am I missing something?

1
  • Depends on where the exception is thrown from. Did you try debugging to see if it happens on the request path? Commented Mar 27, 2018 at 9:23

1 Answer 1

5

In your startup.cs, move the UseMvc() tag to the bottom of the pipeline i.e.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
  ...
  app.UseExceptionHandler();
  app.UseMvcWithDefaultRoute(); 
}

In my case, the request pipeline faulted on startup when the route launched by the browser did not exist. In that scenario, my app.UseExceptionHandler() before the app.UseMvc() was not executed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.