ASP.NET Core multiple types of web convergence cases

67 views Asked by At

I have created an ASP.NET Core MVC project, but I want to add support of Web API and SignalR. I know that both Web API requests and MVC requests go through a unified request pipeline. What I want to ask is if I configure the request pipeline according to the MVC pattern and add a lot of middleware, when Web API requests are processed through a lot of middleware, will it consume a lot of unnecessary performance?

If it is really a waste of performance, then is there a reasonable request pipeline configuration scheme? Let Web API request only through the needed middleware, I know you can use app.UseWhen() method, but I want to learn how to configure the following mature projects, awesome you can share experience or similar project templates.

Thank you guys!

public static void UseMyEndpoints(this IApplicationBuilder application)
{
     application.UseEndpoints(endpoints =>
     {
         // SignalR                
         endpoints.MapHub<...>(...);
         
        // MVC  
         EngineContext.Current.Resolve<IRoutePublisher>().RegisterRoutes(endpoints);
         
         // WebApi
         endpoints.MapControllers();
     });
}

app.UseMyProxy();
app.UseLinqQueryCache();
app.UseMyResponseCompression();
app.UseMyWebOptimizer();
app.UseMyStaticFiles();
app.UseMyWEbMarkupMin();
app.UseKeepAlive();
app.UseSession();
app.UseMyRequestLocalization();
app.UseMyRouting();
app.UseMyCors();
app.UseMyAuthentication();
app.UseAuthorization()
app.UseMyEndpoints();
2

There are 2 answers

3
Tiny Wang On BEST ANSWER

MVC application already support web API, in your controller, you can create action to return string/json/model, you can also right click on the Controllers folder to add API controller template. Then you will have both API controllers and MVC controllers in your app at the same time and they won't influence with each other.

enter image description here

MVC(public class MyMvcsController : Controller -> public abstract class Controller : ControllerBase -> implement Controller class which is also implementing ControllerBase class) and API(public class MyApisController : ControllerBase -> Implement ControllerBase class) are all used to handle requests dispatched by the routing middleware. When http requests with different routes reaching your APP, the route middleware will dispatch them to the corresponding controller action methods to work with the request. Then the action methods defines what the request will receive.

If you want to integrate signlar, following this tutorial. It shows you how to integrate Signalr into a web app, but it's the same as MVC app. You need to install required library signalr.js, add serivces builder.Services.AddSignalR(); and create Hub which handle the websocket connection events.

Signalr is using websocket connections to transfer data which is different with the http requests, websocket connections are long-running connections so that they go through Hubs -> app.MapHub<ChatHub>("/chatHub");. Therefore MVC app can also integrate Signalr at the same time. By the way, it should be a normal scenario when we want to add real-time chat/announcement feature in our MVC app.

Then about the performance what you mentioned. May I know that does your application face performance issue now? Per my understanding, if you defined several custom middleware which cost much time to handle and pass the request, that might bring performance issue, otherwize the middleware shouldn't be the performance issue and we don't require handle it by ourselves. It sounds like "how to solve an issue which doesn't exist?" question. Performance issue is a general concept and normally speaking, I always face performance brought by bad SQL but haven't met from middleware. If you debug you app and found some specific middleware brings performance issue, then let's try to improve it based on the real reason. Middleware like routing, authentication, authorization, static file, https redirecting, etc. won't bring trouble in performance, we might say it cost 0.0001 more second to go through the app.UseHttpsRedirection(); if we have it in our app, but it doesn't make sense.

1
Mohsen Miri On

About your question, it is YES. When configuring your ASP.NET Core application's request pipeline, it's essential to consider performance implications, especially when dealing with multiple middleware components. Adding unnecessary middleware can indeed affect performance, as each middleware component introduces some overhead in processing the request. And about your desired code, you can use this template which works for me:

public static void UseMyEndpoints(this IApplicationBuilder application)
{
  application.UseRouting(); // Enable endpoint routing

  application.UseEndpoints(endpoints =>
  {
     // SignalR
     endpoints.MapHub<...>(...);

     // MVC
     endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");

    // Web API
    endpoints.MapControllers();
});
}
  • UseRouting() enables endpoint routing, which is necessary for both MVC and Web API routing.

  • For MVC, you can use MapControllerRoute to define conventional MVC routes.

  • For Web API, MapControllers() maps attribute-routed controllers.