简体   繁体   中英

What is the process that makes IIS start responding to requests through the Owin pipeline?

If you create an empty ASP.NET Web Application project in Visual Studio 2013 open the package manager console and install-package Microsoft.Owin.Host.SystemWeb

Add a Startup class with a Configuration(IAppBuilder app) method, for example:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.Run(context => context.Response.WriteAsync("hello"));
    }
}

And run, you'll see hello show up in the browser. However, it you look at the project, there's no change to any files, namely to web.config, that indicates that the Owin pipeline is being used. More importantly, if you have the Startup class but don't install the Microsoft.Owin.Host.SystemWeb package the Startup's Configuration method won't ever be run.

I suspect there's a custom module and handler involved in making all this happen but can't find any documentation about it. The only thing that marginally touches this subject that I was able to find was this .

How is it that you can change the way a request is handled just by referencing some dlls?

Starting with ASP.NET 4, you can now define a custom class in your code (referenced DLL or source code), with a particular convention and have it invoked by the ASP.NET system way early in the pipeline.

Just need to mark it with the PreApplicationStartMethodAttribute

The Microsoft.Owin.Host.SystemWeb assembly makes use of this feature and if we reflect on the code, we can see that this startup method registers the Owin Module:

public static class PreApplicationStart
{
    private const string TraceName = "Microsoft.Owin.Host.SystemWeb.PreApplicationStart";

    /// <summary>
    /// Registers the OWIN request processing module.
    /// </summary>
    public static void Initialize()
    {
        try
        {
            if (OwinBuilder.IsAutomaticAppStartupEnabled)
            {
                HttpApplication.RegisterModule(typeof(OwinHttpModule));
            }
        }
        catch (Exception exception1)
        {
            Exception exception = exception1;
            ITrace trace = TraceFactory.Create("Microsoft.Owin.Host.SystemWeb.PreApplicationStart");
            trace.WriteError(Resources.Trace_RegisterModuleException, exception);
            throw;
        }
    }
}

From then on, the OwinHttpModule takes over and goes into the OwinBuilder and OwinAppContext flows, which looks up the Startup class in your assembly to invoke the Configuration method.

Microsoft.Owin.Host.SystemWeb subscribes to the PreApplicationStart event. When this event fires we register an HttpModule which contains all the logic to detect Startup class and build the OWIN pipeline etc.

See OWIN Middleware in the IIS integrated pipeline Although OWIN middleware components (OMCs) are primarily designed to run in a server-agnostic pipeline, it is possible to run an OMC in the IIS integrated pipeline as well well (classic mode is not supported). An OMC can be made to work in the IIS integrated pipeline by installing the following package from the Package Manager Console (PMC): Install-Package Microsoft.Owin.Host.SystemWeb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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