.NET · C# · Dynamics CRM · Plugins · Unit Test · Workflow

Unit Test: InternalsVisibleTo

Over the years of using Unit Test, I have found that it improves the quality of my work significantly. I strongly recommend every developer should use Unit Test whenever they write code.

So tying that to CRM, since the plugin/custom workflow assembly must be signed, in order to test ‘internal’ methods, the Test project must be allowed to see those methods via InternalsVisibleTo assembly attribute.

This attribute is to be added in the AssemblyInfo.cs of the Plugin project. It should look like this.

using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("MyPlugin.UnitTest,PublicKey=00240000048000009400000006020000002400005253413100040000010001000376b553affe0be362cea5142cee0e998c01c766e8fb058c1c9c9fb457fa1238825f2f571fb3c37088aa158756b077e7082ea8e0b780f95a4eaabf3fbea67c61f75010901e9debecfe81b9fd82af02366e36d917a15855c322838f08bb38ca123788abe3eb9b7650ca42b5a63db318459e128a00f5b544de12b31f14019cf38e")]

Notice that we need the PublicKey, not the PublicKeyToken.

The steps to do this are:
1. Sign the plugin project
2. Sign the test project using the same strong key, I normally create a strong key file used by the entire solution
3. Extract the public key from the strong key .snk file
sn –p strongkeyfile.snk strongkeyfile.snk.publickey
4. Show the public key from the .publickey file
sn -tp strongkeyfile.snk.publickey
5. Copy the public key and add the InternalsVisibleTo attribute to your AssemblyInfo
6. Don’t forget to add the namespace using System.Runtime.CompilerServices;

PS: If you had trouble finding the sn.exe executable, try the following path
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\sn.exe



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