【问题标题】:Mixed mode assembly issue with dotnet 4.5dotnet 4.5 的混合模式组装问题
【发布时间】:2017-03-04 14:23:16
【问题描述】:

我刚刚将我的 C# winforms 应用程序从 dotnet 2.0 (VS2010) 升级到 dotnet 4.5 (VS2015)。我现在在尝试运行它时遇到此错误:

混合模式程序集是针对版本“v2.0.50727”构建的 运行时并且不能在没有额外的 4.0 运行时加载 配置

经过大量研究后,我明白这是因为我的 dotnet 4.5 应用程序引用了使用 dotnet 2.0(或者我认为是 3.0 或 3.5)构建的代码。解决方案(根据我所阅读的内容)是将一些文本添加到 app.config 文件中。但无论我添加什么(每次都关闭应用程序),它都不起作用。

到目前为止,我已经尝试过(不分先后):

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

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

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

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

<startup useLegacyV2RuntimeActivationPolicy="true" /> 

我要尝试的东西已经不多了。

我不确定这是否相关,但是当我将鼠标悬停在某些区域上时,我的 App.config 文件会给我警告,给我如下消息:

命名空间“urn:schemas-microsoft-com:asm.v1”的导入架构是 没有解决。

我在另一个帖子中提出了这个问题,但有人说这应该不是问题。下面的快速截图: Mouseover message

任何帮助将不胜感激。

我当前的 App.config 如下所示:

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true" />

  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <section name="netTiersService" type="MyApp.Data.Bases.NetTiersServiceSection, MyApp.Data"/>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="MyApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
    </sectionGroup>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="MyApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add source="Enterprise Library Logging" formatter="Text Formatter" log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Formatted EventLog TraceListener"/>
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}


EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: 

{appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread 

Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, 

Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter"/>
    </formatters>
    <categorySources>
      <add switchValue="All" name="Exceptions">
        <listeners>
          <add name="Formatted EventLog TraceListener"/>
        </listeners>
      </add>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Formatted EventLog TraceListener"/>
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events"/>
      <notProcessed switchValue="All" name="Unprocessed Category"/>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Formatted EventLog TraceListener"/>
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="NoneExceptionPolicy">
        <exceptionTypes>
          <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="NotifyRethrow" name="Exception">
            <exceptionHandlers>
              <add logCategory="Exceptions" eventId="100" severity="Error" title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.XmlExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Logging Handler"/>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
  <connectionStrings>
    <add name="MyApp" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <netTiersService defaultProvider="SqlNetTiersProvider">
    <providers>
      <add name="SqlNetTiersProvider" type="MyApp.Data.SqlClient.SqlNetTiersProvider, MyApp.Data.SqlClient" useStoredProcedure="false" connectionStringName="MyApp" applicationName="MyAppWeb" providerInvariantName="System.Data.SqlClient" enableEntityTracking="false" useEntityFactory="false"/>
    </providers>
  </netTiersService>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.EnterpriseLibrary.Logging" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="4.0.0.0" newVersion="5.0.414.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="4.0.0.0" newVersion="5.0.414.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.EnterpriseLibrary.Data" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="4.0.0.0" newVersion="5.0.414.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="4.0.0.0" newVersion="5.0.414.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <userSettings>
    <MyApp.Properties.Settings>
      <setting name="LastConnection" serializeAs="String">
        <value>LocalSqlServer</value>
      </setting>
    </MyApp.Properties.Settings>
  </userSettings>
  <applicationSettings>
    <MyApp.Properties.Settings>
      <setting name="MyApp_NAVService_MyAppInt" serializeAs="String">
        <value>http://localhost:7047/DynamicsNAV/WS/MyApp_Integration/Codeunit/MyAppInt</value>
      </setting>
    </MyApp.Properties.Settings>
  </applicationSettings>

</configuration>

【问题讨论】:

  • 当然。我已更新我的帖子以包含 App.config。
  • 这是我读过的一篇关于此的文章,我尝试过在 app.config 中添加的建议。
  • 作为进一步的实验,我删除了我的 App.config 并重新开始。现在我所拥有的只是启动节点。所以看起来它不是 App.config 中引起冲突的其他东西。这就是我的 app.config 的全部内容:&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt; &lt;startup useLegacyV2RuntimeActivationPolicy="true"&gt; &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/&gt; &lt;requiredRuntime version="v4.0.20506" /&gt; &lt;/startup&gt; &lt;/configuration&gt;
  • MyApp.Data 也是 dotnet 4.5。我已将此解决方案中的每个项目升级到 dotnet 4.5,除 winforms 项目外,其他所有项目都可以正常编译。其他项目也引用了 dotnet 2.0 dll,但他们没有遇到同样的问题。
  • 对不起 - 也许我之前的评论不清楚。我的意思是我把所有东西都去掉了,除了启动位,我仍然得到错误。

标签: c# winforms c#-4.0 visual-studio-2015 app-config


【解决方案1】:

问题最终是我使用的一个特定 dll (dotnet v2.0)。它是一个 C++ 的,不知何故使它不同于许多其他 dotnet 的(我不知道如何 - 我根本不了解 C++)。

我将该解决方案升级到 4.5 并重新构建后,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-01
    • 2016-10-19
    • 2011-04-14
    • 1970-01-01
    • 2020-12-27
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多