Iis 7 使用签名程序集时IIS7中的AutoMapper出现问题

Iis 7 使用签名程序集时IIS7中的AutoMapper出现问题,iis-7,automapper,signed,Iis 7,Automapper,Signed,我正在尝试使用IIS 7上运行的web应用程序。预期用途是将外部dll中定义的域类型映射到IIS应用程序中定义的模型。这可以正常工作,除非外部dll被烧毛。然后我得到以下错误: AutoMapper.AutoMapperMappingException was unhandled by user code Message="Trying to map TestLibrary.Source to WebApplication1.Destination.\nUsing mapping confi

我正在尝试使用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应用程序。(我们使用的是Windows7)

2) 从下载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; }
    }
5) 将对此库的引用添加到web应用程序中

6) 运行解决方案,您应该会看到“Automapper works!”输出

7) 要让它失败,你必须做两件事。
i) 将网站配置为在IIS而不是Visual Studio开发服务器下运行。 ii)签署TestLibrary组件。 之后,运行解决方案将产生上面报告的错误


有人知道怎么避开这件事吗?我们已经检查过,应用程序在IIS管理控制台上以完全信任的方式运行。

因此,以编程方式创建lambda表达式,然后对其进行编译时会出现此异常。您可以尝试在Default.aspx.cs中执行相同的操作吗?一种方法是在代码中创建lambda表达式:

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

然后做:

var func=expr.Compile()

这句话对你来说是失败的


如果这样做有效,我接下来会将此代码放入签名程序集中,并从web应用程序访问它。

不确定这是否有帮助,但AutoMapper 1.0 RC1可用?我也有AutoMapper 1.0 RC1的相同问题。这真的很奇怪。我来看看。所以我遵循了所有这些步骤,无法得到错误。这是在Win7机器上,我已经确定AutoMapper已经在上面标记了AllowPartiallyTrustedCallers。谷歌告诉我这是因为信任问题,但我不是IIS专家…所以我不知道你可以在这里看什么…对不起,这应该是一个评论:PJimmy,我尝试了你建议的代码,包括调用编译后的函数,效果很好。我甚至将代码放入一个签名程序集中,并从web应用程序调用该程序集。同样,没有问题。但是,automapper调用仍然失败。更有趣的是,由于假期期间无法访问我的主机,我在一台服务器2008虚拟机上对IIS 7进行了一些测试,而automapper在那里工作得很好。我同意这是IIS 7的CAS问题。更多信息:我通过反复试验发现,如果我将AllowPartiallyTrustedCallers属性放在TestLibrary程序集上,问题就会消失。我对这个解决方案不是100%满意,因为显然其他人(包括我自己在Server2008中)不需要这样做就能让相同的代码正常工作。所以我仍然不清楚这里到底发生了什么。他们说IIS7应该简化事情:P。我们已经在托管和非托管场景中进行了多次部署,没有遇到这个问题。至少,在我们添加AllowPartiallyTrustedCallers属性之后。我希望我能多帮点忙!所有部分信任的来电者也为我工作,谢谢你的提示。
namespace TestLibrary
{
    public class Source
    {
        public object Value { get; set; }
    }
}