如何在ASP.NET Core 的任意類中注入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ì)有很多類是在這三塊之外的。
請(qǐng)問(wèn)我如何在這三大塊之外實(shí)現(xiàn) Configuration 的注入呢?換句話說(shuō):可以在任意類中實(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 類型的參數(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)的類,在 Startup 中將 IConfiguration 賦值給該靜態(tà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ō)的,我想在任意類中都可以讀取 Configuration 😂😂😂,問(wèn)題的差異來(lái)自于 Asp.Net Core 使用了 DI先行 的打法,哈哈,忘了過(guò)去吧,從 ♥ 開(kāi)始 ...
以上就是如何在 ASP.NET Core 的任意類中注入Configuration 的詳細(xì)內(nèi)容,更多關(guān)于ASP.NET Core 注入Configuration 的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
asp.net中如何批量導(dǎo)出access某表內(nèi)容到word文檔
最近有項(xiàng)目需求是這樣的,需要將某表中的每一條記錄中的某些內(nèi)容導(dǎo)出在一個(gè)word文檔中。下面小編就把我的解決辦法分享給大家,供大家參考2015-10-10
HTML服務(wù)器控件和WEB服務(wù)器控件的區(qū)別和聯(lián)系介紹
學(xué)習(xí)asp.net的時(shí)候一會(huì)用Html服務(wù)器控件,一會(huì)用Web服務(wù)器控件,起初做起例子來(lái)也挺迷糊的,下面對(duì)這兩個(gè)控件研究了一下做個(gè)筆記在此與大家分享下,感興趣的朋友可以了解下2013-08-08
ASPX向ASCX傳值以及文本創(chuàng)建圖片(附源碼)
把用戶在TextBox輸入的文字創(chuàng)建為一個(gè)圖片,ASCX的ImageButton的ImageUrl重新指向這剛產(chǎn)生的圖片,接下來(lái)介紹下ASPX向ASCX傳值,感興趣的朋友可以參考下哈2013-03-03
asp.net 自動(dòng)將漢字轉(zhuǎn)換成拼音第一個(gè)字母
把漢字轉(zhuǎn)換成拼音第一個(gè)字母 的實(shí)現(xiàn)代碼2009-03-03
google suggest 下拉菜單實(shí)現(xiàn)代碼(asp.net版本)
原來(lái)發(fā)表過(guò),是asp版本的,但是不支持上下鍵,現(xiàn)在后臺(tái)處理程序用.net寫(xiě)的。代碼進(jìn)行部分優(yōu)化。2009-07-07
使用VS2022在ASP.NET?Core中構(gòu)建輕量級(jí)服務(wù)
本文詳細(xì)講解了使用VS2022在ASP.NET?Core中構(gòu)建輕量級(jí)服務(wù)的方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
asp.net上傳execl文件后,在頁(yè)面上加載顯示(示例代碼)
本篇文章主要是對(duì)asp.net上傳execl文件后,在頁(yè)面上加載顯示(示例代碼)進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02
Asp.Mvc?2.0實(shí)現(xiàn)用戶注冊(cè)實(shí)例講解(1)
這篇文章主要介紹了Asp.Mvc?2.0如何實(shí)現(xiàn)用戶注冊(cè),實(shí)例講解很細(xì)致,注冊(cè)功能是每個(gè)網(wǎng)站必不可少的組成部分,感興趣的的朋友可以參考下2015-08-08
解決 The Controls collection cannot be modified because the co
在.aspx或.ascx的如果包括%,并在.aspx, .ascs中使用了AjaxToolkit中的控件,那么很可能會(huì)引發(fā)這個(gè)問(wèn)題,下面給出具體的解決方法。2010-10-10

