3

I have a class with a method that should run on application startup. I don't want to call this method directly from Application_Start event. What's the best way to get this class instantiated and method run on application_start?

In other words, I want to inject this code into application startup.

user3219798
  • 333
  • 6
  • 14
  • 1
    May I ask why you don't want to call code that needs to run on the application start in Application_Start? – Adrian Toman Apr 26 '14 at 13:03
  • 1
    I have a CMS application and multiple web sites (mvc areas) using this CMS. I don't want to change the application_start of the actual CMS because I can run updates of the CMS without changing areas. So the areas should have some code that register itself on startup – user3219798 Apr 26 '14 at 13:21

3 Answers3

4

I've noticed that some people use WebActivatorEx.PostApplicationStartMethod. I've not delved into the details but it is the first place I would look. Here's an example of a class registered to automatically run when RegisterBundles is called. One of the other hooks may be what you are looking for.

[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(BootstrapBundleConfig), "RegisterBundles")]

namespace Deloitte.EmploymentMemo.Presentation.App_Start
{
    public class BootstrapBundleConfig
    {
        public static void RegisterBundles()
        {
            // Add @Styles.Render("~/Content/bootstrap") in the <head/> of your _Layout.cshtml view
            // For Bootstrap theme add @Styles.Render("~/Content/bootstrap-theme") in the <head/> of your _Layout.cshtml view
            // Add @Scripts.Render("~/bundles/bootstrap") after jQuery in your _Layout.cshtml view
            // When <compilation debug="true" />, MVC4 will render the full readable version. When set to <compilation debug="false" />, the minified version will be rendered automatically
            BundleTable.Bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include("~/Scripts/bootstrap.js"));
            BundleTable.Bundles.Add(new StyleBundle("~/Content/bootstrap").Include("~/Content/bootstrap.css"));
            BundleTable.Bundles.Add(new StyleBundle("~/Content/bootstrap-theme").Include("~/Content/bootstrap-theme.css"));
        }
    }
}
Honorable Chow
  • 3,097
  • 3
  • 22
  • 22
2

One of possible solutions to use OWIN startup.

Install nuget package: install-package Microsoft.Owin.Host.SystemWeb

Add to appsettings startup class:

   <appSettings>
          <add key="owin:appStartup" value="MyProject.Code.Startup" />
   </appSettings>

And by convention you will need class with method called Configuration:

    public class Startup
    {
            public void Configuration(IAppBuilder app)
            {
                app.Run(context =>
                {
                    string t = DateTime.Now.Millisecond.ToString();
                    return context.Response.WriteAsync(t + " Production OWIN App");
                });
            }
    }

Or do anything you need.

If you interesting in it, check it asp.net: OWIN and Katana project

Uriil
  • 11,948
  • 11
  • 47
  • 68
-1

Use delegates. They hold references to methods and that give you possibility to call some methods at one time; An example of using delegates :

public delegate void myDelegate();

private static void Main()
{
    myDelegate myFunctions = Method1; //initialize delegate with Method1
    myFunctions += Method2;           //Add Method2 to delegate

    myFunctions(); //call all methods added to delegate
}

public static void Method1()
{
    Console.WriteLine("Hello from method1");
}

public static void Method2( )
{
    Console.WriteLine("Hello from method2");
}

This will call both Method1 and Method2

Adrian
  • 693
  • 5
  • 19
  • More information about delegates and events you can find here http://www.codeproject.com/Articles/4773/Events-and-Delegates-Simplified – Adrian Apr 26 '14 at 13:15