您好,登錄后才能下訂單哦!
本篇內容介紹了“ASP.NET Core服務生命周期是什么”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
在ConfigureServices方法中的容器注冊每個應用程序的服務,Asp.Core都可以為每個應用程序提供三種服務生命周期:
Transient(暫時):每次請求都會創建一個新的實例。這種生命周期最適合輕量級,無狀態服務。
Scoped(作用域):在同一個作用域內只初始化一個實例 ,可以理解為每一個請求只創建一個實例,同一個請求會在一個作用域內。
Singleton(單例):整個應用程序生命周期以內只創建一個實例,后續每個請求都使用相同的實例。如果應用程序需要單例行為,建議讓服務容器管理服務的生命周期,而不是在自己的類中實現單例模式。
為了演示生命周期和注冊選項之間的差異,請考慮以下接口,將任務表示為具有唯一標識符 OperationId 的操作。根據以下接口配置操作服務的生命周期的方式,容器在類請求時提供相同或不同的服務實例:
public interface IOperation { Guid OperationId { get; } } public interface IOperationTransient : IOperation { } public interface IOperationScoped : IOperation { } public interface IOperationSingleton : IOperation { } public interface IOperationSingletonInstance : IOperation { }
上面四種服務接口在 Operation 類中實現。調用Operation類時將自動生成一個GUID,下面是Operation類的實現:
public class Operation : IOperationTransient, IOperationScoped, IOperationSingleton, IOperationSingletonInstance { public Operation() : this(Guid.NewGuid()) { } public Operation(Guid id) { OperationId = id; } public Guid OperationId { get; private set; } }
再注冊一個OperationService服務實例,當通過依賴關系注入請求 OperationService 實例時,它將接收每個服務的新實例或基于從屬服務(Operation)的生命周期的現有實例。OperationService 服務作用就是第二次調用 Operation類,查看Operation類實例的作用域變化。
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()服務容器中注冊各個生命周期的實例:
public void ConfigureServices(IServiceCollection services) { 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>(); }
再在IndexModel模塊里面調用OnGet方法輸出,觀察IOperation與OperationService類屬性OperationId 值的變化:
public class IndexModel : PageModel { public OperationService _operationService { get; } public IOperationTransient _transientOperation { get; } public IOperationScoped _scopedOperation { get; } public IOperationSingleton _singletonOperation { get; } public IOperationSingletonInstance _singletonInstanceOperation { get; } public IndexModel( OperationService operationService, IOperationTransient transientOperation, IOperationScoped scopedOperation, IOperationSingleton singletonOperation, IOperationSingletonInstance singletonInstanceOperation) { _operationService = operationService; _transientOperation = transientOperation; _scopedOperation = scopedOperation; _singletonOperation = singletonOperation; _singletonInstanceOperation = singletonInstanceOperation; } public void OnGet() { Console.WriteLine("IOperation操作:"); Console.WriteLine("暫時:" + _transientOperation.OperationId.ToString()); Console.WriteLine("作用域:" + _scopedOperation.OperationId.ToString()); Console.WriteLine("單例:" + _singletonOperation.OperationId.ToString()); Console.WriteLine("實例:" + _singletonInstanceOperation.OperationId.ToString()); Console.WriteLine("OperationService操作:"); Console.WriteLine("暫時:" + _operationService._transientOperation.OperationId.ToString()); Console.WriteLine("作用域:" + _operationService._scopedOperation.OperationId.ToString()); Console.WriteLine("單例:" + _operationService._singletonOperation.OperationId.ToString()); Console.WriteLine("實例:" + _operationService._singletonInstanceOperation.OperationId.ToString()); } }
執行IndexModel 類輸出結果:
由圖總結如下:
2.1 Transient(暫時):每次調用服務的時候都會創建一個新的實例。即在IndexModel類的局部方法或屬性中(這里是OnGet方法)實例化一個依賴對象Operation類,偽代碼是:
public class IndexModel: PageModel { public void OnGet() { //調用IndexModel類時,實例化了兩次Operation類 //第一次 OperationService operationService=new OperationService(); //第二次 IOperationTransient TransientOperation=new Operation(); } }
2.2 Scoped(作用域):一次請求(Action)內對象實例是相同的,但每次請求會產生一個新實例。相當于在IndexModel類的全局中實例化一次依賴對象Operation類,偽代碼是:
OperationService operationService = null; public IndexModel() { operationService = new OperationService(); operationService._scopedOperation = new Operation(); } public void OnGet() { operationService._scopedOperation.OperationId; IOperationScoped operationScoped = operationService._scopedOperation; operationScoped.OperationId }
2.3 Singleton(單例):首次請求初始化同一個實例,后續每次請求都使用同一個實例。相當于在整個應用Application中只實例化一次實例,常見的單例模式。
生命周期流程圖如下:
“ASP.NET Core服務生命周期是什么”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。