50

This code was generated for me after added entity framework code-first for SQL Server CE using NuGet. They did no changes to any other file. The file SQLCEEntityFramework.cs was created and placed in App_Start folder.

Does this mean it automatically gets executed or something? The same thing happened when I added Ninject for MVC 3. No code was added to the global.ascx file so I have no idea if its plug and play or I have to configure something.

[assembly: WebActivator.PreApplicationStartMethod(typeof(StackTorrents.WebUI.App_Start.SQLCEEntityFramework), "Start")]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
  • This link : ([Define an initialization order of WebActivator.PreApplicationStartMethod classes](http://stackoverflow.com/a/12590328/1997229)) may help whom the order of execution is important for them. – Iman Mahmoudinasab Oct 28 '13 at 07:10

2 Answers2

44

According to:

http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx

This new attribute allows you to have code run way early in the ASP.NET pipeline as an application starts up. I mean way early, even before Application_Start. This happens to also be before code in your App_code folder (assuming you have any code in there) has been compiled. To use this attribute, create a class library and add this attribute as an assembly level attribute. A common place to add this would be in the AssemblyInfo.cs class within the Properties folder.

John Farrell
  • 24,673
  • 10
  • 77
  • 110
  • 3
    The PreApplicationStartMethodAttribute from the .NET framework allows only one per assembly. Most importantly, WebActivator provides it's own implementation of the attribute that permits multiple instances of per assembly. – Ed Chapel Apr 28 '11 at 04:39
  • @Ed Chapel - does this mean that when I imported SQLCE4 from nuget and also ninject, both has a `PreApplicationStartMethod`. Only a random one will be executed? – Shawn Mclean May 03 '11 at 15:10
  • I don't know about SQLCE4, but ninject uses WebActivator. I am certain that if you stick to the WebActivator attribute that all of the `PreApplicationStartMethod` attributes will run. – Ed Chapel May 03 '11 at 15:14
  • I'm glad I found this. You can not search for Stack Overflow Exceptions in google any more. – Jordan Jun 26 '15 at 14:36
20

To clarify, it gives you a way of hooking into several application start and application shutdown events WITHOUT having to change any existing code files (previously you had to edit Globals.asax.cs).

This is mostly a big deal when making packages as these events are really useful for bootstrapping Http modules and it is really difficult to write code into existing files.

George Mauer
  • 117,483
  • 131
  • 382
  • 612