代码之家  ›  专栏  ›  技术社区  ›  moribvndvs

使用autopac 2.1.12解析asmx中的httpRequestScoped服务

  •  4
  • moribvndvs  · 技术社区  · 15 年前

    描述 我有个传统类型 HttpRequestScoped 以及使用该服务的传统Web服务。为了解决遗留问题中的服务,我有一个全局解析器。这一切在1.4版本中都很有效,现在我使用的是2.1.12版本 DependencyResolutionException .

    代码 在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);
    

    resolver是一个singleton,它被设置为autosacresolver,看起来像这样:

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

    Web服务如下所示:

    [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上似乎不起作用。

    1 回复  |  直到 13 年前
        1
  •  5
  •   Tim Cooper    13 年前

    如果将“解析器”配置为使用RequestLifetime而不是ApplicationContainer,那么所有这些都应按预期工作。

    这意味着您的IContainer参数必须更改为ILifetimeScope。

    我不确定注入asmx依赖项的更好方法,可能有一种,但我认为autopac不支持它。