2010-03-01 108 views
4

说明我有一个传统类型HttpRequestScoped和一个遗留Web服务使用该服务。为了解决遗留问题中的服务,我有一个全局解析器。这在1.4中运行良好,现在我正在使用2.1.12我正在体验DependencyResolutionException使用Autofac解决ASMX中的HttpRequestScoped服务2.1.12

守则在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>(); 
    } 
} 

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。

回答

5

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

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

我不确定更好的方法来注入ASMX依赖关系,可能有一个,但我不认为Autofac支持它。