Strange Behavior with InternalsVisibleTo

I ran into an interesting situation the other day while using the InternalsVisibleToAttribute.  Our team is developing three libraries as part of our current project, and all three of them are signed with the same key.  Assembly A makes its internals visible to Assembly B and C.  Assembly A and B build successfully, but Assembly C fails with the error:

Friend access was granted to ‘AssemblyC, PublicKey=0024000004800000…’, but the output assembly is named ‘AssemblyC, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null’. Try adding a reference to ‘AssemblyC, PublicKey=0024000004800000…’ or changing the output assembly name to match.

We obviously can’t add a reference to Assembly C in Assembly A, as that would cause a circular dependency.  The only clue we could get out of that error message is the PublicKeyToken=null bit.  Why would the PublicKeyToken be null if the assembly is signed?  I wanted to confirm that the assembly was signed with Reflector or sn.exe, but there was no dll to check because the build failed.  After commenting out all of the code that accessed the internal parts of Assembly A, we got Assembly C to build and confirmed that it was indeed signed.  From the output window, we saw that the “WorkflowCompilation” task was executing and failing, and that the “CoreCompile” wasn’t even running.  It seems that the compilation fails because Assembly C is not yet signed when the compiler attempts to verify the InternalsVisibleTo attribute on Assembly A.  We managed to get the project to build by adding the AssemblyKeyFile attribute to it:

[assembly: AssemblyKeyFile(“MyKey.snk”)]

This produces the warnings “Use command line option ‘/keyfile’ or appropriate project settings instead of ‘AssemblyKeyFile'” and “Option ‘keyfile’ overrides attribute ‘System.Reflection.AssemblyKeyFileAttribute’ given in a source file or added module” that we have to ignore.  I’ve found this problem to happen to any workflow assemblies that attempt to access the internals of another assembly.  I’ve submitted a bug report about it here.

Advertisements

One thought on “Strange Behavior with InternalsVisibleTo

  1. InternalsVisibleToAttribute does not cause circular depndency.
    It only let’s the assemblies allowed to see stuff marked internal, as if they were public, but if referenced in the references.
    It basicly works like you would write an assembly with public stuff, only that it won’t be visible to 3rd party software, just those signed with the proper keys.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s