'Inheritance security rules violated while overriding member' with EntLib 5.0 and .NET 4

Topics: Enterprise Library Core, General discussion, Logging Application Block
Feb 14, 2011 at 12:54 PM


I'm using EntLib 5.0 with .NET 4. It was working, but now I've used ILMerge in order to merge all the dlls in one redistributable .dll. Everything works but EntLib5. When I try to get a logger, I get this exception:

System.TypeLoadException was unhandled by user code
Message=Inheritance security rules violated while overriding member: 'Microsoft.Practices.Unity.ResolutionFailedException.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.
TypeName=Microsoft.Practices.Unity.ResolutionFailedException.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)
Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)
       at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`
1 resolverOverrides) in :line 0
Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) in :line 0
Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[T](IUnityContainer container, ResolverOverride[] overrides) in :line 0
Microsoft.Practices.Unity.UnityContainerExtensions.AddNewExtension[TExtension](IUnityContainer container) in :line 0
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.UnityContainerExtensions.AddNewExtensionIfNotPresent[TExtension](IUnityContainer container) in :line 0
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.Unity.UnityContainerConfigurator..ctor(IUnityContainer container) in :line 0
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.CreateDefaultContainer(IConfigurationSource configurationSource) in :line 0
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.CreateDefaultContainer() in :line 0
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.SetCurrentContainerIfNotSet() in :line 0
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.get_Current() in :line 0
My.Common.Diagnostics.Logger..ctor(Boolean failSilently) in D:\TFS Source\My.COMMON\Framework4.0\My.Common\Diagnostics\Logger.cs:line 35
My.Common.Tests.LoggerTest.LoggerTestMethod() in D:\TFS Source\My.COMMON\Framework4.0\My.Common.Tests\LoggerTests.cs:line 21

I've read that exception comes from .NET4 because now it's more strict in terms of coding security, but what can I do? download the EntLib 5 source code and fix it myself?

Is there any way to disable that check?

I've merged the files in this order, maybe it's the wrong order:



Feb 14, 2011 at 2:45 PM

Apparently the problem is when I use the switch for .NET 4.0, it looks like entlib is not compilant somehow

Feb 14, 2011 at 6:51 PM

Are you trying to merge the Entlib 5.0 binaries into an .EXE targeting .NET 4.0? In that case, I would expect that error - the Entlib 5.0 binaries target .NET 3.5 SP1.

You can rebuild Entlib from source targeting .NET 4.0. In that case, we use the assembly level attribute [assembly: SecurityRules(SecurityRuleSet.Level1)] to turn the .NET 4.0 security rules back down to the .NET 3.5 level. This scenario (recompiling .NET 3.5 code on .NET 4.0) is why that switch exists. Updating the Entlib code to be SecurityRuleSet.Level2 is going to be a pretty large undertaking, I wouldn't recommend it lightly.

The issue here, I think, is what does ILMerge do with assembly level attributes? Can you get that SecurityRules attribute onto the merged assembly somehow? If so, that would be the easiest thing to do.

Feb 15, 2011 at 10:51 AM

I see.  I was expecting to have backwards compatibility, but it makes sense that if you put everything in the same assembly, you cannot define separate versions.

ILMerge executable is very simple, but it can be used programatically as a library, so maybe I'll try to add the attribute myself later on.