Accessing newly signed third party DLL gives error
.net, .net-assembly, dll, strongname
Solution
I had the exact same issue.
Why it happens
- The 3rd-party assembly is declared with `InternalsVisibleTo` to make it "friend" to other assemblies, e.g. `InternalsVisibleTo("OtherAssembly")`
- .NET requires that strong-name assembly can only be "friend" to other strong-name assemblies, in which case the `InternalsVisibleTo` attribute must specify the public keys of those other assemblies, e.g. `InternalsVisibleTo("OtherAssembly, PublicKey=[key]")`
- At runtime, the CLR sees that `InternalsVisibleTo` is not properly declared for the assembly in question, so it throws the exception.
How to fix
If the "friend" assemblies aren't needed for the program execution (e.g. it's a Test assembly, which isn't deployed in production), follow these steps:
- Disassemble the assembly in question: `ildasm.exe ThirdParty.dll /OUTPUT=ThirdParty.il`
- Use a text editor to edit the IL file, remove any declaration of `InternalsVisibleTo`
- Assemble and sign the IL: `ilasm.exe ThirdParty.il /DLL /OUTPUT=ThirdParty.modified.dll /KEY=key.snk`
- Note: generate a key by: `sn.exe -k key.snk`
If the "friend" assemblies are needed for the program execution, you have to sign all those friend assemblies. Then follow similar steps as above, except instead of removing `InternalsVisibleTo`, you have to amend each declaration with the correct public key.
Problem
I have a signed application that uses third party DLLs. These DLLs were not signed. - So far no problem for the first step: I just signed them (getting *.il with ildasm.exe, ajust publickeytoken in the *.il 's because they have interdependencies, and made the *.dll's with ilasm.exe) The project now compiles fine and also starts up. But when in my code, a class constructor of the 3rd-party-DLL is called (or something else? - was just the first thing I did), I get the error "Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations" It seems there won't be a problem if you have the source of the DLL and can ajust in AssemblyInfo.cs by setting ``` [assembly: InternalsVisibleTo("MyProject.Domain.Tests, PublicKey=..."] ``` But: As mentioned above I have a third-party DLL I don't have the source. So no way to solve the problem like this. Any suggestions to get this running?