.net Core获取已启动应用程序的配置

时间:2019-04-12 12:51:44

标签: c# .net-core

如何在C#.Net Core中获取已启动应用程序的配置

我知道我可以那样做:

public static string Get_Configuration() {
      string conf = "Release";
#if DEBUG
       conf = "Debug";
#endif
      return conf;
}

但是看起来不是很好,有什么主意如何做得最漂亮吗?

enter image description here

2 个答案:

答案 0 :(得分:2)

我猜您需要的是构建平台。

编译时,编译平台已嵌入DLL / EXE中。

我不确定要创建的API的目的和确切输入的内容。因此,我提出了一些选择,我相信这些选择可以帮助您解决问题。

选项1:使用装配体属性

在此情况下,您可以有条件地将装配体配置属性应用于装配体。 然后在运行时,您可以检查应用于DLL / EXE的程序集属性。

#if (Debug || DEBUG)
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif

选项2:使用C#代码

Scott Hanselman's博客中提供了一种方法,该方法采用程序集名称并确定程序集是否使用发布模式构建。

public static int GetBuildType(string AssemblyName)
{
    Assembly assm = Assembly.LoadFrom(AssemblyName);
    object[] attributes = assm.GetCustomAttributes(typeof(DebuggableAttribute), false);
    if (attributes.Length == 0)
    {
        Console.WriteLine(String.Format("{0} is a RELEASE Build....", AssemblyName));
        return 0;
    }

    foreach (Attribute attr in attributes)
    {
        if (attr is DebuggableAttribute)
        {
            DebuggableAttribute d = attr as DebuggableAttribute;
            Console.WriteLine(String.Format("Run time Optimizer is enabled : {0}", !d.IsJITOptimizerDisabled));
            Console.WriteLine(String.Format("Run time Tracking is enabled : {0}", d.IsJITTrackingEnabled));

            if (d.IsJITOptimizerDisabled == true)
            {
                Console.WriteLine(String.Format("{0} is a DEBUG Build....", AssemblyName));
                return 1;
            }
            else
            {
                Console.WriteLine(String.Format("{0} is a RELEASE Build....", AssemblyName));
                return 0;
            }
        }
    }
    return 3;
}

选项3:PowerShell脚本

在同一博客上,您可以找到一些也可以使用的powershell脚本。

根据您的目的和轻松程度,您可以选择任何一个选项。

希望这会有所帮助。

答案 1 :(得分:1)

如果我没记错的话,我认为您的意思是环境。 您可以执行以下操作,转到SolutionExplorer中的 Properties-> LunchSettings.json 。 在您的 launchSettings.json 中,您将找到一个json对象,其中包含名为 Profiles 的部分 在配置文件下,添加所需的环境。例如,首先,您的个人资料将是这样的:

  "profiles": {
"ProjectName.Development": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "api/Locations",
  "applicationUrl": "http://localhost:5000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}}

将个人资料更改为这样:

  "profiles": {
"ProjectName.Development": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "api/Locations",
  "applicationUrl": "http://localhost:5000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
},
"ProjectName.Local": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "api/Locations",
  "applicationUrl": "http://localhost:5000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Local"
  }
},
"ProjectName.Production": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "api/Locations",
  "applicationUrl": "http://localhost:5000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Production"
  }
}}

完成后。添加 appSettings.Development.json appSettings.Local.json appSettings.Production .json 。并将您的配置添加到这些文件。 然后,您可以在环境之间导航并从工具栏中运行所需的任何内容,如下图所示:

enter image description here