简体   繁体   English

.NET 6 中间件执行顺序是否可以指定在 IIS 生命周期中更早运行

[英]Can a NET 6 Middleware execution order be specified to run earlier in IIS lifecycle

I have a.NET 6 web app that is running on an IIS server.我有一个在 IIS 服务器上运行的 .NET 6 web 应用程序。 The server is configured to use IIS HTTPModules which perform logic for every application on the site and hook in the typical events such as BEGIN_REQUEST() and POST_AUTHENTICATE().服务器配置为使用 IIS HTTPModules,它为网站上的每个应用程序执行逻辑,并挂接典型事件,例如 BEGIN_REQUEST() 和 POST_AUTHENTICATE()。

I am aware that.NET 6 does not use that same event handle pattern and, in our scenario the IIS pipeline/lifecycle runs separately than the.NET 6 pipeline/lifecycle.我知道 .NET 6 不使用相同的事件句柄模式,在我们的场景中,IIS 管道/生命周期与 .NET 6 管道/生命周期分开运行。

I have read through the middleware documentation and have implemented previous middleware components.我已经阅读了中间件文档并实现了以前的中间件组件。 I am trying to understand where in the classic ASP.NET pipeline the.NET 6 Middleware kicks in. From what I can tell the.NET 6 middleware execution order occurs sometime after the POST_AUTHENTICATE which is too late for my logic.我试图了解经典 ASP.NET 管道中 .NET 6 中间件的位置。据我所知,.NET 6 中间件执行顺序发生在 POST_AUTHENTICATE 之后的某个时间,这对我的逻辑来说为时已晚。

Can a middleware component can be ordered and injected into the HTTP request at the level of the BEGIN_REQUEST or at least prior to POST_AUTHENTICATE?能否在 BEGIN_REQUEST 级别或至少在 POST_AUTHENTICATE 之前订购中间件组件并将其注入到 HTTP 请求中?

What prevents you from changing the execution order is that ASP.NET Core module hooks itself to IIS RQ_EXECUTE_REQUEST_HANDLER event, as showed in source code .阻止您更改执行顺序的是 ASP.NET 核心模块将自身挂接到 IIS RQ_EXECUTE_REQUEST_HANDLER事件,如源代码所示。 ASP.NET Core runtime is then initialized by this module, and all middleware code executes afterward. ASP.NET 然后由该模块初始化核心运行时,然后执行所有中间件代码。 So for you, this can be not early enough.所以对你来说,这可能还不够早。

You can read IIS reference 1/2 to learn more about the events.您可以阅读 IIS 参考资料 1/2 以了解有关事件的更多信息。

However, the decision to hook to this event was made due to a variety of reasons, both feature-wide and compatibility-wide.但是,挂钩此事件的决定是出于多种原因,包括功能范围和兼容性范围。

So, in your case you should be able to choose from the options below,因此,在您的情况下,您应该能够从以下选项中进行选择,

  • Move your logic to a .NET Framework 4.x module or a native IIS module so that this custom module hooks to an earlier event.将您的逻辑移动到 .NET Framework 4.x 模块或本机 IIS 模块,以便此自定义模块挂钩到更早的事件。

    You can share data if needed between the custom module and your ASP.NET Core middleware, though clearly that boundary crossing isn't easy.如果需要,您可以在自定义模块和您的 ASP.NET 核心中间件之间共享数据,但显然跨越边界并不容易。

  • Build your own ASP.NET Core module to hook to whatever event you like, by modifying the source code.通过修改源代码构建您自己的 ASP.NET 核心模块以挂钩您喜欢的任何事件。

    But this is a huge decision to make because on that path you need to maintain a fork and track actively on security patches.但这是一个巨大的决定,因为在这条路上你需要维护一个分支并积极跟踪安全补丁。 Moving the event hook can also break some ASP.NET Core features as they might not be designed to run early.移动事件挂钩也可能破坏某些 ASP.NET 核心功能,因为它们可能未设计为提前运行。

References参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM