Search code examples
c#.net-standard

.NET Standard: dependency not copied to output directory


I'm converting a .NET Framework library to .NET Standard 2.0, because I need to use it from both an old .NET Framework project and a modern .NET 5 project

In the original library, I use the ProtectedData class, which is part of the .NET Framework (from the System.Security.Cryptography assembly).

This is not part of the base .NET Standard library, so to make it work I've added the appropriate NugetPackage, System.Security.Cryptography.ProtectedData.

With this, the project compiles correctly, however when I try to run it I get the following error:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Security.Cryptography.ProtectedData, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

So I looked in the compilation output directory, and I see that there is no trace of a System.Security.Cryptography.ProtectedData.DLL file, its like the compilation system is not copying the dependency to the output directory. How can I fix this?


Solution

  • Found this answer on another forum, and it seems to work.

    I added:

    <PropertyGroup>
       ...
       <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    

    to the .csproj file and now dependencies are being copied correctly.

    Still would like an explanation as to WHY this is needed, since I never had to do this before for other projects...