11

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:

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:

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)

rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • You violate the basic rule of a .NET Standard class library should not use any Windows specific dependencies (unless it really has to). The Windows Compatibility Pack (registry related for instance) should only be used if the launcher of the application is a .NET Core console, but in your case it is a .NET Framework console. Move all registry operations to your .NET Framework projects please, and then things should work flawlessly. – Lex Li Feb 08 '19 at 18:23
  • @LexLi -- The .NET Framework situation is some existing client assemblies that we're not going to refactor yet (other than to recompile with the new .NET Standard version of the library they've always referenced as .NET Framework). In other words, I'm converting one library from net472 to netstandard2.0. The reason is because I'm creating an ASP.NET Core web service which exposes some of the functionality in the class library and I'd rather keep the code base moving forward (i.e. .net standard) than leverage .NET Framework shims. – rory.ap Feb 09 '19 at 14:13
  • 1
    I literally worked on this question for 2 full days before posting it. Now more than ever, I'd love to know why I'm being down-voted. – rory.ap Feb 11 '19 at 13:55
  • you can have it work w/o any code change if you add something like this in your app.config (adapt versions and paths) ``, or hook the `AppDomain.CurrentDomainAssemblyResolve` event (untested) – Simon Mourier Feb 13 '19 at 10:38
  • @rory.ap I am guessing downvotes are due to artificial restrictions you are placing and they stand in your own way. Idea of netstandard is to be a clean widely adopted subset of features and thus it should really only depend on other standard libs. – aiodintsov Feb 16 '19 at 02:35
  • @SimonMourier thanks that worked! Too bad you didn't post it as an answer before the bounty ended, but you can post it now if you want. – rory.ap Feb 21 '19 at 17:06
  • Well, IMHO, you should have told me it worked before the bounty ended... – Simon Mourier Feb 21 '19 at 17:21
  • @SimonMourier I didn't have time to get back to this until today, but I would have if you had posted it as an answer so it would be before the bounty ended. Why post an answer in comments? I've never understood that. – rory.ap Feb 21 '19 at 17:27
  • Because you don't know if it's really an answer, yet. I post answers when I'm sure they are answers to the question. Maybe it wouldn't have worked for you for some reasons. Usually, OP tests if it works, replies and then the commenter converts to an answer. – Simon Mourier Feb 21 '19 at 17:30
  • An answer is an answer, even if it's one sentence and even if it ultimately doesn't work for the user. You can always delete it afterwords. – rory.ap Feb 21 '19 at 17:42

1 Answers1

1

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>
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298