6

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.

enter image description here

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?

VivekDev
  • 20,868
  • 27
  • 132
  • 202
  • http://www.asp.net/aspnet/overview/owin-and-katana/owin-startup-class-detection –  Sep 23 '14 at 05:24

1 Answers1

4

The secret is that Katana uses an ASP.NET feature called PreAppStart. You can see the source codes here:

https://katanaproject.codeplex.com/SourceControl/latest#src/Microsoft.Owin.Host.SystemWeb/PreApplicationStart.cs

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).

Eilon
  • 25,582
  • 3
  • 84
  • 102
  • Hi Eilon, I think I begin to understand. But now the question is what is causing the code you mentioned(static method in PreApplicationStart class) to execute. Even before that what is causing the that assembly to load? As I mentioned except for project references I have not done any thing to my project. – VivekDev Sep 23 '14 at 06:34
  • @DumbDev see http://stackoverflow.com/questions/5955866/when-does-preapplicationstartmethod-actually-get-triggered-to-run ? –  Sep 23 '14 at 07:13
  • Thanks Andreas. Reasonably complicated. – VivekDev Sep 23 '14 at 07:41
  • 1
    I just realized this is same as "What is the process that makes IIS start responding to requests through the Owin pipeline?" (http://stackoverflow.com/questions/25294905/what-is-the-process-that-makes-iis-start-responding-to-requests-through-the-owin) – VivekDev Sep 24 '14 at 15:01
  • @DumbDev it's indeed basically the same question and answer. – Eilon Sep 24 '14 at 16:01