tags:

views:

252

answers:

1

The Description I had a legacy type that is HttpRequestScoped and a legacy web service consuming that service. To resolve services in legacy concerns, I have a global resolver. This was all working well in 1.4, and now that I'm using 2.1.12 I'm experiencing DependencyResolutionException.

The Code In 2.1.12, my Global.asax.cs:

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's IContainerProvider
Setup.Resolver = new AutofacResolver(_containerProvider.ApplicationContainer);

Setup.Resolver is a singleton, and it is being set to 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 looks like this:

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

The Exception The following exception when Setup.Resolver.Get<SomeLegacyType>() is called:

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 injected in ASMX, the same way my ASPX pages are injected (rather than use Setup.Resolver)? I use the AttributedInjectionModule because of legacy concerns. It doesn't appear that the module works on ASMX.

+5  A: 

Hi!

If you configure your 'resolver' to use the RequestLifetime rather than ApplicationContainer all should work as expected.

This means your IContainer parameter will have to change to ILifetimeScope.

I'm not sure about a better way to inject ASMX dependencies, there may be one but I don't think Autofac supports it.

Cheers Nick

Nicholas Blumhardt