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
|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);|
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
Be careful with pipelines and DI because pipelines are singleton. So if you would like to use a constructor injected object which has
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
SitecoreContext, because if it is resolved incorrectly at the first call, it will be incorrect for the whole request.