How to use the InternalsVisibleTo attribute with a strongly named assembly?

I use the "InternalsVisibleTo" attribute with the assembly to expose the internal methods / classes in my unit test project.

Now I need to install this assembly in the GAC, so I need to give it a strong name. When I try to do this, I get the following error in Visual Studio.

Signature-bound assemblies must indicate the public key in their InternalsVisibleTo declarations

A little googling led me to the following article:

https://msdn.microsoft.com/en-us/library/bb763089.aspx

This article says:

"Define the public key to build the name with a strong name.

This article does not say how to determine the public key. Where can I find the public key for the assembly? Also, as soon as I have the public key, will this be the right way to declare an attribute?

[assembly: InternalsVisibleTo("Namespace.Assembly.Example.Name, PublicKey=ThePublicKey")] 
+23
c # .net-assembly
source share
3 answers

To use InternalsVisibleTo with a strictly signed assembly, your assembly β€œfriends” must also be signed. The public token of the test assembly must be specified as part of the value of InternalsVisibleTo .

Note that the attribute is not used to actually verify the assembly at compile time β€” it only indicates that running the -t ime check (and the -t ime check to build a friend) should check this identity. Therefore, if you just need to compile the main assembly, you can specify any public key token (for example, one of the Microsoft assemblies, as, for example, in all assembly references in your Web.Config).

Generally, since you will be signing assemblies, you will know the public key. Those. if you have a snk file, then sn -t youSnk.snk will show the public key. Or you can follow the steps sn -tp {path to assembly} public key in Visual Studio to configure VS to display the public token for any assembly that uses sn -tp {path to assembly} to get the public key from the assembly. (If the document is missing, the steps are copied to another answer to this question )

+17
source share

UPDATE May 2019: Works great with Visual Studio 2019.


For everyone using Visual Studio 2017, there is a final method:

In our favorite IDE, go to " Tools> External Tools ... " and " Add " a new tool with the following settings:

  • Title: Get PublicKey
  • Command: " C: \ Program Files (x86) \ Microsoft SDKs \ Windows \ v10.0A \ bin \ NETFX 4.6.2 Tools \ sn.exe " (/! \ Select the version of the NETFX tool that matches your version of the NETFX build, here it is 4.6.2)
  • Arguments: -Tp $ (TargetPath)
  • Check "Use output window"

Apply / OK these changes.

In Solution Explorer, click the build name of your project , and then go to Tools> Get PublicKey . The output window should display the (rather long) public key along with the token public key.

Finally, in the project that contains the inner class (ie the tested project) that you want to provide, open the file " AssemblyInfo.cs " and add the line:

[assembly: InternalsVisibleTo ("MyCompany.SolutionName.ProjectName, PublicKey = Insert your public key here")]

/! \ You must remove line breaks from your public key.

This worked great for me, so hopefully this helps you too!

+39
source share

If you are not testing your release configuration, this is an easy way:

 #if DEBUG [assembly: InternalsVisibleTo("TestProjNamespace")] #endif 
+1
source share

All Articles