gpt4 book ai didi

c# - mvc6中app、服务和中间件的区别

转载 作者:IT王子 更新时间:2023-10-29 04:43:29 24 4
gpt4 key购买 nike

我试图理解 MVC6 中中间件的概念。这对我来说仍然有点模糊。我真的看不出您在 Startup 类中获得的几个“标准”变量之间的区别。

据我所知,有 3 种不同的方法可以告诉应用程序它应该使用特定的中间件?

你可以通过服务调用中间件使用。但这似乎只是为了“添加”中间件?

services.AddMvc();

// Add other services
services.AddScoped<IMyCountriesRepository, MyCountriesRepository>();
services.AddScoped<IEmailer, Emailer>();

然后你就有了 IApplicationBuilder 应用。这是要实际使用服务中加载的中间件?所以你可以这样调用它:

app.UseMvc();
app.UseErrorPage(...);
app.UseIdentity(); // cookie authentication

然后有一种方法可以像这样加载和使用中间件:

app.UseMiddleware<MyCustomMiddleware>();

三种类型的注册/使用中间件有什么好处?它们之间的确切区别是什么?

最佳答案

我会区分添加服务和添加中间件。

添加服务

这基本上是将您的功能所需的类注册到 ASP .Net 5 中构建的依赖项注入(inject)容器中。(IServiceCollection 接口(interface))

您可以做的最简单的事情就是手动将它们一一添加,如下所示:

services.AddScoped<IMyCountriesRepository, MyCountriesRepository>();
services.AddScoped<IEmailer, Emailer>();

如果您要构建更复杂的应用程序或自包含框架,您可能需要创建一个函数来注册所需的所有服务。一个好的方法是创建一个扩展方法:

public static void AddMyServices(this IServiceCollection services)
{
services.AddScoped<IMyCountriesRepository, MyCountriesRepository>();
services.AddScoped<IEmailer, Emailer>();
...
}

//register all your services just by running the ext method:
services.AddMyServices();

这正是services.AddMvc();正在做。

In a more flexible way as it allows you to pass a lambda to further customize default services like the model binders (Like services.AddMvc(opts => opts.ModelBinders ...)) and is returning an IMvcBuilder you can use to further customize it things like the view engines (Like services.AddMvc().AddViewOptions(opts => opts.ViewEngines ...)).

添加中间件

ASP .Net 5 不是基于 HTTP 模块和处理程序,而是基于中间件的 OWIN 思想。有一个 nice blog entryAndrei Dzimchuk 描述的中间件很好地总结了它:

Middleware – Pass through components that form a pipeline between a server and application to inspect, route, or modify request and response messages for a specific purpose.

And this definition applies to ASP.NET 5 as well. Middleware can be thought of as both HTTP modules and handlers that we've had in classic ASP.NET. Some middleware would implement various intermediate tasks when processing requests such as authentication, session state retrieval and persistence, logging and so on. Some of them would be the ultimate request handlers that would produce responses.

所以现在您想将自己的行为添加到 ASP 管道中。

最简单的就是定义一个内联中间件:

app.Use(async (context, next) =>
{
//do something before passing the request to the next middleware
await next.Invoke();
});

您也可以create your own middleware class并注册:

app.UseMiddleware<MyMiddleware>();

最后,您可以再次定义扩展方法来封装复杂的设置逻辑。

This is what app.UseMvc() does. It lets you define your routes and it is then adding the routing middleware by calling app.UseRouter(). As you can see, the implementation of app.UseRouter adds the RouterMiddleware into the pipeline with the call to builder.UseMiddleware<RouterMiddleware>(router);

您的中间件所需的任何服务之前都已注册。这意味着它们将通过内置的 DI 容器供您的中间件使用。


最终结果是该框架使您可以更轻松地混合和匹配应用程序所需的组件(服务)和行为(中间件),仅包括您需要的部分。

关于c# - mvc6中app、服务和中间件的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33126665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com