1

Can we use Global.asax file in .NET Class Library projects? Because I have not seen anywhere that Global.asax file can only be used only with Web based applications.

The code of Global.asax file are being complied and being called by .net framework when application starts. I want to use similar kind of functionality with my class library i.e. when my class library is being loaded in the memory the Application_Start of Global.asax file of class library will be called.

I know that this can be used using static constructor but I want to utilize the Global.asax for this.

Any suggestions/comments will be highly appreciated..

Thanks

ivamax9
  • 2,601
  • 24
  • 33
user2147315
  • 23
  • 1
  • 8
  • 2
    Using a `Global.asax` for a class library would be weird. Why do you need initialization code? What are you trying to do? – Yuval Itzchakov Jul 05 '15 at 16:07
  • 1
    class libraries aren't applications that "start", they are simply a single file (.dll) that holds a collection of classes. Normally any initialization code that these classes might need would be done in their constructor. – Claies Jul 05 '15 at 16:13
  • Global.asax isn't some mystery code that is run because it must be run, it is run by IIS because IIS knows that such code is the initialization routine for some service. So you can just as easily apply some add-in(plugin) model with strictly defined initialization, cleanup routines that will be called when you load add-in dlls through appropriate mechanisms. – Eugene Podskal Jul 05 '15 at 16:20
  • Thanks for comments, Yuval. I understands that libraries are not application that's why i used Load not initialized. libraries loads , i think it is similar to as Applications starts. – user2147315 Jul 05 '15 at 16:51
  • So basically global.asax should not be work with class library projects? – user2147315 Jul 05 '15 at 16:54

1 Answers1

7

The global.asax file is a feature of ASP.NET appliations. It is not available in DLLs (class library projects).

There is no standard way (using C#) of running some code on assembly-load, even though the CLR seems to have such a feature. Have a look at this question for some more information and ideas. For example, one answer mentions a tool called InjectModuleInitializer, which is run as a post-build step to inject a module initializer method into a .NET assembly.

Here's a short excerpt from the blog post introducing InjectModuleInitializer:

One feature of the CLR that is not available in C# or VB.NET are module initializers (or module constructors). A module initializer is simply a global function which is named .cctor and marked with the attributes SpecialName and RTSpecialName. It is run when a module (each .NET assembly is comprised of one or more modules, typically just one) is loaded for the first time, and is guaranteed to run before any other code in the module runs, before any type initializers, static constructors or any other initialization code.

Community
  • 1
  • 1
M4N
  • 94,805
  • 45
  • 217
  • 260
  • Even Visual Studio does not allow to add Global.asax file with Class Library type projects. – user2147315 Jul 07 '15 at 07:18
  • @user2147315: If you think the answer was helpful, please mark it as the accepted answer (also with your other questions). Thanks. – M4N Jul 07 '15 at 07:23
  • Yes... I have marked it as answer.. I also wanted to give +1 but I do not have sufficient reputation..:) – user2147315 Jul 07 '15 at 07:29