asp.net – 在MVC5应用程序中使用OWIN软件包的好处

前端之家收集整理的这篇文章主要介绍了asp.net – 在MVC5应用程序中使用OWIN软件包的好处前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在努力了解OWIN和Katana ..可以肯定的是,应用程序可以是自托管的,也可以在Nancy或非IIS上托管.这个问题的原因是我们想使用MVC5(VS 2013)创建一个Web应用程序,该应用程序将在 Windows Azure中的IIS上托管.

但是,我们收到了在mvc5应用程序中使用OWIN中间件组件/包的建议,以获得可插拔架构,性能等的优点.

如果我们在Windows Azure中的IIS上托管的MVC5应用程序中使用OWIN中间件,我想了解如何获得性能提升.我的应用程序会使用owin中间件软件包在IIS管道中跳过许多不必要的事情吗?在IIS上托管的时候,使用OWIN在MVC5中有什么其他好处吗?

解决方法

是的,您可能会跳过很多不必要的事情,因为您将在管道中定义组件,以及使用您的应用程序将使用的不一定由您制定的其他组件.这些组件是中间件,因为它们位于处理流水线的中间,组件可以决定通过async / await C#语法将控制权传递给流水线中的下一个组件,或者结束该组件的处理.

AppGroup对象是Katana发生“魔术”的地方,因为它是调用组件使用的逻辑,签名是这样的:

Func<IDictionary<string,object>,Task>;

Note: The IDictionary<string,object> represents the environment values (such as Request and Response; think HttpContext 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,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,so return null; would be invalid.

那么你怎么知道下一个组件是什么呢?

您的组件的构造函数需要接受Func< IDictionary< string,Task>的参数,如下所示:

public class HelloWorldCOmponent
{
    Func<IDictionary<string,Task> _next;

    public HelloWorldComponent(Func<IDictionary<string,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相关文章