質問

の説明 あったかレガシィタイプ HttpRequestScoped およびレガシィのwebサービスを消費するサービスです。を解決するサービスの遺産、グローバルなリゾルバー.このた全ての作業も1.4になり、私も使ってい2.1.12ん体験 DependencyResolutionException.

このコード に2.1.12、私のです。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);

設定します。ゾルバ-シングルトンで設定されている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.

役に立ちましたか?

解決

あなたは予想通り、すべてが動作するはずというよりもApplicationContainer RequestLifetimeを使用するように「リゾルバ」を設定した場合。

これはあなたのいるIContainerパラメータはILifetimeScopeに変更する必要がありますを意味します。

私はASMXの依存関係を注入するためのより良い方法についてはわからないんだけど、1があるかもしれませんが、私はAutofacがそれをサポートしていないと思います。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top