亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何在ASP.NET Core 的任意類(lèi)中注入Configuration

 更新時(shí)間:2021年04月14日 09:55:42   作者:Stackoverflow  
這篇文章主要介紹了如何在 ASP.NET Core 的任意類(lèi)中注入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ì)有很多類(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)文章

最新評(píng)論