System.Text.Json需要两个不同版本的CompilerServices.Unsafe吗?

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

System.Text.Json requires two different versions of CompilerServices.Unsafe?

问题

不要回答我要翻译的问题。以下是已经翻译好的部分:

用于上下文,我正在为VS2017创建一个VSIX,我需要System.Text.JSON来进行数据的序列化/反序列化。我正在使用System.Text.JSON的版本6.0.0.0。

如果我将CompilerServices.Unsafe设置为4.0.4.1(nuget版本为4.5.3)(并进行适当的绑定重定向(见下文),我会遇到以下错误:

System.Text.Json需要两个不同版本的CompilerServices.Unsafe吗?

正如您所见,它要求System.Runtime.CompilerServices.Unsafe版本为6.0.0.0。在目标目录中使用ILSpy,我可以看到捆绑的System.Runtime.CompilerServices.Unsafe版本是4.0.4.1,这是预期的。

作为回应,我将System.Runtime.CompilerServices.Unsafe升级到6.0.0.0(nuget版本为6.0.0),我会遇到以下错误:

System.Text.Json需要两个不同版本的CompilerServices.Unsafe吗?

是的,现在它需要版本4.0.4.1!

详细信息

app.config、.csproj和packages.config

...使用版本4.0.4.1

当我尝试使用版本4.0.4.1的CompilerServices.Unsafe时,我会像这样设置我的项目:

  • 我在app.config中设置了绑定重定向到版本4.0.4.1
<dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1" />
</dependentAssembly>
  • 我在csproj中添加了对版本4.0.4.1的引用
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
  • 我在packages.config中将其设置为4.5.3。
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net472" />

...使用版本6.0.0.0

同样,当我将其设置为6.0.0.0时,我执行相同的步骤

  • 我在app.config中设置了绑定重定向到版本4.0.4.1
<dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
  • 我设置了csproj
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
  • 我设置了packages.config
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net472" />

修复尝试

我尝试过一些事情:

  • 清除本地包缓存(即删除解决方案根目录中名为“packages”的文件夹)。结果:没有变化。

  • 删除bin和obj文件夹。结果:没有变化。

  • 将system.text.json升级到6.0.7版本(6.0.0主要版本的最高版本)。结果:没有变化。

  • 将system.text.json升级到7.0.2版本(最新版本)。结果:没有变化。

  • 将system.text.json降级。结果:没有变化。

英文:

For context, I am making a VSIX for VS2017, and I require System.Text.JSON for serializing/deserializing data.I am using version 6.0.0.0 of System.Text.JSON

If I set CompilerServices.Unsafe to 4.0.4.1 (nuget version 4.5.3) (with appropriate binding redirects (see below)), I get this error:

System.Text.Json需要两个不同版本的CompilerServices.Unsafe吗?

As you can see, it asks for System.Runtime.CompilerServices.Unsafe version 6.0.0.0. Using ILSpy within the target directory, I can see that the version of System.Runtime.CompilerServices.Unsafe that came bundled was version 4.0.4.1, as expected.

In response, I bump System.Runtime.CompilerServices.Unsafe to 6.0.0.0 (nuget version 6.0.0), I get this error!

System.Text.Json需要两个不同版本的CompilerServices.Unsafe吗?

That's right - now it wants version 4.0.4.1!

Details

app.config, .csproj, packages.config

...with version 4.0.4.1

When I try with version 4.0.4.1 of CompilerServices.Unsafe, I setup my project like so:

  • I setup app.config with a binding redirect to version 4.0.4.1
<dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1" />
</dependentAssembly>
  • I add a reference to version 4.0.4.1 on the csproj
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
  • I set it to 4.5.3 in packages.config.
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net472" />

...with version 6.0.0.0

Similarly, when I set it to 6.0.0.0, I perform the same steps

  • I setup app.config with a binding redirect to version 4.0.4.1
<dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
  • I setup the csproj
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
  • I setup packages.config
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net472" />

Fix Attempts

A few things I have tried:

  • clear the local package cache (ie, deleting the folder called "packages" at solution root). Result: No Change.

  • remove bin and obj folders. Result: No Change.

  • upgrading system.text.json to version 6.0.7 (Highest version of major version 6.0.0) . Result: No Change.

  • upgrading system.text.json to version 7.0.2 (Latest). Result: No change.

  • downgrading system.text.json. Result: No change.

答案1

得分: 1

Alright, "I" figured it out (credit to @zivkan for pointing me in the right direction 😊). Instead of using app.config style of binding redirects (which doesn't work for VSIX) you can instead use ProvideBindingRedirectionAttribute.

I this line to my AssemblyInfo.cs:
```csharp
[assembly: ProvideBindingRedirection(AssemblyName = "System.Runtime.CompilerServices.Unsafe",
      NewVersion = "6.0.0.0", OldVersionLowerBound = "1.0.0.0",
      OldVersionUpperBound = "4.0.4.2")]

...and, as if by magic, it just works.

See also:


<details>
<summary>英文:</summary>

Alright, &quot;I&quot; figured it out (credit to @zivkan for pointing me in the right direction &#128522;). Instead of using app.config style of binding redirects (which doesn&#39;t work for VSIX) you can instead use ProvideBindingRedirectionAttribute.

I this line to my AssemblyInfo.cs:
```csharp
[assembly: ProvideBindingRedirection(AssemblyName = &quot;System.Runtime.CompilerServices.Unsafe&quot;,
      NewVersion = &quot;6.0.0.0&quot;, OldVersionLowerBound = &quot;1.0.0.0&quot;,
      OldVersionUpperBound = &quot;4.0.4.2&quot;)]

...and, as if by magic, it just works.

See also:

huangapple
  • 本文由 发表于 2023年4月11日 01:51:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979449.html
匿名

发表评论

匿名网友

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

确定