unitycontainer / interception

Unity.Interception package
Apache License 2.0
22 stars 18 forks source link

Unity Interception and strong named asseblies won't work #4

Closed ENikS closed 6 years ago

ENikS commented 6 years ago

@mizasie wrote:

Hello,

We use strong named assemblies and internal interfaces/classes in our solutions. Unfortunately it is not possible to use Unity Interception in that case. It creates a dynamic assembly which is not signed and therefore not loaded. We figured out how easily that could be implemented. We did the change locally and rebuild Unity Interception by ourselves. It works great in our particular case.

Important hint: This is only an issue, if you work with internal interfaces/classes. You have to add the following line to your AssemblyInfo.cs

[assembly: InternalsVisibleTo("Unity_ILEmit_InterfaceProxies, PublicKey=002400000480000094000000060200000024000052534131000400000100010095d32d9c234a5c26a67e6f9e63c75501460914df0da0774b35cfd527aaaf774d8b751091c06d63e22d5704eafda93e7666bb7b446abfda77696aec773acff37d117ddd0e688a468505776c154915906181812fb3191b99d389dc5fc4faca70dc7113932ea239c9299edb068d157ab375e38408f54f1f9e69b69ae8e989daafb1")]

Have a look into the constructor without parameters: https://github.com/unitycontainer/unity/blob/master/source/Unity.Interception/Src/Interceptors/InstanceInterceptors/InterfaceInterception/InterfaceInterceptorClassGenerator.cs

We changed it into that:

[SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline",
    Justification = "Need to use constructor so we can place attribute on it.")]
static InterfaceInterceptorClassGenerator()
{
    var assemblyName = new AssemblyName("Unity_ILEmit_InterfaceProxies")
                           {
                               KeyPair = new StrongNameKeyPair(Resources.unity)
                           };

    AssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
        assemblyName,
#if DEBUG_SAVE_GENERATED_ASSEMBLY
        AssemblyBuilderAccess.RunAndSave);
#else
        AssemblyBuilderAccess.Run);
#endif
}

The key file itself could be found on the top level within the build folder. Its name is “unity.snk”. We imported that file as resource file in Visual Studio and named it unity. Therefore it’s possible to access the content with the command Resources.unity.

It would be great to have that functionality in the official builds too. Thanks for all your efforts.

Cheers Micha