2

I am trying to attach a handler/code when the Sitecore web application instance is starting up.

What event is triggered when the application instance starts?

  • Can you expand on your question to make it clearer please? What do you mean by "instance" ? – Richard Seal May 15 '17 at 17:38
  • Instance means Sitecore Web site Instance – Tony May 15 '17 at 17:39
  • Ok - so the application start is very different to a session start (from your question title) - which do you need? – Richard Seal May 15 '17 at 17:40
  • application start I think is what I am looking for. – Tony May 15 '17 at 17:45
10

Ok - for attaching code to when the application starts, you have a couple of options.

Initialize Pipeline

The standard way to do this is via the Initialize pipeline. Create yourself a new processor class. It does not need to inherit from anything, but must have a public method called Process that takes a PipelineArgs object.

Example: public class DoSomethingOnStartup { public void Process(PipelineArgs args) { // Add your code here } }

You would then patch that into Sitecore via an include file:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <initialize>
        <processor type="MyNamespace.DoSomethingOnStartup, MyAssembley" />
      </initialize>
    </pipelines>
  </sitecore>
</configuration>

WebActivatorEx

This is a slightly less Sitecorian way of doing it, but does work. Add WebActivatorEx to your project from NuGet.

Create your class and add it to the App_Start folder and add the WebActivatorEx attribute:

Example:

using System.Diagnostics;
using System.Web.Mvc;
using System.Web.Routing;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(TestWebApp.TestStartupCode.MyStartupCode), "Start",Order = 1)]

namespace TestWebApp.TestStartupCode
{
    public static class MyStartupCode
    {
        public static bool StartCalled { get; set; }

        public static void Start()
        {
            StartCalled = true;
            Debug.WriteLine("MyStartUpCode1");
        }
    }
}

Which to use?

It really is up to your requirements, both methods work. Using the Sitecore pipeline, means you can get access to other data in the Sitecore initialize pipeline and also you can position your startup code to run at a specific point in the start up. Its the option I would take 95% of the time.

If for some reason you can't or don't want to rely on the Sitecore pipeline, WebActivator is a good alternative. E.g if you are writing a component and don't have access to modify any configuration.

What about the Global.asax

Historically, this was also an option, but as this has now been made private by Sitecore, it is not a viable option anymore.

-1

Create your own Global.asax codebehind inheriting the sitecore one.

There you can put an event handler for the Application_Start event to execute your custom code.

  • Don't do this anymore - it will not work with later versions of Sitecore. – Richard Seal Jun 5 '17 at 16:49
  • Yes. Do not change OOTB Sitecore files unless absolutely necessary. The best practices and recommended answer to this is Richard's - using the Initialize Pipeline - that is exactly what it is for. – Radical Dave Oct 26 '17 at 13:43
  • The class Sitecore.Web.Application that the default Global.asax file uses has a virtual method for Application_Start, so yes, you can override it and it will work - I got the 8.2 update-4 version and I'm doing this. I totally agree not to mess with OOTB sitecore files but it's the Global.asax file that we're talking about. It would be dumb of Sitecore to completely own this file and not let developers extend it for their own implementation. – Jeff Sulit Oct 31 '17 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.