gpt4 book ai didi

asmx - 使用 Autofac 2.1.12 解析 ASMX 中的 HttpRequestScoped 服务

转载 作者:行者123 更新时间:2023-12-03 00:11:36 26 4
gpt4 key购买 nike

描述 我有一个遗留类型 HttpRequestScoped以及使用该服务的遗留 Web 服务。为了解决遗留问题中的服务,我有一个全局解析器。这一切在 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 中注入(inject)属性,就像注入(inject) ASPX 页面一样(而不是使用 Setup.Resolver )?我用AttributedInjectionModule因为遗产问题。该模块似乎不适用于 ASMX。

最佳答案

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

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

我不确定是否有更好的方法来注入(inject) ASMX 依赖项,可能有一个,但我认为 Autofac 不支持它。

关于asmx - 使用 Autofac 2.1.12 解析 ASMX 中的 HttpRequestScoped 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2359602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com