Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jeroen Breuer 4860 posts 12127 karma points MVP 3x admin c-trib
    Jan 08, 2017 @ 20:35
    Jeroen Breuer
    0

    Autofac issues when rebuilding Examine indexes

    Hello,

    My project uses Autofac for dependency injection. Here is the register code:

    // setup the mvc dependency resolver to user autofac
    var resolver = new AutofacWebApiDependencyResolver(container);
    GlobalConfiguration.Configuration.DependencyResolver = resolver;
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    

    It used to work without any issues on Umbraco 7.2.8. Now that we've upgraded to Umbraco 7.5.6 I sometimes get the following error:

    System.InvalidOperationException: The request lifetime scope cannot be created because the HttpContext is not available.
       at Autofac.Integration.Mvc.RequestLifetimeScopeProvider.GetLifetimeScope(Action`1 configurationAction)
       at Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(Type serviceType)
       at System.Web.Mvc.DependencyResolverExtensions.GetService[TService](IDependencyResolver resolver)
       at Project.Core.ServiceLocator.GetCacheManager() in d:\data\inetpub\sn-future-proof-platform\Sources\Project.Core\ServiceLocator.cs:line 86
       at Project.Core.UrlProviders.TrainingUrlSegmentProvider.GetUrlSegment(IContentBase content)
    

    Somehow DependencyResolver.Current throws the error.

    This error happens in a UrlSegmentProvider. The strange thing is I only get this error when I rebuild Examine indexes. So when a node is published the code just works.

    When I debugged after a publish these are the properties:

    DependencyResolver.Current = {Autofac.Integration.Mvc.AutofacDependencyResolver}
    RequestLifetimeScope = Tag = "AutofacWebRequest", IsDisposed = false
    

    When I debug during a Examine reindex these are the properties:

    DependencyResolver.Current = {Autofac.Integration.Mvc.AutofacDependencyResolver}
    RequestLifetimeScope = '((Autofac.Integration.Mvc.AutofacDependencyResolver)(DependencyResolver.Current)).RequestLifetimeScope' threw an exception of type 'System.InvalidOperationException'
    

    When I Google for this error I found the following, but I already tried everything suggested: http://stackoverflow.com/questions/21804857/autofac-the-request-lifetime-scope-cannot-be-created-because-the-httpcontext-i

    Has anyone had this error before? The strange thing is that it worked on Umbraco 7.2.8. So did something change with Examine which could cause this issue with Autofac?

    Jeroen

  • Damiaan 434 posts 1279 karma points MVP 2x c-trib
    Jan 09, 2017 @ 10:41
    Damiaan
    0

    We have a site with 7.4.3 running on Autofac. I think we should wait upgrading and watch this thread first.

    Maybe a question: do you tried on 7.4.3?

    Kind regards Damiaan

  • Jeroen Breuer 4860 posts 12127 karma points MVP 3x admin c-trib
    Jan 09, 2017 @ 12:32
    Jeroen Breuer
    0

    We fixed it by changing this code:

    Old code

     return DependencyResolver.Current.GetService(typeof(IRuntimeCacheProvider)) as IRuntimeCacheProvider;
    

    New code

    return (IRuntimeCacheProvider)GlobalConfiguration.Configuration.DependencyResolver.GetService(typeofIRuntimeCacheProvider));
    

    So it probably was related to HttpContext not being available anymore. Strange that it used to work in Umbraco 7.2.8.

    Jeroen

  • Erik 11 posts 42 karma points
    Aug 03, 2019 @ 21:47
    Erik
    0

    Thank you!

  • Shannon Deminick 1488 posts 5009 karma points hq
    Jan 10, 2017 @ 13:04
    Shannon Deminick
    0

    I would presume that in your Autofac container config, somehow the IRuntimeCacheProvider has been registered with a Request based lifespan whereas it's actually not, it's a Singleton (Application lifespan) and can/should be registered based on the ApplicationContext.ApplicationCache property. Somehow your WebApi setup doesn't have an issue with this which leads me to believe that something isn't quite right and that your application is probably hanging on to a Request based lifespan object as a Singleton.

    How have you registered IRuntimeCacheProvider in your container?

  • Jeroen Breuer 4860 posts 12127 karma points MVP 3x admin c-trib
    Jan 11, 2017 @ 16:03
    Jeroen Breuer
    0

    Hi Shannon,

    I inherited this project so I'm still trying to find out how it works.

    In the container we have the following code:

    builder.RegisterType<RuntimeCacheProvider>().As<IRuntimeCacheProvider>();
    

    This IRuntimeCacheProvider isn't the one from Umbraco but a custom implementation. In the RuntimeCacheProvider it does use the following:

    ApplicationContext.Current.ApplicationCache.RuntimeCache.InsertCacheItem
    

    Do we need to change that register?

    Jeroen

  • Damiaan 434 posts 1279 karma points MVP 2x c-trib
    Jan 11, 2017 @ 16:46
    Damiaan
    0

    If you use the ApplicationContext Singleton inside the customCacheProvider, You don't need to register anything for autofac.

  • Shannon Deminick 1488 posts 5009 karma points hq
    Jan 12, 2017 @ 00:05
    Shannon Deminick
    0

    Using singletons in your application where you have IoC configured is an anti-pattern and basically goes against the whole reason you'd have IoC in the first place.

    If you are using IoC then you need to register the services you need in the container even if they come from singletons, as an example (this is not auto-fac syntax):

     container.Register<IRuntimeCacheProvider>(x => ApplicationContext.Current.RuntimeCache);
    

    @jeroen, in my opinion then it would seem that this isn't being done correctly. Whatever their own RuntimeCacheProvider is is being created by auto-fac which means internally of itself it is probably using singletons (see above), that's not the right way to do things.

Please Sign in or register to post replies

Write your reply to:

Draft