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:
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'
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?
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?
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):
@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.
Autofac issues when rebuilding Examine indexes
Hello,
My project uses Autofac for dependency injection. Here is the register code:
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:
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:
When I debug during a Examine reindex these are the properties:
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
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
We fixed it by changing this code:
Old code
New code
So it probably was related to HttpContext not being available anymore. Strange that it used to work in Umbraco 7.2.8.
Jeroen
Thank you!
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?
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:
This IRuntimeCacheProvider isn't the one from Umbraco but a custom implementation. In the RuntimeCacheProvider it does use the following:
Do we need to change that register?
Jeroen
If you use the ApplicationContext Singleton inside the customCacheProvider, You don't need to register anything for autofac.
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):
@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.is working on a reply...