Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly When Net Framework reference a Net Standard Library

Tags:

c#

.net

.net-core

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 !

like image 784
Steven Li Avatar asked Dec 05 '18 17:12

Steven Li


1 Answers

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>
like image 80
Ahmed Mansour Avatar answered Oct 13 '22 01:10

Ahmed Mansour