gpt4 book ai didi

asp.net-mvc-3 - System.ComponentModel.DataAnnotations 自动 Controller 单元测试中的 MethodAccessException,MVC

转载 作者:行者123 更新时间:2023-11-28 20:12:40 26 4
gpt4 key购买 nike

首先,我想说我不会怀疑这是否是一个配置错误,因为它只发生在我们的 CI 构建服务器上,而不是在任何人的本地开发机器上。因此,如果有人有看似明显的配置建议,请随时指出,我的团队没有配置构建服务器,所以可能有些地方没有正确设置。

我的一个自动化测试遇到了问题,我在任何涉及正在使用的实体上的 (System.ComponentModel.)DataAnnotations 的测试中都遇到了这个问题。它似乎只发生在涉及 Controller 调用 TryUpdateModel 的任何测试中。

在我提供完整的错误消息之前,这里列出了我们已经尝试过的一些事情:

  • 将主要 MVC 程序集添加到测试项目的 app.config 的“fullTrustAssemblies”部分
  • 将 System.ComponentModel.DataAnnotations 程序集添加到测试项目的 app.config 的“partialTrustVisibleAssemblies”部分
  • 在代码中将 System.ComponentModel.DataAnnotations 程序集添加到 AppDomainSetup PartialTrustVisibleAssemblies
  • 尝试使用 ReflectionPermission.Demand 来查看是否存在权限问题
  • 确保机器上安装了 .NET Framework 4 和 MVC3
  • 正在检查注册表项 talked about in this article

这是完整的错误:

Test method qTrade.UnitTests.Web.Controllers.Maintenance.TypeLookupControllerTest.CreatePostSaveInvalidEntityAndModelState threw exception: 
System.MethodAccessException: 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=0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9' to the the PartialTrustVisibleAssemblies list when creating the AppDomain

Test method qTrade.UnitTests.Web.Controllers.Maintenance.TypeLookupControllerTest.CreatePostSaveInvalidEntityAndModelState threw exception:
System.MethodAccessException: 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=0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9' to the the PartialTrustVisibleAssemblies list when creating the AppDomain.<br> at System.Web.Mvc.TypeDescriptorHelper.Get(Type type)
at System.Web.Mvc.ModelBinders.GetBinderFromAttributes(Type type, Func`1 errorMessageAccessor)
at System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType, IModelBinder fallbackBinder)
at System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType, Boolean fallbackToDefault)
at System.Web.Mvc.Controller.TryUpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
at qTrade.UI.Web.Controllers.Maintenance.TypeLookupController.Create(FormCollection collection) in d:\cibuild\qTrade\qTradeCI\Sources\source\UI\qTrade.UI.Web\Controllers\Maintenance\TypeLookupController.cs:line 55
at qTrade.UnitTests.Web.Controllers.Maintenance.TypeLookupControllerTest.CreatePostSaveInvalidEntityAndModelState() in d:\cibuild\qTrade\qTradeCI\Sources\source\Tests\qTrade.UnitTests\Web\Controllers\Maintenance\TypeLookupControllerTest.cs:line 178
at System.Web.Mvc.TypeDescriptorHelper.Get(Type type)
at System.Web.Mvc.ModelBinders.GetBinderFromAttributes(Type type, Func`1 errorMessageAccessor)
at System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType, IModelBinder fallbackBinder)
at System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType, Boolean fallbackToDefault)
at System.Web.Mvc.Controller.TryUpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
at qTrade.UI.Web.Controllers.Maintenance.TypeLookupController.Create(FormCollection collection) in d:\cibuild\qTrade\qTradeCI\Sources\source\UI\qTrade.UI.Web\Controllers\Maintenance\TypeLookupController.cs:line 55
at qTrade.UnitTests.Web.Controllers.Maintenance.TypeLookupControllerTest.CreatePostSaveInvalidEntityAndModelState() in d:\cibuild\qTrade\qTradeCI\Sources\source\Tests\qTrade.UnitTests\Web\Controllers\Maintenance\TypeLookupControllerTest.cs:line 178

如有任何建议,我们将不胜感激,谢谢!

最佳答案

我解决了这个问题!

这是在 Medium Trust 中运行、在 GAC 中安装 MVC 以及使用“动态”(或形状)作为 ViewModel 的组合。只有(且仅当)这 3 个条件为真时,您才会遇到此错误。解决方案是更改您的 Controller 代码:不要返回“View(model)”,而是返回“View((object)model)”。

关于asp.net-mvc-3 - System.ComponentModel.DataAnnotations 自动 Controller 单元测试中的 MethodAccessException,MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6362616/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com