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