13

I am very new to netstandard and I just encountered exception when I want to run a debug mode a .Net Framework (console) which has reference to a netstandard library.

The Solution Structure

Exception

So I later figured out that the exception would be gone if I installed the System.IO.Ports from nuget onto the .Net Framework Project. But this library has already installed onto netstandard project.

So I highly doubt that if I need to install all the libraries which netstandard project required if I uses the netstandard project from another .net framework project.

I must be missing something, can anyone who is familiar with netstandard give me some insights about it.

your inputs are much appreciated !

Steven Li
  • 754
  • 1
  • 8
  • 16
  • If you open Solution Explorer in VS and look at references not all references are loaded to keep executable small. You can always add missing reference by going to menu : Project : Add References : .Net and select missing reference which is in the Windows folder when the Net Library is installed. Adding a reference just tells compiler to include in build and does not actually load anything. System.IO.Ports is part of Net and not nuget. – jdweng Dec 05 '18 at 17:55
  • I have the same behavior in my application - package dependencies installed in the .NET Standard class library projects aren't automatically referenced in the consuming .NET Core or .NET framework apps. I just assumed that's how it is and that I need to install the dependencies there separately. – mason Dec 05 '18 at 17:59
  • Yeah, you must add references manually. – Alexander Petrov Dec 05 '18 at 21:58

1 Answers1

17

Open up the .csproj file for the .Net Framework project that is hosting the .Net standard library and add this line within the first <PropertyGroup> like this to change the restore style:

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

Check out this article by Scott Hanselman about the cause of the issue and the fix.

From the article:

The "full" Framework projects are using the older .csproj format and by default, they use package.config to manage dependencies. The newer projects can reference Packages as first-class references. So we need to tell ALL projects in this solution to manage and restore their packages as "PackageReferences."

EDIT:

This fix worked perfectly for me on a new project. When I apply it to the csproj before I restore packages in that new project, nuget gets the correct references.

When applying this to an existing project, it doesn't work. The libraries that are referenced inside the .net standard class library are not pulled by nuget and therefore, it would still throw the same error.

To fix it try this:

  1. Open the csproj. You will notice your library looking like that
<Reference Include="yournetstandardlibrary, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\packages\yournetstandardlibrary\lib\netstandard2.0\.dll</HintPath>
    </Reference>
  1. Delete that reference
  2. Add this reference instead
 <ItemGroup>
     <PackageReference Include="yournetstandardlibrary">
       <Version>1.0.1</Version>
     </PackageReference>   
  </ItemGroup>
Ahmed Mansour
  • 527
  • 5
  • 13