0

I have a .net standard 2.0 library that I want to use to access the registry via the windows compatibility pack (if the OS its running on is Windows)

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
     Console.WriteLine("On windows.");
     var dropFolder = Microsoft.Win32.Registry.GetValue(keyName: $@"{userRoot}\{subkey}", valueName: "DropFolder", defaultValue: @"C:\somepath");
     Console.WriteLine($"Drop folder is {dropFolder}");
 }

This crashes with an exception

Managed Debugging Assistant 'BindingFailure' : 'The assembly with display name 'Microsoft.Win32.Registry' failed to load in the 'Load' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Win32.Registry, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

I have installed the Windows Compatibility nuget package into my .net standard project. I have also tried installing the Microsoft.Win32.Registry nuget package.

The error perists.

If I comment out the lines

var dropFolder = Microsoft.Win32.Registry.GetValue(keyName: $@"{userRoot}\{subkey}", valueName: "DropFolder", defaultValue: @"C:\somepath");
Console.WriteLine($"Drop folder is {dropFolder}");

It works fine.

I am referencing the .net standard library from a .net framework library so no option to go to .net core.

The .dot net standard cs proj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.0" />
    <PackageReference Include="MQTTnet" Version="2.8.2" />
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
    <PackageReference Include="RabbitMQ.Client" Version="5.1.0" />
  </ItemGroup>

</Project>
Stuart
  • 3,949
  • 7
  • 29
  • 58
  • 1
    Did you install the Microsoft.Windows.Compatibility package also in your .NET Framework library? – mm8 Aug 10 '18 at 11:18
  • That fixed it! Why on earth would this need to be added to the .net framework project!! @mm8 If you add your comment as an answer I'll mark it as the solution. – Stuart Aug 10 '18 at 11:23
  • 1
    This is not a crash, merely a notification from a debugging assistant. The BindingFailure mda is normally turned off, it is apt to give false warnings on retargetable assemblies. You need to fix your debug settings, do not do anything drastic yet. Debug > Windows > Exception Settings, "Managed Debugging Assistants" node. If it is ticked now then click until it turns into a square. Or if necessary expand the node and untick "BindingFailure". – Hans Passant Aug 10 '18 at 11:26

1 Answers1

1

For the missing binary to be included in the output of your .NET Framework project, you should also install the Microsoft.Windows.Compatibility package in this project. Or at least install the Microsoft.Win32.Registry package.

mm8
  • 163,881
  • 10
  • 57
  • 88