WorkContext NULL when using ISiteThemeService in Routes during start up

Topics: Core, Customizing Orchard, Troubleshooting, Writing modules
Jan 6, 2013 at 2:30 PM

We have implemented a service implementing IRouteConstraint with a match-method. Some routes call this match method, which in turn calls ISiteThemeService.GetCurrentThemeName().

We experience problems in the event, the Orchard application has to start (or start over, in case some code has been changed):

When first requesting a URL which does not need the mentioned match method, everything works fine. And if we request a URL afterwards, which does need the matching, also everything works as expected.

But when the first URL which is requested, is one of those routes, who enter the match method, we get the following error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 55:                 _cacheContextAccessor.Current = context;
Line 56: 
Line 57:                 entry.Result = acquire(context);
Line 58:             }
Line 59:             finally {


Source File: c:\Projects\discoverize\src\Orchard\Caching\Cache.cs    Line: 57

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   Orchard.Themes.Services.SiteThemeService.b__0(AcquireContext`1 ctx) +247
   Orchard.Caching.Cache`2.CreateEntry(TKey k, Func`2 acquire) in c:\Projects\discoverize\src\Orchard\Caching\Cache.cs:57
   Orchard.Caching.Cache`2.AddEntry(TKey k, Func`2 acquire) in c:\Projects\discoverize\src\Orchard\Caching\Cache.cs:27
   Orchard.Caching.<>c__DisplayClass2.b__0(TKey k) in c:\Projects\discoverize\src\Orchard\Caching\Cache.cs:19
   System.Collections.Concurrent.ConcurrentDictionary`2.AddOrUpdate(TKey key, Func`2 addValueFactory, Func`3 updateValueFactory) +161
   Orchard.Caching.Cache`2.Get(TKey key, Func`2 acquire) in c:\Projects\discoverize\src\Orchard\Caching\Cache.cs:17
   Orchard.Caching.DefaultCacheManager.Get(TKey key, Func`2 acquire) in c:\Projects\discoverize\src\Orchard\Caching\DefaultCacheManager.cs:33
   Orchard.Themes.Services.SiteThemeService.GetCurrentThemeName() +178
   Teamaton.Discoverize.StaticPages.PageNameProvider.GetPagePathsForCurrentTheme() +162
   Teamaton.Discoverize.StaticPages.PageNameProvider.GetPageNames() +51
   Teamaton.Discoverize.StaticPages.PageNameRouteConstraint.Match(HttpContextBase httpContext, Route route, String parameterName, RouteValueDictionary values, RouteDirection routeDirection) +269
   System.Web.Routing.Route.ProcessConstraint(HttpContextBase httpContext, Object constraint, String parameterName, RouteValueDictionary values, RouteDirection routeDirection) +100
   System.Web.Routing.Route.ProcessConstraints(HttpContextBase httpContext, RouteValueDictionary values, RouteDirection routeDirection) +284
   System.Web.Routing.Route.GetRouteData(HttpContextBase httpContext) +377
   Orchard.Mvc.Routes.ShellRoute.GetRouteData(HttpContextBase httpContext) in c:\Projects\discoverize\src\Orchard\Mvc\Routes\ShellRoute.cs:47
   System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext) +354
   System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +74
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +79
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +269


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 

When debugging the code we worked out that IOrchardServices.WorkContext is still NULL when the ISiteThemeService.GetCurrentThemeName() is being called.

What can we do to remedy this situation?

Is this expected behavior, or may it even be bug in Orchard?