如何在ASP.NET Core 的任意類(lèi)中注入Configuration
遇到的問(wèn)題
我已經(jīng)通讀了 MSDN 上關(guān)于 Configuration 的相關(guān)內(nèi)容,文檔說(shuō)可實(shí)現(xiàn)在 application 的任意位置訪問(wèn) Configuration .
下面是 Startup.cs 的模板代碼。
public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); if (env.IsEnvironment("Development")) { // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately. builder.AddApplicationInsightsSettings(developerMode: true); } builder.AddEnvironmentVariables(); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseApplicationInsightsRequestTelemetry(); app.UseApplicationInsightsExceptionTelemetry(); app.UseMvc(); } }
我知道可以通過(guò) DI 的方式將 Configuration 注入到 Controller,Service,Repository 等地方,但在真實(shí)項(xiàng)目中,會(huì)有很多類(lèi)是在這三塊之外的。
請(qǐng)問(wèn)我如何在這三大塊之外實(shí)現(xiàn) Configuration 的注入呢?換句話說(shuō):可以在任意類(lèi)中實(shí)現(xiàn) Configuration 的注入... 😭
解決方案
在 .NET Core 中你可以將 IConfiguration 作為參數(shù)直接注入到 Class 的構(gòu)造函數(shù)中,這本身就是可以的,如下代碼所示:
public class MyClass { private IConfiguration configuration; public MyClass(IConfiguration configuration) { ConnectionString = new configuration.GetValue<string>("ConnectionString"); } }
接下來(lái)要做的就是 new MyClass(),很顯然這樣做是不行的,因?yàn)槟愕臉?gòu)造函數(shù)還需要一個(gè) IConfiguration 類(lèi)型的參數(shù),所以你需要將 new MyClass() 塞入到 Asp.NET Core 的 DI 鏈中。
思路也很簡(jiǎn)單。
- 將 MyClass 注入到 ServiceCollection 容器中
public void ConfigureServices(IServiceCollection services) { services.AddTransient<MyClass>(); services.AddControllers(); }
- 生成 MyClass 實(shí)例
在 MyClass 的調(diào)用方處通過(guò) DI 生成實(shí)例,這里以 Controller 處為例,如下代碼所示:
public class MyController : ControllerBase { private MyClass _myClass; public MyController(MyClass myClass) { _myClass = myClass; } }
這樣是不是就完美的實(shí)現(xiàn)了在 MyClass 中使用 Configuration 了?
還有一種更簡(jiǎn)單粗暴的做法,無(wú)需注入, 只需定義一個(gè)靜態(tài)的類(lèi),在 Startup 中將 IConfiguration 賦值給該靜態(tài)類(lèi)保存即可,參考代碼如下:
public static class MyAppData { public static IConfiguration Configuration; } public Startup(IConfiguration configuration) { Configuration = configuration; MyAppData.Configuration = configuration; }
然后就可以在項(xiàng)目的各處訪問(wèn) MyAppData.Configuration 啦。
總結(jié)
在 Asp.Net 時(shí)代,讀取配置文件真的是一點(diǎn)都不需要操心,一個(gè) ConfigurationManager 走天下😄😄😄,比如下面的代碼:
private static string AppKey = ConfigurationManager.AppSettings["AppKey"];
反倒在 Asp.Net Core 中卻有點(diǎn)懵逼了,就像題主說(shuō)的,我想在任意類(lèi)中都可以讀取 Configuration 😂😂😂,問(wèn)題的差異來(lái)自于 Asp.Net Core 使用了 DI先行 的打法,哈哈,忘了過(guò)去吧,從 ♥ 開(kāi)始 ...
以上就是如何在 ASP.NET Core 的任意類(lèi)中注入Configuration 的詳細(xì)內(nèi)容,更多關(guān)于ASP.NET Core 注入Configuration 的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解ASP.NET 生成二維碼實(shí)例(采用ThoughtWorks.QRCode和QrCode.Net兩種方式)
本篇文章主要介紹了ASP.NET 生成二維碼實(shí)例,使用了兩種方法,包括ThoughtWorks.QRCode和QrCode.Net,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12ASP.NET MVC過(guò)濾器執(zhí)行順序介紹
這篇文章介紹了ASP.NET MVC過(guò)濾器的執(zhí)行順序,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03在Asp.net中為圖像加入水印信息并保存為Jpg類(lèi)型
這篇文章主要介紹了在Asp.net中為圖像加入水印信息,可定義字體、筆刷等等并保存為Jpg類(lèi)型,需要的朋友可以參考下2014-08-08ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)咨詢管理的架構(gòu)(十一)
這篇文章主要介紹了ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)咨詢管理的架構(gòu),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09在.NET Core類(lèi)庫(kù)中使用EF Core遷移數(shù)據(jù)庫(kù)到SQL Server的方法
下面小編就為大家分享一篇在.NET Core類(lèi)庫(kù)中使用EF Core遷移數(shù)據(jù)庫(kù)到SQL Server的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12ASP.NET Core使用HttpClient調(diào)用WebService
這篇文章介紹了ASP.NET Core使用HttpClient調(diào)用WebService的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03