使用并排AutoMapper版本的问题

时间:2017-05-22 01:51:37

标签: .net automapper app-config .net-assembly

我有一个应用程序,我想使用最新版本的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就足以做到这一点,它们都不必在运行时执行。我可以注释除Profile类之外的所有内容,从不实际加载Profile,它仍会破坏第三方程序集。

这里有什么想法?

编辑:我意识到我应该提到我意识到AutoMapper的NuGet安装会向app.config添加bindingRedirect,这会使所有版本的AutoMapper重定向到新安装的版本。我在安装后从配置文件中删除了这个重定向。

1 个答案:

答案 0 :(得分:0)

回答我自己的问题。问题是从.NET 4.5.1开始,仅适用于传统桌面应用程序,Visual Studio ignores what you put in app.config and quietly adds its own bindingRedirect to the output config file

  

自动   传统桌面应用程序默认启用绑定重定向   以.NET Framework 4.5.1及更高版本为目标。绑定   重定向将添加到输出配置(app.config)文件时   应用程序已编译并覆盖可能的程序集统一   否则发生。源app.config文件未被修改。

哎呀,谢谢你们。如果您要静默修改我的配置文件,您至少可以修改源文件,以便我可以告诉它。

一旦我关闭了自动绑定重定向,就可以了。