runtime: Getting error on windows => System.PlatformNotSupportedException: 'System.IO.Ports is currently only supported on Windows.'

I have a .net standard 2.0 dll (Drivers.CommunicationLocal) which is referencing SerialPort class. When I try to use this from a .Net 4.6.1 console app or a xUnit (.net core 2.0) on windows 10, I get the following exception. Using it from UWP app works fine.

System.PlatformNotSupportedException: System.IO.Ports is currently only supported on Windows. at System.IO.Ports.SerialPort…ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits) at Drivers.CommunicationLocal.CommunicationLayerSerialPortLocal…ctor(SettingSerialPortLocal settings) in C:\Users\abc\Documents\Visual Studio 2017\Projects\experiment\senna\src\Drivers.CommunicationLocal\CommunicationLayerSerialPortLocal.cs:line 24

I am using nuget package System.IO.Ports.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 22 (9 by maintainers)

Most upvoted comments

Still persists in v5

I’ve stuck with this issue in my WPF project after migrating from .netframework to .NET 6. My library and main projects have TargetFramework as “net6.0-windows”. Installing package System.IO.Ports 6.0.0 does not fix the issue. Only copying dll from runtime “net6.0” fixes it.

For automation I added next code to the .csproj file:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
   <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
     <Output TaskParameter="Assemblies" ItemName="CurrentAssembly" />
   </GetAssemblyIdentity>
   <Exec Command="xcopy /F /Y /E $(TargetDir)\\runtimes\\win\\lib\\net6.0\\ $(TargetDir)\\" />
 </Target>

This issue has been marked needs-author-action since it may be missing important information. Please refer to our contribution guidelines for tips on how to report issues effectively.

I’ve stuck with this issue in my WPF project after migrating from .netframework to .NET 6. My library and main projects have TargetFramework as “net6.0-windows”. Installing package System.IO.Ports 6.0.0 does not fix the issue. Only copying dll from runtime “net6.0” fixes it.

For automation I added next code to the .csproj file:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
   <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
     <Output TaskParameter="Assemblies" ItemName="CurrentAssembly" />
   </GetAssemblyIdentity>
   <Exec Command="xcopy /F /Y /E $(TargetDir)\\runtimes\\win\\lib\\net6.0\\ $(TargetDir)\\" />
 </Target>

I have a serialport project: On Ubuntu 18.04, dotnet 6.0.200, System.IO.Ports 6.0.0, works OK On Manjaro, dotnet 6.0.102, System.IO.Ports 6.0.0, Error “only support on windows” The package System.IO.Ports was installer vía nuget (dotnet add package) On Manajaro, the @djanri suggestion works OK.

I switched my minimal repo (https://github.com/JonasGoldt/PortsPackageProblem) to .NET5 and System.IO.Ports 5.0.0 and it seems to be fixed.

The package isn’t correctly authored, my guess here is that the way that net461 project is consuming the package is via packages.config which won’t produce an assets file so it’s just bringing the same implementation picked by the netstandard assembly. It would really help if we could get a binlog to confirm my suspicions.

Here is a way to work around it:

  1. Create your .NET Standard library project.
  2. Add System.IO.Ports package version 4.7.0 and use it by for example referencing the SerialPort class.
  3. Create a .NET Framework project that targets net461.
  4. Add the following code to your .NET Framework .csproj:
<PropertyGroup>
  <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
  <RuntimeIdentifier>win</RuntimeIdentifier>
</PropertyGroup>
  1. Add reference from .NET Framework project to your .NET Standard library project.
  2. Using msbuild, restore packages for the .NET Framework project (either via developer command prompt or via Visual Studio). This step is very important. If doing it from the command line, then just call msbuild yourNetFrameworkProject.csproj /t:Restore
  3. Use the netstandard library

After running the above steps, I’m getting the right System.IO.Ports placed in the binfolder and I’m able to run correctly. Please give that a try and if it doesn’t work let me know so I can investigate.

I believe the issue is within project.asset.json. I created a copy of the whole project and then removed stuff one by one. I removed the bin and obj and restored the packages. This resulted in a different project.asset.json.

Unfortunately, I can’t share it and still trying to have a meaningful minimal repo that reproduces the issue. Sorry about that.

@harvinders is it possible to boil this down to a minimal repro (would be at least two projects and stub sources) and share for us? I suspect a reference isn’t flowing correctly through the project references.

Meantime you may be able to work around this by adding a direct Nuget package reference from your app project to System.IO.Ports package.