Monday, August 14, 2017

InternalsVisibleTo with Strongly-Type Assemblies and Shared Assembly (SharedAssemblyInfo)



InternalsVisibleTo with Strongly-Type Assemblies


For testing, use the InternalsVisibleTo for unit tests. It looks like this:

[assembly: InternalsVisibleTo("MyProject.Domain.Tests")]

When the assembly is strongly typed, though, this simple statement will not work, and we get the following message:


Error: "Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations"
To fix this, you must do two things: You must sign the test assembly, and you must include the public key in the InternalsVisibleTo statement. 

To get the public key value from the .snk file, use the sn.exe utility included in the .NET SDK. You must do two things, first, create a .snk file that contains only the public key:

sn -p MyKey.snk MyKey.PublicKeyOnly.snk

Then, you can extract the public key value with this command(visual studio command):

sn -tp MyKey.PublicKeyOnly.snk

This will produce a similar output:

Public key is
0024000004800000940000000602000000240000525341310004000001000100cfb8bc23b86a08
e70d021dd53d3b0293e716e71015870bdcc58a0231a4228618851a83e06077f5a44f42beb2baf3
56ad2d344521a96b0081ed0f25f9227523e3625eda524efe1cf2e1e5e41f3693a76ec52347684b
8129a4bb2d5fc49681adf33da0eecc4f81f011af4539d12abe1b4e760b5ce32d766db1012d4402
8381f0b4

Public key token is 2ff2b71993eeff95

Copy the public key value and update the InternalsVisibleTo:

[assembly: InternalsVisibleTo("MyProject.Domain.Tests, PublicKey=
0024000004800000940000000602000000240000525341310004000001000100cfb8bc23b86a08

e70d021dd53d3b0293e716e71015870bdcc58a0231a4228618851a83e06077f5a44f42beb2baf3
56ad2d344521a96b0081ed0f25f9227523e3625eda524efe1cf2e1e5e41f3693a76ec52347684b
8129a4bb2d5fc49681adf33da0eecc4f81f011af4539d12abe1b4e760b5ce32d766db1012d4402
8381f0b4")]

OK! You can now access the internals from your test project for strong named assemblies.

To MOQ internal interface add below in main project
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]


Shared Assembly (SharedAssemblyInfo.cs) -- Add as Link to individual project properties

using System.Reflection;
using System.Runtime.InteropServices;


[assembly: AssemblyCompany("BSS")]
[assembly: AssemblyCopyright("Copyright © BSS")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

AssemblyInfo.cs 

sing System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;


// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("BSS.SubProj.Web")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyProduct("BSS.SubProj.Web")]
#if DEBUG
[assembly: InternalsVisibleTo("BSS.SubProj.Web.Tests, PublicKey = 0024000004800fdsgdsgssdsdfgsdfgsdgsdfgsdfgsdsdfgfg")]
#endif

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("x5b0b566-c76b-8365-k2e4-9c54430e24xy")]


--------------------------------------------------
Getting Public Key Token of Assembly Within Visual Studio
Tools ---> external tools 

Click the Add button and add the following:
Title: Get &PublicKeyToken
Command: C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\sn.exe
Arguments: -Tp $(TargetPath)
Check the Use Output Window checkbox. 
-----result----
click toos--> Get &PublicKeyToken    in each project --- out put window will come public key





No comments:

Post a Comment

Encrypt/Decrypt the App.Config

Program.cs using System; using System.Diagnostics; using System.IO; namespace EncryptAppConfig {     internal class Program     {         pr...