【问题标题】:Mixed mode assembly is built against version ‘v2.0.50727′ of the runtime混合模式程序集是针对运行时的“v2.0.50727”版本构建的
【发布时间】:2011-09-19 12:31:54
【问题描述】:

我收到以下异常:

混合模式程序集是针对运行时版本“v2.0.50727”构建的,如果没有额外的配置信息,则无法在 4.0 运行时中加载。

当我试图从我的 WPF 程序中导出水晶报表时...

我已经在 app.config 中添加了以下内容...

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
  <NetFx40_LegacySecurityPolicy enabled="true"/>
</runtime>

有高手可以帮忙吗???

我找到的参考资料: http://www.davidmoore.info/2010/12/17/running-net-2-runtime-applications-under-the-net-4-runtime

【问题讨论】:

  • 重要提示:如果错误发生在错误列“文件”为SGEN,则修复需要位于sgen.exe.config 旁边的文件sgen.exe 中。例如,对于 VS 2015,创建 C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\sgen.exe.config。来源:SGEN Mixed mode assembly 最小文件内容:&lt;configuration&gt;&lt;startup useLegacyV2RuntimeActivationPolicy="true"/&gt;&lt;/configuration&gt;
  • 请注意,您可能不仅有“C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\”,而且在此之下,还有一个 'x64 ' 目录,该目录也有一个 sgen.exe,可能还有其他几个 sgen 副本/版本都在不同的路径中,具体取决于您安装的 VS 的数量和版本。
  • @ToolmakerSteve 请发表您的评论作为答案,以便我投票;其他答案都没有帮助我。
  • @Malcolm - 好的,posted as an answer

标签: .net visual-studio-2010 .net-4.0 ado.net crystal-reports


【解决方案1】:

尝试在配置节点下的 app.config 中使用这个确切的启动标记

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    <requiredRuntime version="v4.0.20506" />
  </startup>

【讨论】:

  • 其实你应该在YourApp.**exe**.config中添加棘手的行。
  • 那是app.config。编译器将在构建时重命名它。
  • 唯一需要的部分是 useLegacyV2RuntimeActivationPolicy="true"。您可以保留当前的框架版本。
  • Ben 说我可以加倍,我遗漏了额外的 requiredRuntime,我的问题就消失了。
  • 此链接也应该可以帮助您了解问题所在以及解决方案对您的作用:msdn.microsoft.com/en-us/magazine/ee819091.aspx
【解决方案2】:

尝试使用另一个配置文件(不是您项目中的那个)并重新启动 Visual Studio:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.executionengine.x86.exe.config
(32-bit)

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.executionengine.exe.config
(64-bit)

【讨论】:

  • 我会对此进行扩展,因为绕过驻留在应用程序中并且通常需要额外应用程序配置的本机 .config 并不是一个好的解决方案。
  • 对于单元测试,配置更改必须发生在 vstest.executionengine.x86.exe.config 文件中,因为该可执行文件是您正在测试的代码的父进程。这个问题应该在 Visual Studio 2012 SP4 中得到修复,但它仍然存在于我的机器上。
  • (请注意,您不必重新启动 Visual Studio 以使更改生效。您应该只需要结束 vstest.* 进程上的任务,因为即使在您的单元测试完成后这些任务仍然存在。 )
  • 赞成这个,因为它把我带到了我必须改变它的实际地方。
【解决方案3】:

我实际上遇到了与 inverse 解决方案相同的问题。我已将 .NET 项目升级到 .NET 4.0,然后又恢复到 .NET 3.5。我项目中的 app.config 继续具有以下导致上述错误的问题:

<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

解决此错误的解决方案是将其恢复为正确的 2.0 参考,如下所示:

<startup>
  <supportedRuntime version="v2.0.50727"/>
</startup>

因此,如果降级产生上述错误,您可能需要备份 .NET Framework 支持的版本。

【讨论】:

    【解决方案4】:

    该异常清楚地表明某些 .NET 2.0.50727 组件包含在 .NET 4.0 中。在 App.config 文件中使用:

    <startup useLegacyV2RuntimeActivationPolicy="true" /> 
    

    解决了我的问题

    【讨论】:

    • 这需要一个斜线&lt;startup useLegacyV2RuntimeActivationPolicy="true" /&gt;
    【解决方案5】:

    对我来说,这是在 MSTest (VS2015) 下运行单元测试时抛出的。不得不添加

    <startup useLegacyV2RuntimeActivationPolicy="true">
    </startup>
    

    C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\TE.ProcessHost.Managed.exe.config

    Mixed-Mode Assembly MSTest Failing in VS2015

    【讨论】:

      【解决方案6】:

      请在您的应用程序 app.config 文件中添加属性 useLegacyV2RuntimeActivationPolicy="true"。

      旧值

        <startup>
          <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
        </startup>
      

      新价值

        <startup useLegacyV2RuntimeActivationPolicy="true">
          <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
        </startup>
      

      它会解决你的问题。

      【讨论】:

      • 很高兴知道它对您有用。感谢您的评论!
      【解决方案7】:

      从 app.config 启用旧版对我不起作用。由于未知原因,我的应用程序没有激活 V2 运行时策略。我在here 附近找到了一份工作。

      从 app.config 启用旧版是推荐的方法,但在某些情况下它无法按预期工作。在您的主应用程序中使用以下代码来强制 Legacy V2 策略:

      public static class RuntimePolicyHelper
      {
      public static bool LegacyV2RuntimeEnabledSuccessfully { get; private set; }
      
      static RuntimePolicyHelper()
      {
          ICLRRuntimeInfo clrRuntimeInfo =
              (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
                  Guid.Empty, 
                  typeof(ICLRRuntimeInfo).GUID);
          try
          {
              clrRuntimeInfo.BindAsLegacyV2Runtime();
              LegacyV2RuntimeEnabledSuccessfully = true;
          }
          catch (COMException)
          {
              // This occurs with an HRESULT meaning 
              // "A different runtime was already bound to the legacy CLR version 2 activation policy."
              LegacyV2RuntimeEnabledSuccessfully = false;
          }
      }
      
      [ComImport]
      [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
      [Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
      private interface ICLRRuntimeInfo
      {
          void xGetVersionString();
          void xGetRuntimeDirectory();
          void xIsLoaded();
          void xIsLoadable();
          void xLoadErrorString();
          void xLoadLibrary();
          void xGetProcAddress();
          void xGetInterface();
          void xSetDefaultStartupFlags();
          void xGetDefaultStartupFlags();
      
          [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
          void BindAsLegacyV2Runtime();
      }
      }
      

      【讨论】:

        【解决方案8】:

        如果错误列“文件”为 SGEN 时发生错误,则修复需要位于 sgen.exe 旁边的文件 sgen.exe.config 中。例如,对于 VS 2015,创建 C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\sgen.exe.config。最小文件内容:&lt;configuration&gt;&lt;startup useLegacyV2RuntimeActivationPolicy="true"/&gt;&lt;/configuration&gt;

        来源:SGEN Mixed mode assembly

        【讨论】:

          猜你喜欢
          • 2016-05-08
          • 2013-01-26
          • 2012-08-11
          • 1970-01-01
          • 2016-03-29
          • 1970-01-01
          • 1970-01-01
          • 2023-03-24
          • 2011-04-30
          相关资源
          最近更新 更多