Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetFramework app referencing NetFramework library in same solution referencing NetStandard library in another soln.: could not load file or assembly

There are many similar questions about problems referencing a .NET Standard class library from a .NET Framework project where a NuGet package dependency in the netstandard library doesn't flow to the netframework app, and the Could not load file or assembly error occurs at runtime:

enter image description here

Many sources exist, like the one below, that indicate this can be resolved by adding the missing dependency on the netframework project:

  • https://stackoverflow.com/a/46015829/2704659

This is unfavorable, however, because I don't want to have projects have to carry around direct references that they shouldn't require; the dependencies should flow naturally so future added/removed dependencies just work.

Other sources indicate that it can be resolved by adding <RestoreProjectStyle>PackageReference</RestoreProjectStyle> and <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> to the netframework project file:

  • https://stackoverflow.com/a/53654690/2704659
  • https://stackoverflow.com/a/53732075/2704659
  • https://www.hanselman.com/blog/ReferencingNETStandardAssembliesFromBothNETCoreAndNETFramework.aspx

I've tested both of the above fixes with projects that reside within the same Visual Studio solution and had success, but I prefer the second approach because it's a "set it and forget it" solution.

The problem I've found is when I try to reference a netstandard class library from a netframework project in another VS solution and I use the <RestoreProjectStyle>PackageReference</RestoreProjectStyle> and <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>approach in the latter project. In my specific case, I have a .NET Framework executable project that references a .NET Framework class library in the same solution, and that class library references a .NET Standard class library in another solution.

I've created an MCVE on GitHub that demonstrates this behavior. I'm using VS 2017 v15.9.4.

Other than always adding the packages directly on the netframework project, is there a way to get this working?


(Note: it sounds similar to the problem here, but I'm not using "click once": https://stackoverflow.com/a/47839628/2704659)

like image 638
rory.ap Avatar asked Feb 08 '19 16:02

rory.ap


People also ask

What is Netstandard library?

NET Standard allows a library to use more APIs but means it can only be used on more recent versions of . NET. Targeting a lower version reduces the available APIs but means the library can run in more places.

What is the difference between creating .NET STD library and .NET Core library?

NET Core, you can build cross-platform console apps and ASP.NET Core Web applications and cloud services. . NET Standard: This is the set of fundamental APIs (commonly referred to as base class library or BCL) that all . NET implementations must implement.

Can we use .NET Core library in .NET framework?

NET Framework can only run in . NET Framework based applications and the libraries which target . NET Core can only run in . NET Core compatible applications.

How do I add a reference to .NET Core library?

Add Class Library Reference To use a class library in your application, you must add a reference to the library to access its functionality. Right click on the project name of your console app in Solution Explorer and select Add ->Reference option.


1 Answers

You can have it work without any code change if you add something like this in your app.config (adapt versions and paths to your context)

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly> 
            <assemblyIdentity name="Microsoft.Win32.Registry" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /><codeBase version="4.1.1.0"
                href="C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.win32.registry\4.5.0\lib\net461\Microsoft.Win32.Registry.dll"/>
        </dependentAssembly>
    </assemblyBinding>
</runtime>
like image 180
Simon Mourier Avatar answered Oct 16 '22 07:10

Simon Mourier