如何在 Simple Injector 3.0.0 版中定义 DefaultScopedLifestyle?

How to define DefaultScopedLifestyle in Simple Injector version 3.0.0?

我在一个项目中使用简单的注入器进行依赖注入,它工作正常,直到我需要定义一个生活方式来解析那个特定版本的依赖,3.0.0(我用的是这个版本因为我用的是visual studio2010).

在官方文档中,它说我可以使用这行代码定义一个依赖的生活方式:

container.Register<IService, RealService>(Lifestyle.Scoped);

但是当我验证它时,它告诉我应该定义 DefaultScopeLifestyle,使用这个(或变体):

container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

但是在这个版本中,我找不到 AsyncScopedLifestyle class 或其变体。

如何设置 DefaultScopeLifestyle 或直接在依赖项中设置?

AsyncScopedLifestyle 的 v3 等价物是 ExecutionContextScopeLifestyle:

container.Options.DefaultScopedLifestyle = new ExecutionContextScopeLifestyle();

这种生活方式位于 SimpleInjector.Extensions.ExecutionContextScoping nuget package

此软件包要求您 运行 .NET 4.5 或更高版本。

如果您使用 .NET 4.0,则必须同步使用 LifetimeScopeLifestyle/ThreadScopedLifestyle 和 运行 操作,或者在构建时使用 WebRequestLifestyle网络应用程序。

您可以找到有关 LifetimeScope 的更多信息here and about ThreadScopedLifestyle here