Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC5: No scope with a Tag matching 'AutofacWebRequest' is visible from the scope

I am encountering an error in my ASP.NET MVC 5 application using autofac v3.5.0, Autofac.Extras.CommonServiceLocator v3.2.0, Autofac.Mvc5 v3.3.2 all with targetframework net45 :

No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.

when trying to convert the following Rhino-Security windsor mapping:

WindsorServiceLocator windsorServiceLocator = new WindsorServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => windsorServiceLocator);

    container.Register(
            Component.For<IAuthorizationService>()
                .ImplementedBy<AuthorizationService>()
                .Lifestyle.Is(Lifestyle.Transient),
            Component.For<IAuthorizationRepository>()
                .ImplementedBy<AuthorizationRepository>()
                .Lifestyle.Is(Lifestyle.Transient),
            Component.For<IPermissionsBuilderService>()
                .ImplementedBy<PermissionsBuilderService>()
                .Lifestyle.Is(Lifestyle.Transient),
            Component.For<IPermissionsService>()
                .ImplementedBy<PermissionsService>()
                .Lifestyle.Is(Lifestyle.Transient)
            );

to an autofac mapping. The first 4 mappings work without any issue but the last one causes the above error. I have traced the first instance of where the error occurs this line: https://github.com/hibernating-rhinos/rhino-security/blob/master/Rhino.Security/Security.cs#L34

builder.RegisterType<AuthorizationService>().As<IAuthorizationService>().InstancePerDependency();
builder.RegisterType<AuthorizationRepository>().As<IAuthorizationRepository>().InstancePerDependency();
builder.RegisterType<PermissionsBuilderService>().As<IPermissionsBuilderService>().InstancePerDependency();
builder.RegisterType<PermissionsService>().As<IPermissionsService>().InstancePerDependency();

// Everything works except this
builder.RegisterType<OrganizationInformationExtractor>().As<IEntityInformationExtractor<Organization>>().InstancePerDependency();
like image 852
jmm312 Avatar asked Jul 28 '14 16:07

jmm312


1 Answers

Check all the parameters that OrganizationInformationExtractor needs. One of those parameters is either not registered in autofac or it is nor registered as InstancePerDependency. This worked for me.

like image 118
bondus Avatar answered Oct 27 '22 00:10

bondus