Background

Start-up code using WebActivator in MVC application

Mar 13, 2018, 13:47 PM by Lino Tadros
()
WebActivator allows us to execute a code long before application start-up. There may be requirements when you need to load external assemblies or instantiate a particular object before the application start up.
WebActivator allows us to execute a code long before application start-up. There may be requirements when you need to load external assemblies or instantiate a particular object before the application start up. You need to make sure some codes execute before the codes inside the App_Start folder and the global.asax file. To do this WebActivator should be used.
You can add the WebActivator package using NuGet.

WA1

Let us consider an example in an MVC application in which we want to call a method of a class before  the execution of the application. To do this follow the steps below:

  • Attribute the class with WebActivator
  • Pass the type of the class
  • Pass the name of the method to be executed
[assembly: WebActivatorEx.PostApplicationStartMethod
(typeof(AreaDemo.AreaStart), "RegisterAreas")]
namespace AreaDemo
{
    public class AreaStart
    {
        public static void RegisterAreas()
        {

In the above code listing the RegisterAreas() method, the AreaStart class will be executed on after the execution of the global.asax.

In WebActivator, you can configure when the code should be executed:

  1. Before the application start method using PreApplicationStartMethod
  2. After the application start method using PostApplicationStartMethod
  3. After the application shut down method using ApplicationShutdownMethod
WA2

So if you want to execute AreaStart.RegisterArea() before the execution of the global.asax then use PreApplicationStartMethod

[assembly: WebActivatorEx.PreApplicationStartMethod
(typeof(AreaDemo.AreaStart), "RegisterAreas")]
namespace AreaDemo
{
    public class AreaStart
    {
        public static void RegisterAreas()
        {

 
These are the steps you should follow to execute a code before an application start-up or shut down.


Load more reviews
You've already submitted a review for this item
|
()

Tags:

Where Will Lino be?

November 2019
IoT, Machine Learning & DevOps
Las Vegas, USA
Vegas

December 2019
IoT & Machine Learning
Mumbai, India
Mumbai

January 2020
IoT & Machine Learning
Toulouse, France
Toulouse

Copyright © 2019 Alain "Lino" Tadros
Using Sitefinity 12.1.7100
ProgressEducationProgressEliteServiceDeliveryProgressSystemIntegratorProgressSitefinityCertified