So about a day or so ago my blog started failing when navigating to individual blog posts, this was a full three week after my upgrade to .NET Core 3 so I was adequately surprised. After checking the logs this is what I found (important stuff bolded):

2019-12-17 20:21:15.761 +00:00 [Error] Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware: An unhandled exception has occurred while executing the request.

System.InvalidOperationException: Endpoint DasBlog.Web.Controllers.BlogPostController.Post (DasBlog.Web) contains authorization metadata, but a middleware was not found that supports authorization.

Configure your application startup by adding app.UseAuthorization() inside the call to Configure(..) in the application startup code. The call to app.UseAuthorization() must appear between app.UseRouting() and app.UseEndpoints(...).

I am kind of aware of this issue, it is necessary to add UseAuthorization() in Startup.cs in order to migrate from .NET 2.x to .NET 3.0, but I had updated and deployed that particular fix weeks ago.

So what was the issue? As indicated by the the last error UseAuthorization() must appear between UseRouting() and UseEndpoints(). This a basic concept of middleware, the order is critically important. After opening my project in Visual Studio was smart enough to actually indicate the problem and propose a fix:

The call to UseAuthorization should appear between app.UseRouting() and app.UseEndpoints(..) for authorization to be correctly evaluated.

Ok, so this was obviously wrong from my stand point, the only question is why did this ever work? My hypothesis is that the recent rollout of the .NET Core 3.1 SDK to Azure may have finally exacerbated this problem, well at least that is my best guess.

Comment Section

Comments are closed.