Reputation: 3514
How to get configuration of started application in C# .Net Core
I know that I can do it like that:
public static string Get_Configuration() {
string conf = "Release";
#if DEBUG
conf = "Debug";
#endif
return conf;
}
But it doesn’t look very good, any ideas how to do it most beautifully?
Upvotes: 4
Views: 2790
Reputation: 1695
I think you mean environment if I'm not wrong. Here's what you can do, in SolutionExplorer go to:
Properties -> lunchSettings.json
In your launchSettings.json, you'll find a json object which contain a section named Profiles.
Under profile add the environments you want.
For example, in the first place your Profiles will be like that:
"profiles": {
"ProjectName.Development": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/Locations",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}}
Change your Profiles to be like this:
"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"
}
}}
After finishing. Add to your solution explorer:
appSettings.Development.json,
appSettings.Local.json,
appSettings.Production.json.
And add your configuration to these files.
Then you can navigate between your environments and run whatever you want from the tool bar as you can see in the following image:
Upvotes: 2
Reputation: 5624
I guess what you need is build platform.
Build platform is embedded in the DLL / EXE at the time of compilation.
I am not sure what is the purpose and what exactly should be input to the API you want to create. Hence I am throwing couple of options which I believe would help you to solve the issue.
Option 1: Use Assembly Attribute
In this you can conditionally apply assembly configuration attribute to the assembly. Then at runtime, you can check the assembly attributes applied to the DLL / EXE.
#if (Debug || DEBUG)
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
Option 2: Using C# code
There is a method is from Scott Hanselman's blog which takes assembly name and decides if the assembly was built using Release mode.
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;
}
Option 3: PowerShell Script
On the same blog, you can find some powershell scripts which can also be used.
Based on your purpose and ease, you can select any one of the option.
Hope this helps.
Upvotes: 3