最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何在ASP.NET Core 的任意類中注入Configuration

 更新時(shí)間:2021年04月14日 09:55:42   作者:Stackoverflow  
這篇文章主要介紹了如何在 ASP.NET Core 的任意類中注入Configuration ,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下

遇到的問(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)文章

最新評(píng)論

隆林| 广丰县| 富民县| 承德县| 连州市| 襄城县| 西充县| 岐山县| 浠水县| 静宁县| 新民市| 陕西省| 海兴县| 抚松县| 沾化县| 宜昌市| 龙南县| 宿松县| 翁牛特旗| 安义县| 贡觉县| 板桥市| 房产| 栾城县| 故城县| 岐山县| 平度市| 新蔡县| 宁津县| 永安市| 贵南县| 岑巩县| 衡阳县| 基隆市| 南郑县| 蓬安县| 定兴县| 鹤峰县| 荆门市| 吐鲁番市| 彝良县|