I have a class library [AllowPartiallyTrustedCallers] containing subtypes of System.DataAnnotations.ValidationAttribute. The library is used for WCF service contract types.
In .NET 2 / 3.5, this worked fine. Since .NET 4.0, however, starting the service client in the Visual Studio debugger throws the exception "Inheritance Security Rules Violated by Type:" (my subtype is ValidationAttribute). Derived types must either match the security of the underlying type or be less accessible. " (System.TypeLoadException)
An error appears only when all of the following conditions are true:
- The ValidationAttribute subclass is in the AllowPartiallyTrustedCallers Reflection assembly
- used to check attribute
- Visual Studio hosting process is enabled (Project Properties check box, Debug tab)
So, in Visual Studio .NET 2010:
- create a new console project,
- add a link to "System.ComponentModel.DataAnnotations" 4.0.0.0,
- write the following code:
.
using System; [assembly: System.Security.AllowPartiallyTrustedCallers()] namespace TestingVaidationAttributeSecurity { public class MyValidationAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute { } [MyValidation] public class FooBar { } class Program { static void Main(string[] args) { Console.WriteLine("ValidationAttribute IsCritical: {0}", typeof(System.ComponentModel.DataAnnotations.ValidationAttribute).IsSecurityCritical); FooBar fb = new FooBar(); fb.GetType().GetCustomAttributes(true); Console.WriteLine("Press enter to end."); Console.ReadLine(); } } }
- Press F5 and you will get an exception!
Press Ctrl-F5 (start without debugging) and all this works fine without exception ...
The strange thing is that ValidationAttribute will or will not be critical depending on how you run the program (F5 or Ctrl + F5). As shown in Console.WriteLine in the above code. But then again, it looks like other attributes (and types?) Too.
Now questions ...
Why do I have this behavior when inheriting from ValidationAttribute, but not when inheriting from System.Attribute? (Using Reflector I don't see any special settings for the ValidationAttribute class or assembly)
And what can I do to solve this? How can I save MyValidationAttribute inheriting from ValidationAttribute in the AllowPartiallyTrustedCallers assembly without labeling it with SecurityCritical, still using the new .NET 4 security model level 2 and still working using the VS.NET debug host (or other hosts)?
Thank you so much! Rudi
codetuner
source share