Glass Mapper – SitecoreContext.Database is null

In the last days I had to discover why on first load of the IIS site SitecoreContext.Database is null.

We are using SimpleInjector for dependency injection, so the SitecoreContext object is registered in the DI container with WebRequestLifestyle.

public class Initializer
public static void Process(PipelineArgs args)
container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
container.Register(typeof(ISitecoreContext), () => new SitecoreContext(), Lifestyle.Scoped);
new ChainedDependencyResolver(
new SimpleInjectorDependencyResolver(container),
view raw Initializer.cs hosted with ❤ by GitHub

This is no problem until you use SitecoreContext in Controllers. The problem comes when accidentally SitecoreContext is resolved before the Sitecore.Context.Database is resolved. SitecoreContext initilazation won’t fail but the SitecoreContext.Database remain null through the whole request because of WebRequestLifestyle.

Be careful with pipelines and DI because pipelines are singleton. So if you would like to use a constructor injected object which has TransientLifeStyle or WebRequestLifestyle they will be singleton under the hood.

Other places where you need to use carefully the SitecoreContext with DI:

  • IIS modules (these are running before Sitecore database resolver)
  • IIS handlers (these are running before Sitecore database resolver)
  • initialize pipeline and every pipeline

So in general be careful when you are using WebRequestLifestyle for SitecoreContext, because if it is resolved incorrectly at the first call, it will be incorrect for the whole request.

Leave a Reply

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

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

Facebook photo

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

Connecting to %s