3

I have a clean installation of Sitecore 9.0.2 and am trying to hook up Autofac, however I am getting the following exception when trying to load any page on the site / CMS.

enter image description here

In the logs:

Exception: Autofac.Core.DependencyResolutionException
Message: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = HttpContextBase (DelegateActivator), Services = [System.Web.HttpContextBase], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---> A delegate registered to create instances of 'System.Web.HttpContextBase' returned null. (See inner exception for details.)
Source: Autofac
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Execute()
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
   at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   at Sitecore.DependencyInjection.SitecorePerRequestScopeModule.GetScope(IServiceProvider serviceProvider)
   at Sitecore.DependencyInjection.SitecoreSingletonServiceScope.get_ServiceProvider()
   at Sitecore.DependencyInjection.ServiceLocator.get_ServiceProvider()
   at Sitecore.DependencyInjection.ServiceLocator.<>c__14`1.<GetRequiredResetableService>b__14_0()
   at Sitecore.DependencyInjection.LazyResetable`1.get_Value()
   at Sitecore.CES.DeviceDetection.Providers.DeviceDetectionClient.JobManagerWrapper.IsJobQueued(String jobName)
   at Sitecore.CES.DeviceDetection.Providers.DeviceDetectionClient.Tick()

Nested Exception

Exception: Autofac.Core.DependencyResolutionException
Message: A delegate registered to create instances of 'System.Web.HttpContextBase' returned null.
Source: Autofac
   at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)

Config:

<sitecore>
    <serviceProviderBuilder>
        <patch:attribute name="type" value="Foundation.DependencyInjection.DI.RegisterDependencies, Foundation.DependencyInjection" />
    </serviceProviderBuilder>
</sitecore>

Code:

public class RegisterDependencies : BaseServiceProviderBuilder
{
    protected override IServiceProvider BuildServiceProvider(IServiceCollection serviceCollection)
    {
        var builder = new ContainerBuilder();

        // Register Sitecore services in Autofac.
        builder.Populate(serviceCollection);

        IContainer container = builder.Build();

        // Return Autofac's service provider adaptor.
        return container.Resolve<IServiceProvider>();
    }
}

Relevant Packages:

<package id="Autofac" version="4.8.1" targetFramework="net462" />
<package id="Autofac.Extensions.DependencyInjection" version="4.2.2" targetFramework="net462" />
<package id="Autofac.Mvc5" version="4.0.2" targetFramework="net462" />
<package id="AutoMapper" version="5.1.1" targetFramework="net462" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net462" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net462" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net462" />
<package id="Microsoft.Extensions.DependencyInjection" version="1.1.0" targetFramework="net462" />
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.1.0" targetFramework="net462" />
1
  • Not really an answer to your question but since Sitecore started using dependency injection, it might be easier to didge autofac and switch to Microsoft.Extensions.DependencyInject. Then you don't have to mix frameworks. Jul 9 '18 at 8:52
2

Sitecore support asked if I can just change over to use the built-in Microsoft DI, and gave arguments that it is faster and better supported by sitecore. They mentioned that this 197841 may fix the issue. At the time they didn't have a release for 9.0.2 ready, but it looks like it's up now. Instead I bit the bullet and changed over to Microsoft DI.

0

It might cause from the version of Nuget packages, I think. Could you try with the suggestion from mine?

  1. Autofac -version 4.8.0
  2. Microsoft.Extensions.DependencyInjection -version 1.1.1
  3. Microsoft.Extensions.DependencyInjection.Abstractions -version 1.1.1

This the package.config:

<package id="Autofac" version="4.8.0" targetFramework="net462" />
<package id="Autofac.Extensions.DependencyInjection" version="4.2.2" targetFramework="net462" />
<package id="Autofac.Mvc5" version="4.0.2" targetFramework="net462" />
<package id="Microsoft.Extensions.DependencyInjection" version="1.1.1" targetFramework="net462" />
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.1.1" targetFramework="net462" />

Hope it'll solve your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.