2
I have class library project in .net standard 2.0.3 where I’m using System.Security.Cryptography.Xml
to sign a xml document with a privateRSAkey.
var sign = GetXmlSign(doc, rsa);
private static XmlElement GetXmlSign(XmlDocument xml, AsymmetricAlgorithm key)
{
var signedXml = new SignedXml(xml) {SigningKey = key};
var refer = new Reference {Uri = ""};
reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
signedXml.AddReference(refer);
signedXml.ComputeSignature();
return signedXml.GetXml();
}
Now when I’m calling GetXmlSign(doc, rsa);
I’m getting the exception below.
System.IO.FileNotFoundException: ‘Could not load file or assembly ‘System.Security.Cryptography.Xml, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51′ or one of its dependencies. The system cannot find the file specified.’
NuGets
Note: There is no Nuget “System.Security.Cryptography.Xml” with Version=4.0.1.0
In addition to having a .NET Standard library you also have an application (like a console application) or perhaps a test project. The platform for the application determines what specific assembly referenced by your .NET Standard library to load.
So your library references
System.Security.Cryptography.Algorithms
4.3.0 however the actual version of the assembly to load for your platform may be 4.1.0 (that is the version you get on .NET Framework 4.6.1).So you need to inform your application to redirect the desired version (4.3.0) to the actual version for your runtime (4.1.0). You can do that in the
app.config
file. Remember that this file is used by the application and not the library. Adding anapp.config
file to your library will not make a difference.I tried to create a small project like the one you describe that in addition to a .NET Standard 1.4 library that references
System.Security.Cryptography.Algorithms
4.3.0 has a NET Framework 4.62 console application and I had to include anapp.config
file with the following contents for this to work:Anecdotally, this seems to be less of a problem if you switch to .NET Standard 2.0.