How to enable HttpClient exists in several libraries

I try to use the IdentityModel package in the .NET Core class library, but I have a conflict between netstandardand System.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 file:

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

By default Class1.cs:

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

What is the right way to solve this problem?

+6
source share
2 answers

, HttpClient Xamarin. , 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}\.nuget\packages\

  • {package-location}\IdentityModel\2.8.1\identitymodel.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>
    
  • dotnet restore

  • dotnet build

.

0

All Articles