如何解析 HttpClient 存在于多个库中



我正在尝试在.NET Core类库中使用IdentityModel包,但是netstandardSystem.Net.Http之间发生了冲突:

error CS0433: The type 'HttpClient' exists in both
'System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and
'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'

项目文件:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="IdentityModel" Version="2.8.1" />
  </ItemGroup>
</Project>

默认Class1.cs

using System;
using System.Net.Http;
namespace Test
{
    public class Class1
    {
        HttpClient client = new HttpClient();
        public Class1() {}
    }
}

解决此问题的正确方法是什么?

我最近在尝试在Xamarin项目中使用HttpClient时遇到了类似的问题。为了解决这个问题,我添加了一个配置文件并重定向到特定版本的System.Net.Http。同样可能适用于您,但您必须选择要使用的版本"2.0.0.0"与"4.1.1.1"。app.config 的内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.1.1" newVersion="4.1.1.1" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

这不是一个答案,而是肮脏的解决方法。希望帮助某人解决实际问题。

  1. 查找全局 NuGet 包位置

    dotnet nuget locals all -l

    在我的机器上,它位于C:Users{user}.nugetpackages

  2. 编辑{package-location}IdentityModel2.8.1identitymodel.nuspec 。修改目标框架以引用.netstandared2.0而不是.netstandared1.4,并删除或注释掉目标框架下的<dependency id="System.Net.Http" version="4.3.2" exclude="Build,Analyzers" />.netstandard2.0

    <group targetFramework=".NETStandard2.0">
        <dependency id="NETStandard.Library" version="1.6.1" exclude="Build,Analyzers" />
        <dependency id="Newtonsoft.Json" version="9.0.1" exclude="Build,Analyzers" />
        <!--<dependency id="System.Net.Http" version="4.3.2" exclude="Build,Analyzers" />-->
        <dependency id="System.ValueTuple" version="4.3.1" exclude="Build,Analyzers" />
        <dependency id="System.Security.Claims" version="4.3.0" exclude="Build,Analyzers" />
        <dependency id="System.Security.Cryptography.Algorithms" version="4.3.0" exclude="Build,Analyzers" />
        <dependency id="System.Security.Cryptography.X509Certificates" version="4.3.0" exclude="Build,Analyzers" />
    </group>
    
  3. dotnet restore

  4. dotnet build

现在,应该生成示例。

相关内容

  • 没有找到相关文章

最新更新