How to customize application life cycle

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 100%
  • 6.2
  • Enterprise
  • Community
  • Planned

Overview

One of the reasons Sense/Net ECMS is capable of handling the needs of enterprises is the extensibility of the software. As this is a web application, it is important for developers to be able to execute a custom code whenever the application starts, ends, or at any major point of a single request. This article discusses the possibilities of adding custom services and other executable code to the application and page life cycle.

Details

In Sense/Net ECMS the main entry point of the system is the start of the web application. This is when the Content Repository starts along with every manager type and service that are enabled in the configuration. In this article you can learn how to inject custom executable code at major points of the application and request life cycle so that you can provide your users with additional services and features related to the whole application or request.

If you are writing an executable tool that connects to the Sense/Net Content Repository, you may still make use of the ISnService interface as described in the following section.

ISnService interface

If you are creating a service that needs to start when the application starts and has to run throughout the whole application life cycle, implementing the ISnService interface is the easiest way to go. This is a simple interface containing only one method to implement: Start. Every type that implements this interface will be instantiated during the start process of the repository and every service will be started by calling the Start method. You only have to implement the interface and place your library into the directory of the web application or tool.

For example the following class handles the start process of the Notification feature:

public class NotificationService : ISnService
{
    /// <summary>
    /// Starts the service if it is enabled in the configuration.
    /// </summary>
    /// <returns>True if the service has started.</returns>
    public bool Start()
    {
        return NotificationHandler.StartNotificationSystem();
    }
}

SenseNetGlobal class

There are more complex cases when you need to execute a custom code when the web application starts or ends, or a single request starts or ends. SenseNetGlobal class lets you extend the well-known ASP.NET methods that can be implemented when you inherit the HttpApplication class (e.g. Application_Start, Application_Error, Application_BeginRequest etc.). For example:

protected virtual void Application_Start(object sender, EventArgs e, HttpApplication application);

All extendable methods match the ones you would implement in a custom HttpApplication class and have an additional argument of type HttpApplication. This gives you access to properties and methods on the global application object. To be able to execute custom logic you only have to inherit SenseNetGlobal and override one or more of the following methods.

Application_Start

Executed only once, at the start of the application life cycle. There is only one SenseNetGlobal instance in the system, so it is possible to assign a global variable in the Application_Start method, and access that in any other method during the whole application life cycle.

Application_End

Executed only once, at the end of the application life cycle.

Application_Error

Executed when an unhandled error is thrown by the ASP.NET application.

Application_BeginRequest

Executed at the start of every request.

Application_EndRequest

Executed at the end of every request.

RegisterRoutes

This method is responsible for registering MVC routes for the application. It is called from the Application_Start method. When you override that, it is your responsibility to call RegisterRoutes or call the base Application_Start method.

It is strongly recommended to call the base method (or replicate its behavior) when you override one of the methods above to avoid unexpected system behavior.

Example

The following example demonstrates how can you implement your own global class that executes custom logic at specific points of the application life cycle.

private class MyGlobal : SenseNetGlobal
{
    protected override void Application_Start(object sender, EventArgs e, HttpApplication application)
    {
        base.Application_Start(sender, e, application)
 
        // Start a custom service
        // ...
    }
    protected override void Application_End(object sender, EventArgs e, HttpApplication application)
    {
        // Stop the custom service
        base.Application_End(sender, e, application)
    }
    protected override void Application_BeginRequest(object sender, EventArgs e, HttpApplication application)
    {
        // Do something at the beginning of every request
        base.Application_BeginRequest(sender, e, application)
    }
}

Related links

There are no related links for this article.

References