Enable HttpRequestScoped Services on ASMX with Autofac 2.1.12

Description I had a legacy type of HttpRequestScoped and a legacy web service using this service. To resolve outdated services, I have a global resolver. This all works well in 1.4, and now that I am using 2.1.12, I am experiencing a DependencyResolutionException .

Code B 2.1.12 my Global.asax.cs file:

 builder.Register(c => new SomeLegacyType(HttpContext.Current)) // note: it relies on HttpContext.Current .As<SomeLegacyType>() .HttpRequestScoped(); _containerProvider = new ContainerProvider(builder.Build()); // this is my app IContainerProvider Setup.Resolver = new AutofacResolver(_containerProvider.ApplicationContainer); 

Setup.Resolver is a singleton, and it is installed in AutofacResolver, which looks something like this:

 public class AutofacResolver : IResolver { private readonly IContainer _container; public AutofacResolver(IContainer container) { _container = container; } public TService Get<TService>() { return _container.Resolve<TService>(); } } 

The web service is as follows:

 [WebService] public LegacyWebService : WebService { [WebMethod(EnableSession=true)] public String SomeMethod() { var legacyType = Setup.Resolver.Get<SomeLegacyType>(); } } 

Exception The following exception is thrown during Setup.Resolver.Get<SomeLegacyType>() :

 Autofac.Core.DependencyResolutionException: No scope matching the expression 'value(Autofac.Builder.RegistrationBuilder`3+<>c__DisplayClass0[SomeAssembly.SomeLegacyType,Autofac.Builder.SimpleActivatorData,Autofac.Builder.SingleRegistrationStyle]).lifetimeScopeTag.Equals(scope.Tag)' is visible from the scope in which the instance was requested. at Autofac.Core.Lifetime.MatchingScopeLifetime.FindScope(ISharingLifetimeScope mostNestedVisibleScope) at Autofac.Core.Resolving.ComponentActivation..ctor(IComponentRegistration registration, IResolveOperation context, ISharingLifetimeScope mostNestedVisibleScope) at Autofac.Core.Resolving.ResolveOperation.Resolve(ISharingLifetimeScope activationScope, IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.Core.Lifetime.LifetimeScope.Resolve(IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.Core.Container.Resolve(IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.ResolutionExtensions.TryResolve(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Service service, IEnumerable`1 parameters) at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters) at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context) 

Side question Is there a better way to have properties entered in ASMX, just like my ASPX pages, and not use Setup.Resolver )? I am using AttributedInjectionModule due to legacy issues. It does not look like the module is working in ASMX.

+4
source share
1 answer

If you configure your "resolver" to use RequestLifetime rather than ApplicationContainer, everything should work as expected.

This means that your IContainer parameter will need to change to ILifetimeScope.

I'm not sure of the best way to inject ASMX dependencies, maybe one, but I don't think Autofac supports it.

+5
source

Source: https://habr.com/ru/post/1302807/


All Articles