سؤال

الوصف كان لدي نوع قديم HttpRequestScoped وخدمة ويب قديمة تستهلك تلك الخدمة. لحل الخدمات في المخاوف القديمة ، لدي حل عالمي. كان كل هذا يعمل بشكل جيد في 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);

SETUP.RESOLVER هو SINGENTON ، ويتم تعيينه على 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.

هل كانت مفيدة؟

المحلول

إذا قمت بتكوين "Resolver" الخاص بك لاستخدام requestLifetime بدلاً من ApplicationContainer ، فيجب أن تعمل جميعها كما هو متوقع.

هذا يعني أن معلمة Icontainer الخاصة بك يجب أن تتغير إلى IlifetimesCope.

لست متأكدًا من طريقة أفضل لضخ تبعيات ASMX ، فقد يكون هناك طريقة ولكني لا أعتقد أن Autofac يدعمها.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top