I am calling ModelMetadataProviders.Current.GetMetadataForProperties to fetch list of ModelMetadata in WPF application with Visual Studio 2010. This call executes fine and I get IEnumerable<> in return. At the code line, where I try to iterate over this, I get System.MethodAccessException.
Funny thing is it happens only if I am debugging with F5. If I run code with Ctrl+F5, then I do NOT get this exception and code works fine. If I run exe from windows explorer, it runs fine as well.
Surely, I am missing something. May be, System.Web.Mvc cannot used from WPF app? (why) Is there something about visual studio debugger?
Any help will be appreciated.
Here is whole exception:
Attempt by security transparent method 'System.Web.Mvc.TypeDescriptorHelper.Get(System.Type)' to access security critical method 'System.ComponentModel.DataAnnotations.AssociatedMetadataTypeTypeDescriptionProvider..ctor(System.Type)' failed.
Assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is a conditionally APTCA assembly which is not enabled in the current AppDomain. To enable this assembly to be used by partial trust or security transparent code, please add assembly name 'System.ComponentModel.DataAnnotations, PublicKey=002400000480000094000开发者_StackOverflow中文版0000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9' to the the PartialTrustVisibleAssemblies list when creating the AppDomain.
This is a known bug in System.ComponentModel.DataAnnotations.dll which will be fixed for the next release of the .NET Framework. In short, the DataAnnotations assembly is marked as conditionally APTCA since it was originally intended for use only by ASP.NET applications. The assembly has become more useful so that it can be consumed outside of ASP.NET, but we forgot to remove the conditional APTCA flag from the assembly. This causes transparent DLLs (like System.Web.Mvc.dll) to fail when accessing this assembly outside of an ASP.NET application in certain AppDomains, such as those instantiated by the VS debugging process.
To answer your other question: System.Web.Mvc.dll was never tested for use outside of an ASP.NET application. It has several hooks into System.Web.dll and other related assemblies, so it's not too surprising that it would blow up at some point.
Thanks for information, Levi.
As I could not use DataAnnotations becuase of said bug, I did some coding of my own - which involves reflection. And I faced problem :-( with CompareAttribute when calling GetCustomAttributes.
But, then I found a workaround.
It seems that issue is with Visual Studio Hosting Process.
If I disable visual studio hosting process, both problems go away!
精彩评论