多版本DLL在同一个C#应用程序中的使用

huangapple go评论76阅读模式
英文:

Multi Version DLL Use on the same C# application

问题

您尝试在多个项目中使用不同版本的Newtonsoft.Json库,并遇到了加载错误。为了解决此问题,您可以尝试以下步骤:

  1. 在您的Stripe项目中,确保将以下绑定重定向添加到Web.config中,以指定Stripe项目使用的Newtonsoft.Json版本:
<dependentAssembly>
  <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
  <codeBase version="13.0.0.0" href="Stripe\Newtonsoft.Json.dll"/>
</dependentAssembly>

这将确保Stripe项目仅在需要时使用较高版本的Newtonsoft.Json。

  1. 确保在Stripe项目的项目文件中,将Newtonsoft.Json.dll的路径设置为正确的低版本,如下所示:
<Content Include="..\..\Comps\Json.NET\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  <Link>Newtonsoft.Json.dll</Link>
</Content>

这将确保Stripe项目使用的是较低版本的Newtonsoft.Json。

  1. 验证您的Web应用程序的其他部分是否正确配置以使用较低版本的Newtonsoft.Json。确保没有其他地方尝试引用较高版本的库。

通过执行以上步骤,您应该能够限制Stripe项目使用较高版本的Newtonsoft.Json,同时确保其他项目继续使用较低版本。如果仍然遇到问题,请检查其他可能影响装配绑定的因素,如应用程序池配置或其他依赖项。

英文:

I have got a web application with multiple projects that is using newtonsoft 6.0.0.0. Now I have added a stripe dll to two of the projects which turns out that it needs higher newtonsoft. What I need to is that instead of upgrading all newtonsoft library across the web application projects, and also in other apps as there are some shared library, I want to limit the use of newtonsoft higher version to only stripe so that all projects still use the lower version even the projects using stripe can still use the lower version for non stripe logics.

What I tried so far is I added below to the project files:

      &lt;ItemGroup&gt;
        &lt;!-- Place the new snippet here --&gt;
        &lt;Content Include=&quot;..\..\Comps\Json.NET\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll&quot;&gt;
          &lt;CopyToOutputDirectory&gt;PreserveNewest&lt;/CopyToOutputDirectory&gt;
          &lt;Link&gt;Stripe\Newtonsoft.Json.dll&lt;/Link&gt;
        &lt;/Content&gt;
        &lt;Content Include=&quot;..\..\Comps\Json.NET\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll&quot;&gt;
          &lt;CopyToOutputDirectory&gt;PreserveNewest&lt;/CopyToOutputDirectory&gt;
          &lt;Link&gt;Newtonsoft.Json.dll&lt;/Link&gt;
        &lt;/Content&gt;
    	
      &lt;/ItemGroup&gt;

Also I added the lower version as a reference

&lt;Reference Include=&quot;Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL&quot;&gt;
  &lt;HintPath&gt;..\..\Comps\Json.NET\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll&lt;/HintPath&gt;
&lt;/Reference&gt;	

I have also added this in Web.config

  &lt;dependentAssembly&gt;
    &lt;assemblyIdentity name=&quot;Newtonsoft.Json&quot; publicKeyToken=&quot;30ad4fe6b2a6aeed&quot;/&gt;
    &lt;bindingRedirect oldVersion=&quot;0.0.0.0-8.0.0.0&quot; newVersion=&quot;6.0.0.0&quot; /&gt;
    &lt;codeBase&gt; version=&quot;6.0.0.0&quot; href=&quot;Newtonsoft.Json.dll&quot;&lt;/codeBase&gt;
  &lt;/dependentAssembly&gt;
  &lt;dependentAssembly&gt;
    &lt;assemblyIdentity name=&quot;Newtonsoft.Json&quot; publicKeyToken=&quot;30ad4fe6b2a6aeed&quot;/&gt;
    &lt;bindingRedirect oldVersion=&quot;9.0.0.0-13.0.0.0&quot; newVersion=&quot;13.0.0.0&quot; /&gt;
    &lt;codeBase&gt; version=&quot;13.0.0.0&quot; href=&quot;Stripe\Newtonsoft.Json.dll&quot;&lt;/codeBase&gt;
  &lt;/dependentAssembly&gt;

But I am getting error loading the web app.

Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Any help to resolve this will be appreciated.

答案1

得分: 1

这不会起作用。它只采用给定 assemblyIdentity 的第一个重定向规则。您可以在这里看到。

如果在重定向方面存在冲突,将使用配置文件中的第一个匹配的重定向语句。

英文:

That will not work. It only takes the first redirect rule with a given assemblyIdentity. You can see that here

>In case of a conflict in redirection, the first matching redirection statement in the configuration file is used.

答案2

得分: 0

您的XML格式不正确。属性已被放置在内容中。应该像这样:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
    <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="6.0.0.0" />
    <codeBase version="6.0.0.0" href="Newtonsoft.Json.dll"></codeBase>
</dependentAssembly>
<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
    <bindingRedirect oldVersion="9.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
    <codeBase version="13.0.0.0" href="Stripe\Newtonsoft.Json.dll"></codeBase>
</dependentAssembly>
英文:

Your xml is wrong. The attributes have been made content. It should look like this:

  &lt;dependentAssembly&gt;
    &lt;assemblyIdentity name=&quot;Newtonsoft.Json&quot; publicKeyToken=&quot;30ad4fe6b2a6aeed&quot;/&gt;
    &lt;bindingRedirect oldVersion=&quot;0.0.0.0-8.0.0.0&quot; newVersion=&quot;6.0.0.0&quot; /&gt;
    &lt;codeBase version=&quot;6.0.0.0&quot; href=&quot;Newtonsoft.Json.dll&quot;&gt;&lt;/codeBase&gt;
  &lt;/dependentAssembly&gt;
  &lt;dependentAssembly&gt;
    &lt;assemblyIdentity name=&quot;Newtonsoft.Json&quot; publicKeyToken=&quot;30ad4fe6b2a6aeed&quot;/&gt;
    &lt;bindingRedirect oldVersion=&quot;9.0.0.0-13.0.0.0&quot; newVersion=&quot;13.0.0.0&quot; /&gt;
    &lt;codeBase version=&quot;13.0.0.0&quot; href=&quot;Stripe\Newtonsoft.Json.dll&quot;&gt;&lt;/codeBase&gt;
  &lt;/dependentAssembly&gt;

huangapple
  • 本文由 发表于 2023年5月25日 17:25:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定