gpt4 book ai didi

c# - Ninject 中的 .NET Core DI 作用域生命周期

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

编辑:由于许多用户错误地将其视为 ASP.NET 特定问题。请注意,我的应用程序不是 Web 应用程序,并且我没有使用 ASP.NET 应用程序(我使用的是它的功能,它在 .NET Core 中也可用)。


最近,在 Ninject DI 中配置 Entity Framework DbContext 生命周期时,我一直在研究 .NET Core 依赖注入(inject),因为它已经具有注册 DbContext 的功能并且可以找到 here .默认的上下文生命周期是 ServiceLifetime.Scoped

在 code peek 中,我们可以看到在 ASP.NET 应用程序中,“作用域”意味着:

scope is created around each server request

namespace Microsoft.Extensions.DependencyInjection
{
//
// Summary:
// Specifies the lifetime of a service in an Microsoft.Extensions.DependencyInjection.IServiceCollection.
public enum ServiceLifetime
{
//
// Summary:
// Specifies that a single instance of the service will be created.
Singleton = 0,
//
// Summary:
// Specifies that a new instance of the service will be created for each scope.
//
// Remarks:
// In ASP.NET Core applications a scope is created around each server request.
Scoped = 1,
//
// Summary:
// Specifies that a new instance of the service will be created every time it is
// requested.
Transient = 2
}
}

我正在尝试在 Ninject DI 中实现类似的功能,但在谈论 .NET Core 应用程序(那不是网络申请!)。

Ninject 有 InRequestScope方法,但它仅适用于 Web 应用程序,因此它与 .NET Core DI ServiceLifetime.Scoped 设置确实不同。

也许我必须创建某种类型的 custom scope in Ninject ,但仍然 - 我真的无法说明如何实现与 .NET Core DI 中完全相同的作用域行为。为此,我需要了解作用域生命周期在 .NET Core DI 中的 .NET Core 应用程序上下文中是如何工作的。我的猜测是,有一个正在创建的 DbContext 实例,并在应用程序退出后被释放。

因此我的问题:

  • .NET Core DI scope 生命周期设置是如何工作的,它的生命周期是什么?
  • 是否有可能在 Ninject DI 中实现类似的行为?

最佳答案

How is .NET Core DI scope life time setting working and what is it's life cycle?

.Net 核心在内部使用名为 ServiceScope 的类。当调用新请求(例如网络请求)时,将创建新实例,其中包括新的服务提供者。在请求期间,此服务提供者用于依赖项解析。请求完成后,处理范围及其服务提供者及其解析的服务。

  internal class ServiceScope : IServiceScope, IDisposable
{
private readonly Microsoft.Extensions.DependencyInjection.ServiceProvider _scopedProvider;

public ServiceScope(Microsoft.Extensions.DependencyInjection.ServiceProvider scopedProvider)
{
this._scopedProvider = scopedProvider;
}

public IServiceProvider ServiceProvider
{
get
{
return (IServiceProvider) this._scopedProvider;
}
}

public void Dispose()
{
this._scopedProvider.Dispose();
}
}

Is it possible to achieve a similar behaviour in Ninject DI?

正如您已经注意到的那样,实现自定义范围是可行的。您可以在另一个答案中查看如何执行此操作:

Ninject - In what scope DbContext should get binded when RequestScope is meaningless?

编辑:

.NET Core DI 的原理与任何其他 IOC 容器相同。它通过 DI 为您的对象(MVC Controller 等)提供依赖性并控制其生命周期。

  • 如果您为 DbContext 指定单例生存期,那么只有一个是创建,在请求时由 DI 提供并保存在内存中整个应用程序/容器的生命周期。
  • 如果你指定 transient 你得到新的一直请求 DbContext。
  • 如果您指定范围,DbContext 的生命周期绑定(bind)到某个一次性范围,它是在某些逻辑请求(在 asp 的情况下是 http 请求)的开始时创建的。当 DbContext 是DI 第一次请求,创建新的,保存在内存中,并且在期间你总是一样的后续的 DI 请求直到范围被处理(在 asp 的情况下以 http 请求结束)和 DbContext

您可以找到与 TransactionScope 类似的并行。在这里,同一个 TransactionScope 中的所有 sqlCommand 都被征募到同一个 sql 事务中,以处理/提交该范围。

关于c# - Ninject 中的 .NET Core DI 作用域生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51163692/

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