Detecting platform using IsOSPlatform() doesn't work in a Docker linux container #8777
-
I'm trying to define the OS platform in my csproj as follows: <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
<DefineConstants>OS_WINDOWS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Linux'))">
<DefineConstants>OS_LINUX</DefineConstants>
</PropertyGroup> which would work fine normally, but when run in a docker linux container it thinks the OS is Windows. Are there any other options? I've got libraries I'm trying to prevent from being included, but if this is more of a runtime issue then I may need to figure out alternatives.
#if OS_WINDOWS
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !EnvironmentUtils.InDocker) {
var performanceCounter = new PerformanceCounter();
// no good
}
#endif |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
How do you set the target frameworks; do you have I'm surprised you get a TypeInitializerException. Which version of the System.Diagnostics.PerformanceCounter package are you using, and what is the inner exception? |
Beta Was this translation helpful? Give feedback.
Condition="$([MSBuild]::IsOSPlatform('Windows'))"
-- AFAIK this checks the platform on which you run MSBuild. So if you build on Windows but run on Linux, it won't match.How do you set the target frameworks; do you have
net6.0-windows
there?I'm surprised you get a TypeInitializerException. Which version of the System.Diagnostics.PerformanceCounter package are you using, and what is the inner exception?