System.IO.Ports 5.0 NuGet package does not work in my solution

1

I'm getting quite desparate here. My project is stalled by problems getting System.IO.Ports to work in this .NET 5.0 solution on Windows. I have recently ported the whole application from VS2017/.NET Framework 4.6.1/WinForms to VS2019/.NET5/WPF. I didn't have this problem before the big operation.

If I create a new bare bone solution with a WPF application and a class library that uses System.IO.Ports it works perfectly.

The differences to the real solution are:

  • The real solution uses other NuGet packages: Antlr4.Runtime.Standard 4.9.1, Antlr4BuildTasks 8.13, CommandLineParser 2.8, Newtonsoft.Json 12.0.3, System.Reflection.Emit 4.7 and System.Resources.Extensions 5.0.0.
  • The class library using System.IO.Ports is being dynamically loaded using reflection.
  • The WPF application is using ActiproSoftware.Controls.WPF 20.1.1

The first thing showing something is wrong is there is no copy of System.IO.Ports.dll in the output folder after building. I tried adding a console application to the solution (also .NET Core 3.1 then changed to .NET5), a copy of System.IO.Ports.dll IS being copied to the output folder. But the bare bone test solution does copy System.IO.Ports.dll to its output folder as expected, and in that it is a class library that has the dependency to System.IO.Ports. Even if I add a new class library to the solution without adding any project references to its dependencies, no System.IO.Ports.dll is copied.

I add the System.IO.Ports 5.0.0 package to the project using "Manage NuGet Packages..." on the project in Solution Explorer.

I hope I can do some kind of resetting or cleanup of the solution to fix it, but I have no idea what to do. For now I just need it to work on Windows.

I could also try creating the whole application from the ground up again and start adding the source files and dependencies until the problem shows again. But for now I hope for some help :-)

It could very well be a trivial thing in the solution/project settings/configuration somewhere. I'm not strong in that part of .net development.

I'm not sure if it has anything to do with my problem, but I'm not sure if the .NET 5 SDK is 100% correct installed, because I don't have .NET5 project templates (and I don't know if I should have). I create projects of the ".NET Core" type and after creation I change the target framework to .NET 5.0. I did a complete reinstall, and it's the same. Everything else seems to work...

Cheers, Jan

UPDATE

I might have confused things by falsely believing System.IO.Ports.dll should be copied to the output folder. I understand that it shouldn't.

I dont get any problems at build time. Everything works there. The error I get at runtime, and the reason I started experimenting, is this:

System.IO.FileNotFoundException
  HResult=0x80070002
  Message=Could not load file or assembly 'System.IO.Ports, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
   at System.Reflection.RuntimeMethodInfo.get_Signature()
   at System.Reflection.RuntimeMethodInfo.FetchNonReturnParameters()
   at System.Reflection.RuntimeMethodInfo.GetParameters()
   at Newtonsoft.Json.Serialization.DefaultContractResolver.GetCallbackMethodsForType(Type type, List`1& onSerializing, List`1& onSerialized, List`1& onDeserializing, List`1& onDeserialized, List`1& onError)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveCallbackMethods(JsonContract contract, Type t)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract(JsonContract contract)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
   at StepBro.Core.Parser.StepBroListener.CreateVariableContainerObjectInitAction(Type objectType, PropertyBlock properties, ErrorCollector errors, IToken startToken) in C:\SW_development\Private\StepBro\source\StepBro.Core\Parser\StepBroListener.cs:line 277

I have now tried re-installing the .NET5 runtime but gets the same error. Should I also uninstall the SDK or should I only install one of them ? I can see two SDK installations in the Apps & features: MS .NET SDK 5.0.102 (x64) and one with the postfix "from Visual Studio".

UPDATE #2

It would be best if I created a new question, but here's the latest update on my discoveries.

System.IO.Ports is being used by my library StepBro.Streams. StepBro.Streams is loaded dynamically by StepBro.Core which is used by the WPF application. I get the above error the moment an instance of one of the types in StepBro.Streams is created by StepBro.Core (through Newtonsoft.Json).

I'm not stuck anymore, because I have temporarily added a reference to System.IO.Ports from StepBro.Core. That way, the assembly is already loaded the moment reflection is used to create a type from StepBro.Streams (or System.IO.Ports).

So, there is a problem, when dynamically loading the assembly, and I have no idea what to do about it after all the reinstallations and different experiments I have done. Maybe it's a problem with my VS2019 solution, with my installation of .NET5 or indeed a bug in .NET5.

c#
.net
wpf
visual-studio-2019
.net-5
asked on Stack Overflow Feb 8, 2021 by Tannen • edited Feb 9, 2021 by Tannen

1 Answer

1

Could not load file or assembly 'System.IO.Ports, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.

The reflection loader is trying to load a System.IO.Ports.dll assembly and failing. I don't know what all that non-sense in comments about not needing the assembly binary to exist is, but the loader disagrees with both of you.

System.IO.Ports is being used by my library StepBro.Streams

Then you packaged your library wrong. You want to export the dependency so that it is inherited by the host project.

Once that is done, the only remaining issue is that, because you never actually use the types exposed by the ports assembly directly, the compiler doesn't know that you actually use the library, so it will omit it from the generated binaries. You want to at least reference something in that assembly at some point, even a simple typeof(SerialPort).

answered on Stack Overflow Feb 9, 2021 by Blindy

User contributions licensed under CC BY-SA 3.0