This is a rule of thumb in Sitecore 9: Do not change global.asax. There are other ways to run a piece of code at the start of the Sitecore 9.
Until now I have used 2 ways in Sitecore 9 to run a piece of code at the start of the solution. In a similar manner, you can do it in Sitecore 8 as well.
#1 Using Sitecore Owin middleware.
With the release of Sitecore 9, Sitecore decided to embed a OWIN module and with that, there are a couple of pipelines that can be used.
What’s OWIN Middleware?
It stands for Open Web Interface for .Net. It is a new standardized interface between web servers and applications. It stands as a middleware to be used in a pipeline to handle requests and associated responses. OWIN provides a decoupling layer that allows two frameworks with disparate object models to be used together.
Sitecore 9 has implemented the OWIN Pipeline directly into the core platform and with that, we can integrate external identity provider logins and no only.
You can initialize the OWIN configuration like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> | |
<sitecore> | |
<pipelines> | |
<owin.initialize> | |
<processor type="MyExtensions.Pipelines.Demo.DemoCodeOWIN, MyExtensions" /> | |
</owin.initialize> | |
</pipelines> | |
</sitecore> | |
</configuration> |
And the code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyExtensions.Pipelines.Demo | |
{ | |
using Sitecore.Owin.Pipelines.Initialize; | |
using Owin; | |
public class DemoCodeOWIN : InitializeProcessor | |
{ | |
public override void Process(InitializeArgs args) | |
{ | |
ConfigureAuth(args.App); | |
} | |
public void ConfigureAuth(IAppBuilder app) | |
{ | |
// do magic with your app | |
} | |
} | |
} |
A good usage example is implemented in the module Sitecore SignalR.
Then Sitecore already implemented Federation Authentication in Sitecore 9 using OWIN middleware and you can extend it for your needs. Bas wrote the first article on how to Enable federated authentication and configure Auth0 as an identity provider in Sitecore 9.0.
Also, you can write your own implementation using OWIN, but will give you a proper example in my next article that is touching this topic and how to have custom authentication for your Sitecore Web Api.
In Sitecore 8 you can use module Sitecore-Owin to add OWIN support to your solution. Works quite well, as I have already used it to customize the Sitecore SignalR module.
#2 Using Initialize Pipeline in Sitecore.
This functionality was also available in Sitecore 8, in case you were not aware of that. This is used to start some functionality at Sitecore application start-up.
In a clean web application it possible to use global.asax, but Sitecore decided some time ago (not sure with what version exactly) to make globas.asax private. Instead you need to use the initialize pipeline. The initialize pipeline is started at the application start. Initialize pipeline is defined in Sitecore.config.
In order to use initialize pipeline it’s needed to created a new include config file with the new processor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> | |
<sitecore> | |
<pipelines> | |
<initialize> | |
<processor type="MyExtensions.Pipelines.Demo.DemoCode, MyExtensions" /> | |
</initialize> | |
</pipelines> | |
</sitecore> | |
</configuration> |
And the code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyExtensions.Pipelines.Demo | |
{ | |
public class DemoCode | |
{ | |
public void Process(PipelineArgs args) | |
{ | |
Sitecore.Diagnostic.Log.Info("MyExtensions: Running DemoCode.Process method", this); | |
//do your magic | |
Sitecore.Diagnostic.Log.Info("MyExtensions: Done running DemoCode.Process method", this); | |
} | |
} | |
} |
Anders Laub also has an article about this subject on this blog article Global.asax events and Sitecore pipelines.
One thing that this initialize pipeline can be used is to implement dependency injection into your solution One way to implement Dependency Injection for Sitecore Habitat.
Or you can with this functionality to register a Web Api Route using RouteTable from System.Web.Routing. Sitecore Habitat is using this functionality to register the routes like in this example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Sitecore.Feature.Demo.Pipelines | |
{ | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using Sitecore.Pipelines; | |
public class RegisterWebApiRoutes | |
{ | |
public void Process(PipelineArgs args) | |
{ | |
RouteTable.Routes.MapRoute("Feature.Demo.Api", "api/demo/{action}", new | |
{ | |
controller = "Demo" | |
}); | |
} | |
} | |
} |
I hope all this information is going to help you in to your implementations.
4 replies on “Do not change global.asax in Sitecore 9!”
Agree 100% – but how do you tap into the Application_Error event as part of a pipeline? That is really the only thing we have in the global.asax right now, and I’m looking for a creative way to take that out as well…
LikeLike
There is no pipeline for Application_Error, but Sitecore logs these anyway.
LikeLike
Right – so in that case, the only way to hook into that is using Global.asax, correct? I know Sitecore logs those, however, to enable a 500 error page or add additional information in the logs, it may be useful to hook into that event.
LikeLike
Thanks, this article really helped!
As its not mentioned, for #1, the dlls required for the references are Sitecore.Owin.Pipelines.Initialize (Sitecore.Owin.dll) and Owin (Owin.dll) which are shipped with the Sitecore 9 install.
Cheers
LikeLike