InternalsVisibleTo attribute with signed assemblies in .NET 6

425 views Asked by At

I have a .NET 6 project called A.dll that is strongly signed. I also have a test project called A.Test.dll that must have access to the internals of A.dll. This means A.Test.dll must be strongly signed and the InternalsVisibleTo attribute must be used in A.dll. But I am unable to make this work. I use the SDK style project files.

I have set these properties in a common file for both A.dll and A.Test.dll:

<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\Key.snk</AssemblyOriginatorKeyFile>

I can build the two projects and the strong name of the A.test.dll project looks like this: A.Test, Version=1.2.3.4, Culture=neutral, PublicKeyToken=01234567890abcdef.

In A.csproj I do this:

   <ItemGroup>
      <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
         <_Parameter1>A.Test</_Parameter1>
      </AssemblyAttribute>
   </ItemGroup>

and I get the following error:

error CS1726: Friend assembly reference 'A.Test' is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations.

I want to add the public key token, and I do it as follows:

   <ItemGroup>
      <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
         <_Parameter1>A.Test, PublicKeyToken=0123456789abcdef</_Parameter1>
      </AssemblyAttribute>
   </ItemGroup>

This results in the following error:

error CS1726: Friend assembly reference 'A.Test, PublicKeyToken=0123456789abcdef'
is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations.

If I add the entire name, as follows:

   <ItemGroup>
      <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
         <_Parameter1>A.Test, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0123456789abcdef</_Parameter1>
      </AssemblyAttribute>
   </ItemGroup>

then I get the following error:

error CS1725: Friend assembly reference 'A.Test, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0123456789abcdef' is invalid. InternalsVisibleTo declarations cannot have a version, culture, public key token, or processor architecture specified.

So what is the right way to do this?

0

There are 0 answers