I was upgrading project framework from ASP.NET Core 2.1 solution to ASP.NET Core 3.1 and got 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 by 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 message like this for SignalR:

In ASP.NET Core 2.1 application, SignalR is written 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 code.

app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<ChatUpdaterHub>("/chat-updater");
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

Furthermore, Microsoft’s 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.

Hope, this helps to resolve error while upgrading your project framework from ASP.NET Core 2.1 to ASP.NET Core 3.1.

Reference

Microsoft

Leave a Reply

Your email address will not be published. Required fields are marked *