日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

NetCore 依赖注入之服务之间的依赖关系

發(fā)布時間:2023/12/10 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NetCore 依赖注入之服务之间的依赖关系 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

簡單介紹,直接官方文檔

https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2

public interface IOperation {Guid OperationId { get; } }public interface IOperationTransient : IOperation { }public interface IOperationScoped : IOperation { }public interface IOperationSingleton : IOperation { }public interface IOperationSingletonInstance : IOperation { } public class Operation : IOperationTransient, IOperationScoped, IOperationSingleton, IOperationSingletonInstance {public Operation() : this(Guid.NewGuid()){}public Operation(Guid id){OperationId = id;}public Guid OperationId { get; private set; } } public class OperationService {public OperationService(IOperationTransient transientOperation,IOperationScoped scopedOperation,IOperationSingleton singletonOperation,IOperationSingletonInstance instanceOperation){TransientOperation = transientOperation;ScopedOperation = scopedOperation;SingletonOperation = singletonOperation;SingletonInstanceOperation = instanceOperation;}public IOperationTransient TransientOperation { get; }public IOperationScoped ScopedOperation { get; }public IOperationSingleton SingletonOperation { get; }public IOperationSingletonInstance SingletonInstanceOperation { get; } } 在 Startup.ConfigureServices 中,根據(jù)其指定的生存期,將每個類型添加到容器中: C#public void ConfigureServices(IServiceCollection services) {services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);services.AddScoped<IMyDependency, MyDependency>();services.AddTransient<IOperationTransient, Operation>();services.AddScoped<IOperationScoped, Operation>();services.AddSingleton<IOperationSingleton, Operation>();services.AddSingleton<IOperationSingletonInstance>(new Operation(Guid.Empty));// OperationService depends on each of the other Operation types.services.AddTransient<OperationService, OperationService>(); } public class IndexModel : PageModel {private readonly IMyDependency _myDependency;public IndexModel(IMyDependency myDependency, OperationService operationService,IOperationTransient transientOperation,IOperationScoped scopedOperation,IOperationSingleton singletonOperation,IOperationSingletonInstance singletonInstanceOperation){_myDependency = myDependency;OperationService = operationService;TransientOperation = transientOperation;ScopedOperation = scopedOperation;SingletonOperation = singletonOperation;SingletonInstanceOperation = singletonInstanceOperation;}public OperationService OperationService { get; }public IOperationTransient TransientOperation { get; }public IOperationScoped ScopedOperation { get; }public IOperationSingleton SingletonOperation { get; }public IOperationSingletonInstance SingletonInstanceOperation { get; }public async Task OnGetAsync(){await _myDependency.WriteMessage("IndexModel.OnGetAsync created this message.");} }

以下兩個輸出顯示了兩個請求的結(jié)果:

第一個請求:

控制器操作:

暫時性:d233e165-f417-469b-a866-1cf1935d2518
作用域:5d997e2d-55f5-4a64-8388-51c4e3a1ad19
單一實例:01271bc1-9e31-48e7-8f7c-7261b040ded9
實例:00000000-0000-0000-0000-000000000000

OperationService?操作:

暫時性:c6b049eb-1318-4e31-90f1-eb2dd849ff64
作用域:5d997e2d-55f5-4a64-8388-51c4e3a1ad19
單一實例:01271bc1-9e31-48e7-8f7c-7261b040ded9
實例:00000000-0000-0000-0000-000000000000

第二個請求:

控制器操作:

暫時性:b63bd538-0a37-4ff1-90ba-081c5138dda0
作用域:31e820c5-4834-4d22-83fc-a60118acb9f4
單一實例:01271bc1-9e31-48e7-8f7c-7261b040ded9
實例:00000000-0000-0000-0000-000000000000

OperationService?操作:

暫時性:c4cbacb8-36a2-436d-81c8-8c1b78808aaf
作用域:31e820c5-4834-4d22-83fc-a60118acb9f4
單一實例:01271bc1-9e31-48e7-8f7c-7261b040ded9
實例:00000000-0000-0000-0000-000000000000

觀察哪個?OperationId?值會在一個請求之內(nèi)和不同請求之間變化:

    • 暫時性 對象始終不同。?第一個和第二個客戶端請求的暫時性?OperationId?值對于?OperationService?操作和在客戶端請求內(nèi)都是不同的。?為每個服務請求和客戶端請求提供了一個新實例。
    • 作用域 對象在一個客戶端請求中是相同的,但在多個客戶端請求中是不同的。
    • 單一實例 對象對每個對象和每個請求都是相同的(不管?Startup.ConfigureServices?中是否提供?Operation?實例)。

接下來是本文重點要說明的

?

public void ConfigureServices(IServiceCollection services) {services.AddSingleton<SingletonService>();services.AddTransient<TransientService>();services.AddScoped<ScopedService>(); }

?

public class SingletonService{public string Id = Guid.NewGuid().ToString();}public class ScopedService{public string Id = Guid.NewGuid().ToString();}public class TransientService{public string Id = Guid.NewGuid().ToString();private ScopedService _transient;private SingletonService _singleton;public TransientService(ScopedService transient, SingletonService singleton){_transient = transient;_singleton = singleton;}}

這種情況是可以的,transient 可以依賴 singleton 和 scoped

對于 Transient 服務,它可以被任何服務依賴,但是在依賴的服務中創(chuàng)建的服務的生命周期會跟隨依賴的服務
對于 Scoped 服務,它只能被 Transient 和 Scoped 服務依賴,生命周期不會變化,如果要在 Singleton 服務中使用,可以使用 IServiceProvider 的 CreateScope 方法 ,然后 GetService, 但是 生成的 scoped 服務生命周期為
Singleton
對于 Singleton 服務,
它可以被任何服務依賴,生命周期不會變化 public class SingletonService{public string Id = Guid.NewGuid().ToString();private ScopedService _scoped;public SingletonService(IServiceProvider serviceProvider){using (var scope = serviceProvider.CreateScope()){_scoped = scope.ServiceProvider.GetService<ScopedService>();}}}public class ScopedService{public string Id = Guid.NewGuid().ToString();}public class TransientService{public string Id = Guid.NewGuid().ToString();}

?

這個 在單例中使用 Scoped 服務

轉(zhuǎn)載于:https://www.cnblogs.com/microestc/p/11215094.html

總結(jié)

以上是生活随笔為你收集整理的NetCore 依赖注入之服务之间的依赖关系的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。