【问题标题】:Read appsettings in asp net core console application在 asp net core 控制台应用程序中读取 appsettings
【发布时间】:2017-03-03 08:29:00
【问题描述】:

我正在尝试读取我的控制台应用程序上的 appsetting 并设置他的 EntityFramework 连接字符串。

我做了很多谷歌,但没有找到任何单一的解决方案,甚至在微软的文档中也没有。

这是我的问题。

  1. 如何设置 EntityFramework 连接字符串?,我的实体框架项目是独立的,对于我的 MVC 项目,我按照以下代码进行。
string connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<MyDBContext>(option =>option.UseSqlServer(connectionString, m => m.MigrationsAssembly("MyMVCDLL")));
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
  1. 如何阅读应用设置?
  2. 如何在控制台应用程序中实现 DI 以获取应用程序设置?

谁能帮帮我。

【问题讨论】:

    标签: c# asp.net-core entity-framework-6 console-application


    【解决方案1】:

    首先,不要将敏感数据(登录名、密码、API 密钥)保存在 appsettings.json 中,因为您可能会不小心将其提交给 Verison Control,从而导致您的凭据泄露。为此,您必须使用 User Secrets 工具进行开发,有关详细信息,请参阅User Secret documentation

    其次,阅读Configuration.GetConnectionString("DefaultConnection");方法的工具提示文档。它明确指出`GetConnectionString 是一个

    GetSection(“ConnectionStrings”)[名称]的简写

    话虽如此,您的 appsettings.json 必须如下所示:

    {
        ...,
        "ConnectionStrings":
        {
            "DefaultConnection" : "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;"
        }
    }
    

    或在使用用户机密时:

    dotnet 用户机密集 ConnectionStrings:DefaultConnection Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

    更新

    在控制台应用程序中使用它是完全一样的。配置包不是 ASP.NET Core 特定的,可以单独使用。

    所需的包是(取决于您要使用的包

    "Microsoft.Extensions.Configuration": "1.0.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0", "Microsoft.Extensions.Configuration.Json": "1.0.0", "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",

    并且构建配置的代码与 ASP.NET Core 中的代码完全相同。与其在Startup.cs 中执行,不如在Main 方法中执行:

    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        // You can't use environment specific configuration files like this
        // becuase IHostingEnvironment is an ASP.NET Core specific interface
        //.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddUserSecrets()
        .AddEnvironmentVariables();
    

    【讨论】:

    • 我完全理解这一点,正如我如何在 MVC 应用程序中使用相同的问题中提到的那样,我的问题是我将如何在控制台应用程序中访问它,如果可能的话,我会分享一些代码。我需要知道我需要使用什么构建器类和什么 dll。
    • 完全一样。配置包不是 ASP.NET Core 特定的,因为它位于 Microsoft.Extensions.Configuration 包中
    • 好的,我将如何使用这个 "string connectionString = Configuration.GetConnectionString("DefaultConnection"); services.AddDbContext(option =>option.UseSqlServer(connectionString, m => m.MigrationsAssembly ("MyMVCDLL"))); services.Configure(Configuration.GetSection("AppSettings"));"
    • 我在这里完全没有线索。在 mvc 应用程序中,我正在使用 DI 获取 appsetting,例如“IOptions appSettings”
    猜你喜欢
    • 1970-01-01
    • 2017-01-27
    • 2015-01-21
    • 2016-08-21
    • 2019-12-29
    • 2023-04-10
    • 2020-02-09
    • 2018-07-21
    • 2023-03-20
    相关资源
    最近更新 更多