20

In a WebAPI application for example, what is the difference between

[assembly: OwinStartup(typeof(MyClass), "MyMethod")]

and

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(MyClass), "MyMethod")]

?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ioan Bucur
  • 557
  • 1
  • 4
  • 13

2 Answers2

40

They both are similar at a high level in the sense that they allow you to carry out initialization of your web application, but they are different in some important ways as below:

  1. Methods targeted by WebActivatorEx.PreApplicationStartMethodAttribute will execute before the Application has started up. This allows you to do things like injecting an HttpModule etc.
  2. Methods targeted by OwinStartupAttribute will execute after Application has initialized. This is because this kind of startup is invoked by OwinHttpModule which in itself is injected in using System.Web.PreApplicationStartMethodAttribute.
  3. Owin startup can be disabled via configuration by using an appsetting within web.config of owin:AutomaticAppStartup
  4. There is also System.Web.PreApplicationStartMethodAttribute which as of .NET 4.5 can be used multiple times within an assembly.

So to summarise, this is the order of execution of methods depending on the attributes used.

  1. System.Web.PreApplicationStartMethodAttribute
  2. WebActivatorEx.PreApplicationStartMethodAttribute
  3. Global.asax (Application_Start method)
  4. OwinStartupAttribute
Gopal Krishnan
  • 968
  • 11
  • 14
5

I found the answer here:

In new SignalR API not using WebActivatorEx anymore. OwinStartup preferred instead of WebActivator.

Community
  • 1
  • 1
Ioan Bucur
  • 557
  • 1
  • 4
  • 13
  • 9
    Not a good answer. Your question was "what is the difference between" and then you don't explain the difference in your answer. – Luke Puplett Sep 25 '15 at 11:07