2009-11-16 50 views
3

当我试图用AutoMapper与IIS 7上运行的Web应用程序旨在使用它,映射外部DLL中查看IIS应用程序定义的模型定义域类型。这工作正常,除了当外部的DLL烧结。然后,我得到以下错误:问题与AutoMapper在IIS7使用签名的程序集

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)创建使用Visual Studio 2008的计算机上安装了IIS 7新的Web应用程序。 (我们正在使用Windows 7)。

2)http://www.codeplex.com/AutoMapper下载AutoMapper.dll。 (我们使用的版本是0.4.x.x),并将一个对此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管理控制台。

+0

不知道这是否有帮助,但AutoMapper 1.0 RC1可用? – Kane 2009-11-16 18:12:49

+0

我也遇到了与AutoMapper 1.0 RC1相同的问题。 – Zen 2009-11-19 15:14:33

+0

这是非常奇怪的。我会看一看。 – 2009-11-20 01:57:28

回答

2

所以这个例外来自编程方式创建一个lambda表达式,然后编译它。你可以尝试做同样的事情,只需在Default.aspx.cs中?这样做的一种方法是在代码中创建一个lambda表达式:

表达式> EXPR =()=> 5;

然后执行:

变种FUNC = expr.Compile();

这是一条线,就像那样对你不合适。

如果一切正常,我那么接下来把这个代码在签名的程序集,并从Web应用程序访问它。

+0

对不起,这应该是一个评论:P – 2009-12-23 22:54:44

+0

吉米,我试过你建议的代码,包括调用编译函数,它工作正常。我甚至把代码放在一个签名的程序集中,并从Web应用程序调用它。再次,没有问题。但是,automapper调用仍然失败。 更有趣的是,在假期没有访问我的主机,我在服务器2008 VM上做了一些IIS 7的测试,我坐在这里并且automapper在那里工作的很好。我同意这是IIS 7的CAS问题。 – 2010-01-11 12:59:11

+2

更多信息:通过试验和错误发现,如果将AllowPartiallyTrustedCallers属性放在TestLibrary程序集上,问题就会消失。我对此解决方案并不是100%满意,因为显然其他人(包括我在Server 2008中)不需要这样做来获得相同的代码。所以我还不清楚这里究竟发生了什么。 – 2010-01-11 13:33:27