Разрешение служб HttpRequestScoped в ASMX с помощью Autofac 2.1.12

Описание У меня был устаревший тип HttpRequestScoped и устаревшая веб-служба, использующая эту службу. Для разрешения сервисов в устаревших задачах у меня есть глобальный преобразователь. Все это хорошо работало в 1.4, а теперь, когда я использую 2.1.12, я испытываю DependencyResolutionException.

Код В версии 2.1.12 мой 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 — это синглтон, и для него устанавливается значение AutofacResolver, которое выглядит примерно так:

public class AutofacResolver : IResolver
{
    private readonly IContainer _container;

    public AutofacResolver(IContainer container)
    {
        _container = container;
    }

    public TService Get<TService>()
    {
        return _container.Resolve<TService>();
    }
}

Веб-сервис выглядит так:

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

Исключение Следующее исключение при вызове 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)

Дополнительный вопрос Есть ли лучший способ внедрить свойства в ASMX, так же, как вставляются мои страницы ASPX (вместо использования Setup.Resolver)? Я использую AttributedInjectionModule из-за проблем с наследием. Не похоже, чтобы модуль работал на ASMX.


person moribvndvs    schedule 01.03.2010    source источник


Ответы (1)


Если вы настроите свой «преобразователь» на использование RequestLifetime, а не ApplicationContainer, все должно работать как положено.

Это означает, что ваш параметр IContainer должен измениться на ILifetimeScope.

Я не уверен в лучшем способе внедрить зависимости ASMX, может быть один, но я не думаю, что Autofac его поддерживает.

person Nicholas Blumhardt    schedule 02.03.2010