This blog describes how to resolve the error: "EndpointRoutingMiddleware matches endpoints setup by EndpointMiddleware and so must be added to the request execution pipeline before EndpointMiddleware" after upgrading ASP.NET Core project framework from 2.1 to 3.1. Additionally, the blog will explain what we have to change on implementing SignalR in ASP.NET Core 3.1 as well as best practices for up-gradation to the ASP.NET Core 3.1.
I was upgrading the project framework from ASP.NET Core 2.1 solution to ASP.NET Core 3.1 and got an error as shown below after upgradation.
Error Details -
“System.InvalidOperationException HResult=0x80131509 Message=EndpointRoutingMiddleware matches endpoints setup
by EndpointMiddleware and so must be added to the request execution pipeline before EndpointMiddleware. Please add EndpointRoutingMiddleware b
y calling ‘IApplicationBuilder.UseRouting’ inside the call to ‘Configure(…)’ in the application startup code. Source=Microsoft.AspNetCore.Routing StackTrace:
at Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.
VerifyEndpointRoutingMiddlewareIsRegistered(IApplicationBuilder app, DefaultEndpointRouteBuilder& endpointRouteBuilder)
at Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseEndpoints(IApplicationBuilder builder, Action`1 configure)
at Stratis.FederatedSidechains.AdminDashboard.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in Location\ProjectName\Startup.cs:line 92
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)”
Solution
We have to use app.UseRouting(); inside the Configuration() method of Startup.cs file.
app.UseRouting();//error message suggested to implement this
Additionally, I got information or warning messages like this for SignalR,
In ASP.NET Core 2.1 application, SignalR is implemented as shown below which resides inside the Configuration() method of Startup.cs file.
app.UseSignalR(routes =>
{
routes.MapHub<ChatUpdaterHub>("/chat-updater");
});
However, in ASP.NET Core 3.1 it should be implemented with endpoints as illustrated in the below sample code.
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatUpdaterHub>("/chat-updater");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Furthermore, recommendation for migration to .NET Core 3.1 is,
- Add UseRouting()
- Sequence of UseStatisFiles(), UseRouting(), UseAuthentication() and UseAuthorization(), UseCors() and UseEndPoints() should be in following order
I hope, this helps to resolve the mentioned error and provides you insights for handling the Startup.cs class while upgrading your project framework from ASP.NET Core 2.1 to ASP.NET Core 3.1.