I have a .NET 8 app which uses a custom conditional compilation symbol as follows:
#if MY_SYMBOL
public MyConstructor() : base(ArgumentOne) { }
#else
public MyConstructor() : base(ArgumentTwo)
{
throw new Exception("A compilation-time tier symbol must be set");
}
#endif
I am finding that if I pass my symbol to the dotnet run command as the DefineConstants
property like this – dotnet run -p:DefineConstants=MY_SYMBOL
– then it works fine (code is compiled with the first constructor).
However, if I do this in my csproj file like this…
<PropertyGroup>
<DefineConstants>$(DefineConstants);MY_SYMBOL</DefineConstants>
</PropertyGroup>
…then run the app in debug mode using a launch profile, the symbol isn’t set so the code is compiled with the second constructor above.
What is the reason for this? I’m trying to achieve a scenario where I have a default symbol in my local dev environment, then pass a custom symbol via dotnet run
in my Dockerfile for deploy environments. Is there a better, more reliable approach?