简体   繁体   English

.NET Core pass 配置部分来自 IIS

[英].NET Core pass configuration section from IIS

In appsettingsjson file i have:在 appsettingsjson 文件中,我有:

  "DataSource": {
    "ConnectionString": "mongodb://localhost:27017",
    "DatabaseName": "Root",
    "CollectionName": "ApiLog"
  },

in Program.cs, i get this data like this在 Program.cs 中,我得到这样的数据

builder.Services.Configure<DatabaseSettings>(
    builder.Configuration.GetSection("DataSource"));

where DatabaseSettings class is; DatabaseSettings class 在哪里;

    public class DatabaseSettings
    {
        public string ConnectionString { get; set; } = null!;

        public string DatabaseName { get; set; } = null!;

        public string CollectionName { get; set; } = null!;
    }

Then i can access instance of DatabaseSettings via dependency injection like:然后我可以通过依赖注入访问 DatabaseSettings 的实例,例如:

    public class LogService
    {
        private readonly IMongoCollection<Log> _collection;

        public LogService(
            IOptions<DatabaseSettings> databaseSettings)
        {
            var mongoClient = new MongoClient(
                databaseSettings.Value.ConnectionString);

            var mongoDatabase = mongoClient.GetDatabase(
                databaseSettings.Value.DatabaseName);

            _collection = mongoDatabase.GetCollection<ElekseLog>(
                databaseSettings.Value.CollectionName);
        }
    }

the question is i dont want to store db info in appsettings json file.问题是我不想将数据库信息存储在 appsettings json 文件中。 I want to host this .net core app from IIS so I want to send this configuration info from IIS. Is there a way to achieve this?我想从 IIS 托管这个 .net 核心应用程序,所以我想从 IIS 发送这个配置信息。有没有办法实现这个?

One Option is to use the Microsoft.Extensions.Configuration.SystemEnvironment provider so you can set configuration values using environment variables.一种选择是使用Microsoft.Extensions.Configuration.SystemEnvironment提供程序,以便您可以使用环境变量设置配置值。

You can then set the environment variables on the IIS server to provide the necessary configuration for your app.然后,您可以在 IIS 服务器上设置环境变量,为您的应用程序提供必要的配置。

Another option would be the Microsoft.Extensions.Configuration.UserSecrets which allows you to store configuration in user locations on the file system instead of the appsettings.json file.另一个选项是Microsoft.Extensions.Configuration.UserSecrets ,它允许您将配置存储在文件系统上的用户位置,而不是 appsettings.json 文件。

You can replace values from appsettings.json with environment variables by adding ".AddEnvironmentVariables();"您可以通过添加“.AddEnvironmentVariables();”将 appsettings.json 中的值替换为环境变量in Program.cs在 Program.cs 中

  public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((webhostBuilderContext, configurationbuilder) =>
            {
                configurationbuilder.AddJsonFile("appsettings.json")
                .AddEnvironmentVariables();
            })
            .UseStartup<Startup>();


}

The setup up of environment variables in ISS is decribed here: https://stackoverflow.com/a/36836533/5706893此处描述了 ISS 中环境变量的设置: https://stackoverflow.com/a/36836533/5706893

There is a naming convention for env variables, eg for the ConnectionString property your env name should be环境变量有一个命名约定,例如,对于 ConnectionString 属性,您的环境名称应该是

DataSource__ConnectionString DataSource__ConnectionString

More details can be found here https://learn.microsoft.com/en-us/as.net/core/fundamentals/configuration/?view=as.netcore-7.0更多细节可以在这里找到https://learn.microsoft.com/en-us/as.net/core/fundamentals/configuration/?view=as.netcore-7.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM