I just started my struggle to understand owin and katana. Following the Asp.Net tutorial
I created a blank asp.net project in VS2013 and added a Nuget Package reference to Microsoft.Owin.Host.SystemWeb
. The project I created is bear blank as shown.
This contains nothing except AssemblyInfo.cs
, Web.config
and packages.config
. Now when I run(F5) this, it says
- No assembly found containing an OwinStartupAttribute.
- No assembly found containing a Startup or [AssemblyName].Startup class. To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config. To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.
Now the question is how come just by adding a Nuget reference to Microsoft.Owin.Host.SystemWeb
, it started to look for something specific to Owin like Startup
class and so on as indicated in the error message?
I mean I ran a different project without that Nuget reference and the error message is totally different. Nothing seems to have changed at least in the two files AssemblyInfo.cs
, Web.config
by adding the Nuget reference. As I understand adding the Nuget added a packages.config file and added some project reference. Also I have compared the project properties for the two projects tab by tab and I did not find any difference!
So I wonder what in the world is causing the Owin project look for a Startup class?
The secret is that Katana uses an ASP.NET feature called PreAppStart. You can see the source codes here:
If an assembly in an ASP.NET app has this assembly-level attribute:
[assembly: PreApplicationStartMethod(typeof(PreApplicationStart), "Initialize")]
Then ASP.NET will automatically run that code as the app starts. This code will run before "user" code will run, before even the Application_Start
event. That's why it's called PreAppStart.
In the case of Katana, this code dynamically registers an ASP.NET HTTP Module (IHttpModule
) that will eventually search for and attempt to call the app's startup/builder class. And if that fails, kablamo!
To disable the automatic behavior, add this line to web.config in the <appSettings>
section:
<add key="owin:AutomaticAppStartup " value="false" />
More information on this behavior can be found on the www.asp.net site: http://www.asp.net/aspnet/overview/owin-and-katana/owin-startup-class-detection (same as the commenter mentioned).