gpt4 book ai didi

c# - 如何解决 "registered as ' Async Scoped 的生活方式,但实例是在事件(Async Scoped)范围的上下文之外请求的”

转载 作者:行者123 更新时间:2023-11-30 18:11:29 26 4
gpt4 key购买 nike

我正在使用 TopShelf、Quartz 和 SimpleInjector。所以我一直有这个异常(exception),我尝试做混合,但最终还是

“已注册为‘混合异步范围/Web 请求’生活方式,但该实例是在事件(混合异步范围/Web 请求)范围的上下文之外请求的”

当我不使用 simpleinjector 并在构造函数中不传递接口(interface)的情况下简单调用类时,它会调用 Start 方法,但如果我使用 simpleinjector,它不会调用指定的 FooService:

HostFactory.Run(config =>
{
config.SetServiceName("Foo Scheduler");
config.SetDisplayName("Foo Scheduler");
config.RunAsLocalSystem();
config.UseSimpleInjector(container);
config.StartAutomatically();

config.Service<IFooService>(scheduler =>
{
scheduler.ConstructUsingSimpleInjector();
scheduler.WhenStarted((s, c) => s.Start());
scheduler.WhenStopped((s, c) => s.Stop());
});
});

这是错误堆栈跟踪:

Topshelf v4.2.1.215, .NET Framework v4.0.30319.42000 Topshelf.HostFactory Error: 0 : An exception occurred creating the host, Topshelf.ServiceBuilderException: An exception occurred creating the service: IFooService ---> SimpleInjector.ActivationException: FooService is registered as 'Async Scoped' lifestyle, but the instance is requested outside the context of an active (Async Scoped) scope. Please see https://simpleinjector.org/scoped for more information about how to manage scopes. at SimpleInjector.Scope.GetScopelessInstance[TImplementation](ScopedRegistration1
registration) at
SimpleInjector.Scope.GetInstance[TImplementation](ScopedRegistration
1 registration, Scope scope) at SimpleInjector.Advanced.Internal.LazyScopedRegistration1.GetInstance(Scope
scope) at lambda_method(Closure ) at
SimpleInjector.InstanceProducer.GetInstance() at
SimpleInjector.Container.GetInstanceFromProducer(InstanceProducer
instanceProducer, Type serviceType) at
SimpleInjector.Container.GetInstanceForRootType[TService]() at
SimpleInjector.Container.GetInstance[TService]() at
Topshelf.SimpleInjector.ServiceConfiguratorExtensions.<>c__0
1.b__0_0(HostSettings serviceFactory) at Topshelf.Builders.DelegateServiceBuilder1.Build(HostSettings
settings) --- End of inner exception stack trace --- at
Topshelf.Builders.DelegateServiceBuilder
1.Build(HostSettings settings) at Topshelf.Builders.RunBuilder.Build(ServiceBuilder serviceBuilder) at Topshelf.HostConfigurators.HostConfiguratorImpl.CreateHost() at Topshelf.HostFactory.New(Action1 configureCallback)
Topshelf.HostFactory Error: 0 : The service terminated abnormally,
Topshelf.ServiceBuilderException: An exception occurred creating the
service: IFooService ---> SimpleInjector.ActivationException:
FooService is registered as 'Async Scoped' lifestyle, but the instance
is requested outside the context of an active (Async Scoped) scope.
Please see https://simpleinjector.org/scoped for more information
about how to manage scopes. at
SimpleInjector.Scope.GetScopelessInstance[TImplementation](ScopedRegistration
1 registration) at SimpleInjector.Scope.GetInstance[TImplementation](ScopedRegistration1
registration, Scope scope) at
SimpleInjector.Advanced.Internal.LazyScopedRegistration
1.GetInstance(Scope scope) at lambda_method(Closure ) at SimpleInjector.InstanceProducer.GetInstance() at SimpleInjector.Container.GetInstanceFromProducer(InstanceProducer instanceProducer, Type serviceType) at SimpleInjector.Container.GetInstanceForRootTypeTService at SimpleInjector.Container.GetInstanceTService at Topshelf.SimpleInjector.ServiceConfiguratorExtensions.<>c__01.<ConstructUsingSimpleInjector>b__0_0(HostSettings
serviceFactory) at
Topshelf.Builders.DelegateServiceBuilder
1.Build(HostSettings settings) --- End of inner exception stack trace --- at Topshelf.Builders.DelegateServiceBuilder1.Build(HostSettings
settings) at Topshelf.Builders.RunBuilder.Build(ServiceBuilder
serviceBuilder) at
Topshelf.HostConfigurators.HostConfiguratorImpl.CreateHost() at
Topshelf.HostFactory.New(Action
1 configureCallback) at Topshelf.HostFactory.Run(Action`1 configureCallback)

编辑:我的 simpleinjector 容器包含以下内容:

var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
container.RegisterInstance<IJobFactory>(new SimpleInjectorJobFactory(container));
var quartzSchedulerFactory = new StdSchedulerFactory();
container.RegisterInstance<ISchedulerFactory>(quartzSchedulerFactory);
container.RegisterInstance<IScheduler>(await quartzSchedulerFactory.GetScheduler());
container.Register<IJobListener, CtrackJobListener>(Lifestyle.Scoped);
container.Register<ISchedulerService, SchedulerService>(Lifestyle.Scoped);

最佳答案

所以我已经通过阅读以下 URL 解决了这个问题: https://simpleinjector.readthedocs.io/en/latest/decisions.html#don-t-allow-resolving-scoped-instances-outside-an-active-scope

这只是意味着所使用的生活方式对于特定的一组类是错误的(例如,您正在强制使用异步作用域或其他,但它应该是单例或 transient 的)

事实证明,所有使用或继承 DbContext 的类以及使用 HttpClient 的类都应该使用“Lifestyle.Singleton”,如果它们有任何依赖关系(意味着在其中注入(inject)了另一个类),那么它们也应该使用 Lifestyle.Singleton .

我最终使用了一个混合体,并将这种生活方式应用于所有其他注入(inject)的类(除了那些使用单例的类)

var lifeStyle = Lifestyle.CreateHybrid(
lifestyleSelector: () => HttpContext.Current != null,
trueLifestyle: new AsyncScopedLifestyle(),
falseLifeStyle: Lifestyle.Transient);

我希望这对将来的其他人有帮助。

关于c# - 如何解决 "registered as ' Async Scoped 的生活方式,但实例是在事件(Async Scoped)范围的上下文之外请求的”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58055380/

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