Anatomy of an OWIN Startup

2020-03-19 07:15发布

问题:

What are all the hooks on the OWIN Startup class? Information on these is scarce.

For example, one required hook on every Startup class is that it should have a Configuration method. This information can be gathered from the Microsoft documentation.

class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        ...
    }
}

What is the rationale behind not having an IOwinStartup interface or OwinStartup base class in the framework?

interface IOwinStartup
{
    void Configuration(IAppBuilder appBuilder);
}

How do I perform cleanup for my OWIN-based application? Does OWIN detect a Dispose method on the Startup class, similar to how it detects a Configuration method?

After a lot of searching I found this related question: In self-hosted OWIN Web API, how to run code at shutdown? It's not clear how the peopled who answered that question arrived at the necessary information. Am I missing critical documentation or are these details of the OWIN Startup class as elusive as they seem?

回答1:

It's not so much a "hook" as it is a convention. There is a good article on this here:

http://www.asp.net/aspnet/overview/owin-and-katana/owin-startup-class-detection

As for why there is no interface, most likely it's because there was no need to lock it down to that level. This is largely reflection based, and you can specify the class and method to use for startup by various configuration parameters.

In the case of WebAPI in the example you link to, you can do so in the WebApp.Start method, and specify StartOptions with the name of the method to use, but the convention is Configuration.

Cleanup can be accomplished by getting the cancelation token. This information is in the documentation, which is linked from the examples you show. I'm not sure I understand how you arrive at the conclusion that the documentation is missing when it's clearly not.

http://msdn.microsoft.com/en-us/library/microsoft.owin.builderproperties.appproperties.onappdisposing(v=vs.113).aspx

Certainly, it's missing elaboration and examples... but there are a lot of blog entries about this stuff...

You may also want to read the OWIN specification:

http://owin.org/spec/spec/owin-1.0.0.html