在MVC5应用程序中使用OWIN包的好处 [英] Benefits of using OWIN packages in MVC5 application

查看:196
本文介绍了在MVC5应用程序中使用OWIN包的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解OWIN和Katana ..确定,应用程序可以自托管或可以在Nancy或非IIS上托管,这很酷。这个问题的原因是我们想要使用MVC5(VS 2013)创建一个Web应用程序,它将在Windows Azure中的IIS上托管。

I am trying to understand OWIN and Katana.. sure, it's cool that the application can be self-hosted or can be hosted on Nancy or on non-IIS. The reason for this question is we want to create a web application using MVC5 (VS 2013), which will be hosted on IIS in Windows Azure.

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

However, we received a recommendation to use OWIN middleware components/packages in mvc5 application to get the benefits of pluggable architecture, performance, etc.

我想了解如何获得性能提升如果我们在MVC5应用程序中使用OWIN中间件,它将在Windows Azure中的IIS上托管。我的应用程序是否会通过使用owin中间件包在IIS管道中跳过很多不必要的东西?当它在IIS上托管时,我可以通过在MVC5中使用OWIN获得任何其他好处吗?

I would like to understand how there will be a performance gain if we use OWIN middleware in MVC5 application, which will be hosted on IIS in Windows Azure. Will my application skip alot of unnecessary things in the IIS pipe by using owin middleware packages? Are there any other benefits that I can get by using OWIN in MVC5, when it is hosted on IIS?

推荐答案

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

Yes, you will potentially be able to skip a lot of unnecessary things in the pipeline, as you will define the components in the pipeline, along with using other components not necessarily made by you, that your application will use. These components are middleware, because they sit in the middle of the processing pipeline and the components can decide to pass control on to the next component in the pipeline via the async/await C# syntax or end the processing at that component.

AppFunc 对象是Katana中神奇发生的地方,因为它是组件在调用时使用的逻辑,所以签名是:

The AppFunc object is where the "magic" happens in Katana, because it is the logic that a component uses when it is invoked, the signature is this:

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








注意: IDictionary< string,object> 表示环境值(例如 Request Response ;想想 ASP.NET中的HttpContext ,OWIN标准定义了这个词典中必须存在的某些值,例如owin .RequestBodyowin.ResponseBody。 Katana是微软对OWIN标准的实现,因此可以开箱即用这些和其他字典项目。

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,object>,任务> ,如下所示:


An example of a component would be a method that matches the signature of AppFunc (which is Func<IDictionary<string, object>, Task>, like this:

public async Task Invoke(IDictionary<string, object> environment)
{
    // Do processing...

    // Wait for next component to complete
    await _nextComponent(environment);

    // Do more processing...
}








注意:OWIN希望该方法返回任务或生成异常,所以返回null; 将无效。

Note: OWIN expects the method to return a Task or generate an exception, so return null; would be invalid.






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


So how do you know what the next component will be?

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

The constructor of your component needs to accept a parameter of Func<IDictionary<string, object>, Task>, like this:

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);
    }
}

这篇关于在MVC5应用程序中使用OWIN包的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆