August 9, 2012

CRM 2011 plugin registration error “Inheritance security rules violated by type”.

When trying to register a plugin using an external dll ILMerged into it I came across the following error:

Unhandled Exception: System.TypeLoadException: Inheritance security rules violated by type: 'XXX.YYY.ZZZ.Plugins.ABC'. Derived types must either match the security accessibility of the base type or be less accessible.
at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
at System.Reflection.RuntimeAssembly.GetExportedTypes()

I remembered I had this issue before. The reason is that some legacy external libraries don’t follow .NET 4.0 security rules. The solution it actually quite simple – revert back to .NET 2.0 security. Simply add the following attribute anywhere inside you’re code (after the using statements):

[assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]