C#在 .NET中使用依賴注入的示例詳解
寫在前面
在 .NET 中使用依賴注入 (DI)。 可以借助 Microsoft 擴(kuò)展,通過添加服務(wù)并在 IServiceCollection 中配置這些服務(wù)來管理 DI。 使用 IHost 接口來公開所有 IServiceProvider 實(shí)例,用來充當(dāng)所有已注冊的服務(wù)的容器。
示例代碼中使用了一個關(guān)鍵的枚舉 ServiceLifetime 指定 IServiceCollection 中服務(wù)的生存期,該枚舉包含三個類型:
Scoped 服務(wù)只會隨著新范圍而改變,但在一個范圍中是相同的實(shí)例。
Singleton 服務(wù)總是相同的,新實(shí)例僅被創(chuàng)建一次。
Transient 服務(wù)總是不同的,每次檢索服務(wù)時,都會創(chuàng)建一個新實(shí)例。
需要從NuGet安裝 Microsoft.Extensions.Hosting 類庫

代碼實(shí)現(xiàn)
服務(wù)接口實(shí)現(xiàn)
using Microsoft.Extensions.DependencyInjection;
namespace ConsoleDI.Example;
public interface IReportServiceLifetime
{
Guid Id { get; }
ServiceLifetime Lifetime { get; }
}
// 創(chuàng)建了多個接口和相應(yīng)的實(shí)現(xiàn)。 其中每個服務(wù)都唯一標(biāo)識并與 ServiceLifetime 配對
public interface IExampleTransientService : IReportServiceLifetime
{
ServiceLifetime IReportServiceLifetime.Lifetime => ServiceLifetime.Transient;
}
public interface IExampleScopedService : IReportServiceLifetime
{
ServiceLifetime IReportServiceLifetime.Lifetime => ServiceLifetime.Scoped;
}
public interface IExampleSingletonService : IReportServiceLifetime
{
ServiceLifetime IReportServiceLifetime.Lifetime => ServiceLifetime.Singleton;
}
internal sealed class ExampleTransientService : IExampleTransientService
{
Guid IReportServiceLifetime.Id { get; } = Guid.NewGuid();
}
internal sealed class ExampleScopedService : IExampleScopedService
{
Guid IReportServiceLifetime.Id { get; } = Guid.NewGuid();
}
internal sealed class ExampleSingletonService : IExampleSingletonService
{
Guid IReportServiceLifetime.Id { get; } = Guid.NewGuid();
}
示例代碼
namespace ConsoleDI.Example;
internal sealed class ServiceLifetimeReporter(
IExampleTransientService transientService,
IExampleScopedService scopedService,
IExampleSingletonService singletonService)
{
public void ReportServiceLifetimeDetails(string lifetimeDetails)
{
Console.WriteLine(lifetimeDetails);
LogService(transientService, "每次都是新建的對象,一直保持不同");
LogService(scopedService, "在函數(shù)域范圍內(nèi)只創(chuàng)建一次,不同函數(shù)內(nèi)為不同對象");
LogService(singletonService, "全局單例,一直是同一個");
}
private static void LogService<T>(T service, string message)
where T : IReportServiceLifetime =>
Console.WriteLine($" {typeof(T).Name}: {service.Id} ({message})");
}
調(diào)用示例

到此這篇關(guān)于C#在 .NET中使用依賴注入的示例詳解的文章就介紹到這了,更多相關(guān)C#依賴注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c#遍歷System.drawing.Color下面的所有顏色以及名稱以查看
c#遍歷System.drawing.Color下面的所有顏色以及名稱以查看,需要的朋友可以參考一下2013-02-02
WinForm實(shí)現(xiàn)同時讓兩個窗體有激活效果的特效實(shí)例
這篇文章主要介紹了WinForm實(shí)現(xiàn)同時讓兩個窗體有激活效果的特效實(shí)例,基于windows api實(shí)現(xiàn)一個窗體激活的時候給另外一個發(fā)消息的特效,在進(jìn)行C#項(xiàng)目開發(fā)時有一定的實(shí)用價值,需要的朋友可以參考下2014-09-09
C#利用Free Spire.XLS for .NET復(fù)制Excel工作表
在日常的 .NET 開發(fā)中,我們經(jīng)常需要操作 Excel 文件,本文將詳細(xì)介紹C#如何使用Free Spire.XLS for .NET 在同一工作簿內(nèi)或不同工作簿之間復(fù)制工作表,有需要的可以了解下2025-09-09
使用遞歸實(shí)現(xiàn)數(shù)組求和示例分享
這篇文章主要介紹了使用遞歸實(shí)現(xiàn)數(shù)組求和示例,思路是給定一個含有n個元素的整型數(shù)組a,求a中所有元素的和,需要的朋友可以參考下2014-03-03
C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法
這篇文章主要介紹了C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法,本文總結(jié)了Convert.ToDateTime(string)、Convert.ToDateTime(string, IFormatProvider)、DateTime.ParseExact()三種方法,需要的朋友可以參考下2015-07-07
C#中創(chuàng)建統(tǒng)一API接口的實(shí)現(xiàn)方案
在 C# 中創(chuàng)建統(tǒng)一 API 接口需要從架構(gòu)設(shè)計(jì)、技術(shù)選型和代碼實(shí)現(xiàn)等多個層面進(jìn)行規(guī)劃,本文給大家詳細(xì)介紹了實(shí)現(xiàn)方案和完整示例代碼,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2025-04-04

