Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 590
Rui Avatar asked Aug 13 '14 19:08

Rui


People also ask

What is OWIN pipeline?

OWIN allows web apps to be decoupled from web servers. It defines a standard way for middleware to be used in a pipeline to handle requests and associated responses. ASP.NET Core applications and middleware can interoperate with OWIN-based applications, servers, and middleware.

Does OWIN use IIS?

OWIN sits between IIS and your application so that you can switch out IIS without rewriting your application.

Where is the application pipeline set for an OWIN application?

For OWIN console applications, the application pipeline built using the startup configuration is set by the order the components are added using the IAppBuilder. Use method. That is, the OWIN pipeline in the Katana runtime processes OMCs in the order they were registered using IAppBuilder.

What is OWIN startup?

Open Web Interface for . NET (OWIN) defines an abstraction between . NET web servers and web applications. By decoupling the web server from the application, OWIN makes it easier to create middleware for . NET web development.


2 Answers

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.

like image 80
Raja Nadar Avatar answered Nov 09 '22 14:11

Raja Nadar


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

like image 23
RickAndMSFT Avatar answered Nov 09 '22 15:11

RickAndMSFT