English 中文(简体)
Problem with AutoMapper in IIS7 when using signed assemblies
原标题:

I am trying to use AutoMapper with web application running on IIS 7. The intended use it so map domain types defined in an external dll to view models defined in the IIS application. This works fine except when the external dll is singed. Then I get the following error:

AutoMapper.AutoMapperMappingException was unhandled by user code
  Message="Trying to map TestLibrary.Source to WebApplication1.Destination.
Using mapping configuration for TestLibrary.Source to WebApplication1.Destination
Exception 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.
Using mapping configuration for TestLibrary.Source to WebApplication1.Destination
Destination property: Value
Exception 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: 

Steps to Reproduce the problem:

1) Create a new web application using Visual Studio 2008 on a machine with IIS 7 installed. (We are using windows 7).

2) Download the AutoMapper.dll from http://www.codeplex.com/AutoMapper. (We are using version 0.4.x.x) and add a reference to this Dll to your web application.

3) Place the following code in the code behind for the default page:

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) Create a new Class Library named "TestLibrary" and rename the Class1.cs file to Source.cs and put the following code it in:

namespace TestLibrary
{
    public class Source
    {
        public object Value { get; set; }
    }
}

5) Add a reference to this library to your web application.

6) Run the solution and you should see the "Automapper works!" output.

7) To make it fail, you must do two things.
i) Configure the website to run under IIS instead of the Visual Studio development server. ii) Sign the TestLibrary assembly. After that, running the solution should produce the error reported above.

Does anyone have any idea how to get around this? We have checked and the application is running with full trust accourding the the IIS management console.

最佳回答

So this exception comes from creating a lambda expression programmatically and then compiling it. Can you try doing the same thing, just in the Default.aspx.cs? One way to do this would be to create a lambda expression in code:

Expression> expr = () => 5;

then do:

var func = expr.Compile();

it s a line just like that that s failing for you.

If this works, I d then next put this code in that signed assembly, and access it from the web application.

问题回答

暂无回答




相关问题
session transfer issue from Tomcat to ASP.Net

I am using Tomcat to host JSP and using IIS 7.0 to host aspx (C# + .Net 3.5 + VSTS 2008), and I have some session transfer issue from JSP page to ASPX page. JSP page is in one domain and all other ...

How to use appcmd to set debug=false in IIS7

I m trying to use APPCMD to set debug=false in web.config, per the documentation. The specific syntax I am using is: APPCMD SET CONFIG "SITE/VDIR" section:compilation /debug:False /commit:APP ...

Windows User Account that executes only IIS7 Provisions

I have a web application that executes IIS 7 provisions (using Microsoft.Web.Administration.dll) to create our web and ftp sites. When I run this using the administrator account, it works ok. I want ...

Could I get by HttpModule the URL that contains * before?

My web-site is working under ASP.NET 3.5, IIS7, Integrated pipiline mode. My HttpModule is getting request to all resources, including URLs like this: http://my-site.com/address And I would support *...

IIS 7 Http Handler development issue

I am using VSTS 2008 + C# + .Net 3.5 + IIS 7.0 to develop an ASP.Net web site. I want to develop an Http module, which could inspect whether incoming request has some specific http header (e.g. ...

热门标签