我正在尝试将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.x.x)并在您的Web应用程序中添加对此Dll的引用。
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管理控制台。
答案 0 :(得分:2)
因此,此异常来自以编程方式创建lambda表达式然后编译它。你可以尝试在Default.aspx.cs中做同样的事情吗?一种方法是在代码中创建一个lambda表达式:
表达式&GT; expr =()=&gt; 5;
然后做:
var func = expr.Compile();
这就像你失败的那条线。
如果这样可行,我接下来会将此代码放入已签名的程序集中,并从Web应用程序访问它。