Adding reference to an assembly from .NET Standard 2.0 project causses error

-2

I have a .NET Standard 2.0 class library and I want to add a reference to System.DirectoryServices. Everything looks ok, but at runtime I got this error:

Cannot load a reference assembly for execution

The detail description is this:

Could not load file or assembly 'System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058)

My project "p1StandarClasese" which is .NetStandar ver. 2.0, automatic added a reference to a DLL "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.8\System.DirectoryServices.dll"

that references was automatic add by the visual studio editor, the VS detected that the code I wrote need that Assembly an asked to me for added, so I said yes... and the references was added

I really don't understand, I guess a .NET Standard 2.0 can not reference to a .NET assembly of that version? If this is the case - any suggestions? How I can use that Active Directory assembly? I need it for authentication purpose. Is there a nugget to solve this?

thaks for the help

c#
.net-standard
asked on Stack Overflow Feb 10, 2021 by KillemAll • edited Feb 11, 2021 by KillemAll

1 Answer

0

This is due to the dependent project having a lower version than the requested version, or using the wrong DLL for the reference.

See compatibility chart to confirm: https://docs.microsoft.com/en-us/dotnet/standard/net-standard

Based off your question my assumption is that you're using a .Net Standard v2.0 class library and referencing version 4.0.0 of System.DirectoryServices within your project definition.

If this is incorrect, can you provide the .Net version for each of the projects in your solution and where the reference takes place? The error message should have additional context to determine how you're referencing.

You can always remove the reference and redo the reference using NuGet to ensure a proper package is generated.

answered on Stack Overflow Feb 10, 2021 by Seeds

User contributions licensed under CC BY-SA 3.0