我正在尝试将AutoMapper与在 IIS 7 上运行的 Web 应用程序一起使用。预期使用它以便将外部 dll 中定义的域类型映射到 IIS 应用程序中定义的视图模型。这工作正常,除非外部 dll 被烧毁。然后我收到以下错误:
AutoMapper.AutoMapperMappingException was unhandled by user code
Message="Trying to map TestLibrary.Source to WebApplication1.Destination.\nUsing mapping configuration for TestLibrary.Source to WebApplication1.Destination\nException of type 'AutoMapper.AutoMapperMappingException' was thrown."
Source="AutoMapper"
StackTrace:
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
at AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType)
at AutoMapper.MappingEngine.Map[TSource,TDestination](TSource source)
at AutoMapper.Mapper.Map[TSource,TDestination](TSource source)
at WebApplication1._Default.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: AutoMapper.AutoMapperMappingException
Message="Trying to map TestLibrary.Source to System.Object.\nUsing mapping configuration for TestLibrary.Source to WebApplication1.Destination\nDestination property: Value\nException of type 'AutoMapper.AutoMapperMappingException' was thrown."
Source="AutoMapper"
StackTrace:
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
InnerException: System.Security.SecurityException
Message="Request failed."
Source="Anonymously Hosted DynamicMethods Assembly"
StackTrace:
at lambda_method(ExecutionScope , Object )
at AutoMapper.Internal.PropertyGetter.GetValue(Object source)
at AutoMapper.Internal.MemberGetter.Resolve(ResolutionResult source)
at AutoMapper.PropertyMap.ResolveValue(Object input)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap)
InnerException:
重现问题的步骤:
1) 在安装了 IIS 7 的机器上使用 Visual Studio 2008 创建一个新的 Web 应用程序。(我们使用的是 Windows 7)。
2) 从http://www.codeplex.com/AutoMapper下载 AutoMapper.dll 。(我们使用的是 0.4.xx 版本)并将对该 Dll 的引用添加到您的 Web 应用程序中。
3) 在默认页面后面的代码中加入如下代码:
using System;
using AutoMapper;
using TestLibrary;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Mapper.CreateMap<Source, Destination>();
Mapper.AssertConfigurationIsValid();
var source = new Source {Value = "Automapper works!" };
var destination = Mapper.Map<Source, Destination>(source);
Response.Clear();
Response.ContentType="text/plain";
Response.Write(destination.Value);
Response.End();
}
}
public class Destination
{
public object Value { get; set; }
}
4)新建一个名为“TestLibrary”的类库,将Class1.cs文件重命名为Source.cs,在里面放入如下代码:
namespace TestLibrary
{
public class Source
{
public object Value { get; set; }
}
}
5) 将对该库的引用添加到您的 Web 应用程序。
6) 运行解决方案,您应该会看到“Automapper 有效!” 输出。
7)要让它失败,你必须做两件事。
i) 将网站配置为在 IIS 而非 Visual Studio 开发服务器下运行。ii) 签署 TestLibrary 程序集。之后,运行解决方案应该会产生上面报告的错误。
有谁知道如何解决这个问题?根据 IIS 管理控制台,我们已经检查并且应用程序正在以完全信任的方式运行。