我正在尝试在.NET Core类库中使用IdentityModel包,但我在netstandard
和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 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() {}
}
}
解决此问题的正确方法是什么?
答案 0 :(得分:1)
我最近在尝试在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 :(得分:0)
这不是答案,而是肮脏的解决方法。希望帮助某人解决实际问题。
找到全局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
现在应该构建样本。