- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我试图理解 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 (Likeservices.AddMvc().AddViewOptions(opts => opts.ViewEngines ...)
).
- Check the implementation of
MvcServiceCollectionExtensions
andMvcCoreServiceCollectionExtensions
添加中间件
ASP .Net 5 不是基于 HTTP 模块和处理程序,而是基于中间件的 OWIN 思想。有一个 nice blog entry由 Andrei 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 callingapp.UseRouter()
. As you can see, the implementation ofapp.UseRouter
adds theRouterMiddleware
into the pipeline with the call tobuilder.UseMiddleware<RouterMiddleware>(router);
您的中间件所需的任何服务之前都已注册。这意味着它们将通过内置的 DI 容器供您的中间件使用。
最终结果是该框架使您可以更轻松地混合和匹配应用程序所需的组件(服务)和行为(中间件),仅包括您需要的部分。
关于c# - mvc6中app、服务和中间件的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33126665/
如果我调用一个应用程序两次或多次,但只有一个实例应该运行(这是所需的),我就会遇到一个问题。 首先一些(可能必要的)背景信息: 使用 MAC OS X El Capitan (10.11.6) 我有一
覆盖文件。覆盖 Apps 脚本文件。 这是不是 创建一个新的 Apps 脚本文件的问题。那对我没有帮助。我需要更新 现有的 Apps 脚本文件。这个问题类似于创建一个新文件,但不是同一个问题。更新的语
我是 Apps 脚本的新手,正在尝试了解使用另一个帐户在一个帐户中运行/触发脚本的基础知识。需要注意的是:我想在访问脚本的用户而不是拥有脚本的用户的情况下运行脚本——以便将运行时间分配给访问的用户。
我是 Apps 脚本的新手,正在尝试了解使用另一个帐户在一个帐户中运行/触发脚本的基础知识。需要注意的是:我想在访问脚本的用户而不是拥有脚本的用户的情况下运行脚本——以便将运行时间分配给访问的用户。
我有一个安卓应用程序。我想为我的应用程序实现 App Indexing。 我已经点击了 Google 开发者链接 https://developers.google.com/app-indexing/
有什么区别: import App from '../components/App'; 和 var App = require('../components/App'); 两者都用于获取组件,但它没有
问题: 我有一个使用 requireJS 的简单演示应用程序。当require.min.js脚本加载时,它尝试加载入口点脚本。但是,问题是,而不是 localhost:8090/js/app.js它尝
我正在构建一个 React Native 应用程序,目前正在尝试通过 Firebase Auth 实现一个身份验证注册系统。我已经按照指南/网站上的文档来设置 Firebase 配置文件。我运行该应用
因此 app.yaml 文件的一部分如下所示(至少在 GAE 教程中): handlers: - url: /.* script: main.app 但是,我也看到它看起来像这样: handler
我是Android App开发的新手。当我尝试创建一个新项目Android Project时,弹出以下消息: Information:Gradle tasks [:app:generateDebugS
我正在编写一个应用程序脚本(用于处理电子邮件、任务和日历事件)并希望将其部署为网络应用程序。 该应用程序将在运行它的用户的上下文中运行。该应用程序将被超过 10k+ 的用户使用,甚至可能更多。 在将其
我需要实现一个用于登录网站的 Google Apps 脚本应用,然后如果该网站上的身份验证过程成功,用户应该会在 google 脚本边栏中收到一条消息。 例如:用户输入他的邮箱和密码,然后他点击登录按
我正在开发一个跨平台应用程序,它将在 Google Play 商店和 App Store 上发布。 Google Play 政策以及 App Store 政策规定,您不能使用其他支付系统购买将在应用程
我的 AppEngine 应用程序在我的台式机上的开发服务器上运行良好,但我无法在 Google 服务器上获取版本以关注我的源代码更新。 这是最有说服力的例证。我的 app.yaml 文件开始于: a
我像这样将所有内容重定向到我的 app.yaml 中的单个文件 - url: /.* script: frontcontroller.application 但我仍然必须使用 robots.txt
我想构建一个基于 App Engine 的网络应用程序,并使用 Google 帐户对用户进行身份验证。我需要来自多个域的用户可以登录。从我读到的内容看来,仅使用 Google Accounts API
我无法将我的域指向我使用 Google App Engine 托管的网站。这是背景……注意区分“google apps”(域托管、电子邮件等)和“google app engine”(网站框架)的概念
是否可以通过 App Engine 上内置的 OpenId 实现单点登录?我一直在尝试集成一个 Marketplace 应用程序,并让用户在来自 Google Apps(管理面板或通用导航)时登录。我
有没有办法从 azure-cli 为 Web 应用或函数应用创建和/或激活 App Insights? 现在浏览文档。 最佳答案 我之前也考虑过你的问题。要创建应用程序洞察力,az resource
我在以 Angular 创建新项目时遇到问题。当我运行 ng new myapp 命令时,我得到以下命令 ng 新问候语 Error: Path "/app/app.module.ts" does n
我是一名优秀的程序员,十分优秀!