IIS 7 で実行されている Web アプリケーションでAutoMapperを使用しようとしています。これを使用して、外部 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 works!」というメッセージが表示されます。出力。
7) 失敗させるには、2 つのことを行う必要があります。
i) Visual Studio 開発サーバーではなく IIS で実行するように Web サイトを構成します。ii) TestLibrary アセンブリに署名します。その後、ソリューションを実行すると、上記で報告されたエラーが発生するはずです。
これを回避する方法を知っている人はいますか?IIS 管理コンソールで確認したところ、アプリケーションは完全な信頼で実行されています。