zoukankan      html  css  js  c++  java
  • dotnet core 发布配置(测试数据库和正式数据库自动切换)

    一、起源

      在进行项目开发时,常常要求开发环境,测试环境及正式环境的分离,并且不同环境运行的参数都是不一样的,比如监听地址,数据库连接信息等。当然我们把配置信息保存到一个文件中,每次发布的时候,可以先修改配置文件的内容,然后再进行程序发布,这样操作起来无疑是很麻烦,每次发布都得先确定对应的环境,然后修改配置信息,如果需要同时发布多个环境版本,那就得进行多次操作。

    二、解决

      在执行dotnet run之前,可以先执行以下下面的指令:

      set ASPNETCORE_ENVIRONMENT= 环境名称,注意这里没有引号,直接把环境名称写成具体的值即可,比如 set ASPNETCORE_ENVIRONMNET=development,然后再执行dotnet run指令,这样当前运行就会按照set指令中设置的环境进行运行。

      2.1、设置Startup.cs的构造函数
            public IConfiguration Configuration { get; }
    
            public Startup(IHostingEnvironment env)
            {
                Console.WriteLine(env.EnvironmentName);
                var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
                Configuration = builder.Build();
            }

      项目中有2个appsettings.json

      

       2.2、配置Program.cs

      

    namespace AuthorityManagement
    {
        using System.IO;
        using Microsoft.AspNetCore.Hosting;
        using Microsoft.Extensions.Configuration;
    
        public static class Program
        {
            public static void Main(string[] args)
            {
                var config = new ConfigurationBuilder()
                        .AddEnvironmentVariables()
                        .AddCommandLine(args)
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .Build();
    
                var host = new WebHostBuilder()
                    .UseEnvironment(config["ASPNETCORE_ENVIRONMENT"])
                    .UseConfiguration(config)
                    .UseKestrel()
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseIISIntegration()
                    .UseStartup<Startup>()
                    .UseApplicationInsights()
                    .Build();
    
                host.Run();
            }
        }
    }
      2.3、附上launchSettings.json
    {
      "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localhost:32030/",
          "sslPort": 0
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "dev"
          }
        },
        "AuthorityManagement": {
          "commandName": "Project",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "dev"
          },
          "applicationUrl": "http://localhost:32030/"
        }
      }
    }
  • 相关阅读:
    linux上安装vsftpd
    springboot集成mybatisplus
    springboot集成swagger2
    ssm+maven多模块项目整合
    追梦强人工智能(一)
    Linux环境kafka安装
    Linux环境安装jdk10
    东芝笔记本Satellite M40-A
    Maven简介
    postgresql PL/pgSQL—存储过程结构和变量声明
  • 原文地址:https://www.cnblogs.com/WangJunZzz/p/9682598.html
Copyright © 2011-2022 走看看