并排使用 AutoMapper 版本的问题
Issues with using side by side AutoMapper versions
我有一个应用程序,我想在其中使用最新版本的 AutoMapper。我也在使用第三方程序集,它本身使用 AutoMapper 3.1.1.0.
如果我允许第三方程序集使用最新版本,它会尝试调用一个不再存在的方法,我得到这个:
System.TypeInitializationException: 'The type initializer for 'Sif.Framework.Service.Mapper.MapperFactory' threw an exception.'
Inner Exception
MissingMethodException: Method not found: 'AutoMapper.IMappingExpression`2<!!0,!!1> AutoMapper.Mapper.CreateMap()'.
因此我将 3.1.1.0 DLL 放在一个子文件夹中并像这样引用它们。我将它们放在一个奇怪位置的目录中,以确保 codeBase 指令确实有效,而不是程序集正在通过其他方式加载。
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="AutoMapper" publicKeyToken="be96cd2c38ef1005" culture="neutral" />
<codeBase version="3.1.1.0" href="C:\OldAutomapper\AutoMapper.dll" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="AutoMapper.Net4" publicKeyToken="be96cd2c38ef1005" culture="neutral" />
<codeBase version="3.1.1.0" href="C:\OldAutomapper\AutoMapper.Net4.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
如果我从我的应用程序中删除对 6.x DLL 的引用并注释掉使用它的代码,这将起作用。第三方DLL获取引用正常运行
如果我随后将 6.x DLL 添加回项目,它仍然有效。嘿,我们有进展了!
但是当我取消注释使用 AutoMapper 的代码时,它再次抛出 TypeInitializationException。请注意,只需在我的代码中引用 AutoMapper 就足以做到这一点,其中 none 必须在运行时执行。我可以注释掉除配置文件 class 之外的所有内容,从未实际加载配置文件,它仍然会破坏第三方程序集。
知道这里发生了什么吗?
编辑:我意识到我应该提到我知道 AutoMapper 的 NuGet 安装添加了 bindingRedirect
到 app.config,这使得所有版本的 AutoMapper 都重定向到新安装的版本。安装后我从我的配置文件中删除了这个重定向。
回答我自己的问题。问题是从 .NET 4.5.1 开始,仅适用于传统桌面应用程序,Visual Studio ignores what you put in app.config and quietly adds its own bindingRedirect to the output config file:
Automatic
binding redirects are enabled by default for traditional desktop apps
that target the .NET Framework 4.5.1 and later versions. The binding
redirects are added to the output configuration (app.config) file when
the app is compiled and overrides the assembly unification that might
otherwise take place. The source app.config file is not modified.
哎呀,谢谢大家。如果你打算悄悄修改我的配置文件,你至少可以修改源文件,这样我就可以知道它正在发生。
一旦我关闭了自动绑定重定向,它就起作用了。
我有一个应用程序,我想在其中使用最新版本的 AutoMapper。我也在使用第三方程序集,它本身使用 AutoMapper 3.1.1.0.
如果我允许第三方程序集使用最新版本,它会尝试调用一个不再存在的方法,我得到这个:
System.TypeInitializationException: 'The type initializer for 'Sif.Framework.Service.Mapper.MapperFactory' threw an exception.'
Inner Exception
MissingMethodException: Method not found: 'AutoMapper.IMappingExpression`2<!!0,!!1> AutoMapper.Mapper.CreateMap()'.
因此我将 3.1.1.0 DLL 放在一个子文件夹中并像这样引用它们。我将它们放在一个奇怪位置的目录中,以确保 codeBase 指令确实有效,而不是程序集正在通过其他方式加载。
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="AutoMapper" publicKeyToken="be96cd2c38ef1005" culture="neutral" />
<codeBase version="3.1.1.0" href="C:\OldAutomapper\AutoMapper.dll" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="AutoMapper.Net4" publicKeyToken="be96cd2c38ef1005" culture="neutral" />
<codeBase version="3.1.1.0" href="C:\OldAutomapper\AutoMapper.Net4.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
如果我从我的应用程序中删除对 6.x DLL 的引用并注释掉使用它的代码,这将起作用。第三方DLL获取引用正常运行
如果我随后将 6.x DLL 添加回项目,它仍然有效。嘿,我们有进展了!
但是当我取消注释使用 AutoMapper 的代码时,它再次抛出 TypeInitializationException。请注意,只需在我的代码中引用 AutoMapper 就足以做到这一点,其中 none 必须在运行时执行。我可以注释掉除配置文件 class 之外的所有内容,从未实际加载配置文件,它仍然会破坏第三方程序集。
知道这里发生了什么吗?
编辑:我意识到我应该提到我知道 AutoMapper 的 NuGet 安装添加了 bindingRedirect
到 app.config,这使得所有版本的 AutoMapper 都重定向到新安装的版本。安装后我从我的配置文件中删除了这个重定向。
回答我自己的问题。问题是从 .NET 4.5.1 开始,仅适用于传统桌面应用程序,Visual Studio ignores what you put in app.config and quietly adds its own bindingRedirect to the output config file:
Automatic binding redirects are enabled by default for traditional desktop apps that target the .NET Framework 4.5.1 and later versions. The binding redirects are added to the output configuration (app.config) file when the app is compiled and overrides the assembly unification that might otherwise take place. The source app.config file is not modified.
哎呀,谢谢大家。如果你打算悄悄修改我的配置文件,你至少可以修改源文件,这样我就可以知道它正在发生。
一旦我关闭了自动绑定重定向,它就起作用了。