Do not change global.asax in Sitecore 9!

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 similar manner you can do it in Sitecore 8 as well.

#1 Using Sitecore Owin middleware.

With 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:

And the code:

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 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.

And the code:

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:

Hope all this information is going to help you into your implementations.

Advertisements

3 Replies to “Do not change global.asax in Sitecore 9!”

  1. 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…

    Like

  2. 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.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s