我正在尝试了解 OWIN 和 Katana。当然,该应用程序可以自托管,也可以托管在 Nancy 或非 IIS 上,这很酷。提出这个问题的原因是我们想要使用 MVC5 (VS 2013) 创建一个 Web 应用程序,该应用程序将托管在 Windows Azure 中的 IIS 上。
但是,我们收到了在 mvc5 应用程序中使用 OWIN 中间件组件/包的建议,以获得可插拔架构、性能等优势。
我想了解如果我们在 MVC5 应用程序中使用 OWIN 中间件(该应用程序将托管在 Windows Azure 中的 IIS 上),将会如何提高性能。我的应用程序是否会通过使用 owin 中间件包跳过 IIS 管道中的许多不必要的东西?当 OWIN 托管在 IIS 上时,通过在 MVC5 中使用 OWIN 可以获得其他好处吗?
最佳答案
是的,您可能能够跳过管道中的许多不必要的事情,因为您将定义管道中的组件,以及使用应用程序将使用的不一定由您创建的其他组件。这些组件是中间件,因为它们位于处理管道的中间,并且组件可以决定通过async/await
将控制权传递给管道中的下一个组件。 C# 语法或结束该组件的处理。
AppFunc
object 是 Katana 中“魔法”发生的地方,因为它是组件调用时使用的逻辑,签名是这样的:
Func<IDictionary<string, object>, Task>;
<小时/>
<小时/>Note: The
IDictionary<string, object>
represents the environment values (such asRequest
andResponse
; thinkHttpContext
in ASP.NET) and the OWIN standard defines certain values that must exist in this dictionary, such as"owin.RequestBody"
or"owin.ResponseBody"
. Katana is Microsoft's implementation of the OWIN standard and as such has these, and other, dictionary items available out-of-the-box.
组件的一个示例是与 AppFunc
签名匹配的方法。 (即 Func<IDictionary<string, object>, Task>
,如下所示:
public async Task Invoke(IDictionary<string, object> environment)
{
// Do processing...
// Wait for next component to complete
await _nextComponent(environment);
// Do more processing...
}
<小时/>
<小时/>Note: OWIN expects the method to return a
Task
or generate an exception, soreturn null;
would be invalid.
那么你怎么知道下一个组件是什么?
组件的构造函数需要接受 Func<IDictionary<string, object>, Task>
参数,像这样:
public class HelloWorldCOmponent
{
Func<IDictionary<string, object>, Task> _next;
public HelloWorldComponent(Func<IDictionary<string, object>, Task> next)
{
_next = next;
}
public async Task Invoke(IDictionary<string, object> environment)
{
// Do something
// Wait for next component to return
await _next(environment);
}
}
关于asp.net - 在 MVC5 应用程序中使用 OWIN 包的好处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19944704/