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();
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.
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 serivcesbuilder.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.