【问题标题】:System.Net.Http in net47net47 中的 System.Net.Http
【发布时间】:2017-10-18 19:39:05
【问题描述】:

我在 Windows 上有一个普通的类库,使用 .NET 4.7

我想加入web-push-csharp

该软件包列出了 2 个可接受的依赖项(Newtonsoft Json 和 BouncyCastle)。它还想安装Microsoft.Net.Http。我不知道为什么。我不需要它。我的目标框架带有一个非常好的System.Net.Http 供参考。 由于该 Microsoft 软件包有一个其他依赖项列表,这些依赖项与我链中更高层的其他依赖项发生冲突,这给我带来了巨大的不便。为什么要包含我不需要的包?

现在我认为这应该不是问题,然后克隆了 Web 推送包存储库,将我的目标框架放入其中......这是正确的,如果没有外部 @987654328,您将无法为目标框架 net47 编译@ 参考。然而,我的 Create New Project .NET 4.7 项目已经提供了这样的参考,而根本没有触及 nuget。

仅使用 NewtonsoftJson 和 BouncyCastle 将所有包文件复制到新的 ClassLibrary1 中效果很好。

我在这里缺少什么?为什么 .NET 框架从 4.5 到 4.6 失去了它的 System.Net.Http?为什么没有可用的 FrameworkTarget,我可以说“这很好,我有一个完整的 4.7”?

除了克隆存储库并从中创建一个普通的 4.7 类库供我使用之外,还有其他选择吗?


附加信息:

我克隆了 web-push-csharp 存储库并添加了一个新的目标框架 net47,而不依赖于 Microsoft.Net.Http

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netcoreapp1.0;netcoreapp1.1;netstandard1.3;net45;net46;net47</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='net45' OR '$(TargetFramework)'=='net46'">
    <PackageReference Include="BouncyCastle" Version="1.8.1" /> 
    <PackageReference Include="Microsoft.Net.Http" Version="2.2.29" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='net47'">
    <PackageReference Include="BouncyCastle" Version="1.8.1" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='netcoreapp1.0' OR '$(TargetFramework)'=='netcoreapp1.1' OR '$(TargetFramework)'=='netstandard1.3'">
    <PackageReference Include="BouncyCastle.NetCore" Version="1.8.1.3" /> 
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
  </ItemGroup>
</Project>

这会导致多个编译错误:

错误 CS0234 命名空间“System.Net”中不存在类型或命名空间名称“Http”(您是否缺少程序集引用?)WebPush(net47)


解决方案

对于阅读本文并寻找解决方案的任何人:测试后,我将 accepted answer 合并到一个 fork 的 branch 中,并向维护者发送了一个 Pull request,以便它可以在原始包中使用。

【问题讨论】:

  • 真的和.net 4.7有关吗?我认为如果您删除对该包的依赖,即使对于 net45 和 net46,您也无法编译该项目?
  • @Evk hm,确实它对其他人也不起作用,我将删除该标签。
  • 删除PackageReference,添加&lt;Reference Include="System.Net.Http" /&gt;(常规,非nuget参考),它将为4.5\6和4.7编译。

标签: c# .net target-framework


【解决方案1】:

正如您所说,.NET Framework(所有提到的版本:4.5、4.6、4.7)附带了可以使用的System.Net.Http 程序集,因此无需引用任何 nuget 包。但是你需要引用这个程序集本身:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netcoreapp1.0;netcoreapp1.1;netstandard1.3;net45;net46;net47</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='net45' OR '$(TargetFramework)'=='net46'">
    <PackageReference Include="BouncyCastle" Version="1.8.1" /> 
    <!-- here -->
    <Reference Include="System.Net.Http" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='net47'">
    <PackageReference Include="BouncyCastle" Version="1.8.1" />
    <!-- and here -->
    <Reference Include="System.Net.Http" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='netcoreapp1.0' OR '$(TargetFramework)'=='netcoreapp1.1' OR '$(TargetFramework)'=='netstandard1.3'">
    <PackageReference Include="BouncyCastle.NetCore" Version="1.8.1.3" /> 
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
  </ItemGroup>
</Project>

【讨论】:

    【解决方案2】:

    在 nuspec 文件中,他们没有提到支持 .NET 4.7 (https://github.com/web-push-libs/web-push-csharp/blob/master/WebPush.nuspec) 可能您可以克隆存储库并添加修改文件,如下所示

    <?xml version="1.0"?>
    <package >
      <metadata>
        <id>WebPush</id>
        <version>1.0.9</version>
        <authors>Cory Thompson</authors>
        <owners>Cory Thompson</owners>
        <licenseUrl>https://github.com/coryjthompson/web-push-csharp/blob/master/LICENSE</licenseUrl>
        <projectUrl>https://github.com/coryjthompson/web-push-csharp/</projectUrl>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Web Push library for C#</description>
        <copyright>Copyright 2017</copyright>
        <tags>web push notifications vapid</tags>
        <dependencies>  
            <group targetFramework="netcoreapp1.0">
                <dependency id="BouncyCastle.NetCore" version="1.8.1.3"/>
                <dependency id="Newtonsoft.Json" version="10.0.1"/>
            </group>
            <group targetFramework="netcoreapp1.1">
                <dependency id="BouncyCastle.NetCore" version="1.8.1.3"/>
                <dependency id="Newtonsoft.Json" version="10.0.1"/>
            </group>
            <group targetFramework="netstandard1.3">
                <dependency id="BouncyCastle.NetCore" version="1.8.1.3"/>
                <dependency id="Newtonsoft.Json" version="10.0.1"/>
            </group>
    
            <group targetFramework="net45">
                <dependency id="BouncyCastle" version="1.8.1"/>
            <dependency id="Microsoft.Net.Http" version="2.2.29" />
                <dependency id="Newtonsoft.Json" version="10.0.1"/>
            </group>
    
            <group targetFramework="net46">
                <dependency id="BouncyCastle" version="1.8.1"/>
                <dependency id="Microsoft.Net.Http" version="2.2.29" />
                <dependency id="Newtonsoft.Json" version="10.0.1"/>
            </group>
            <group targetFramework="net47">
                <dependency id="BouncyCastle" version="1.8.1"/>
                <dependency id="Microsoft.Net.Http" version="2.2.29" />
                <dependency id="Newtonsoft.Json" version="10.0.1"/>
            </group>
        </dependencies>
      </metadata>
    </package>
    

    并打包您自己的 nuget 文件并在您的项目中使用它。 https://programmium.wordpress.com/2014/05/26/keen-io-windows-phone-8-1-and-nuget-package-creation/ 这篇文章将告诉你如何创建一个新的 nuget 包。

    【讨论】:

    • 但关键是我不想要“Microsoft.Net.Http”。但是我已经将 net47 添加到框架中,并且 web-push-csharp 不会在没有该参考的情况下编译。是什么让我想到了我的问题:为什么。我已经在 4.7 中有System.Net.Http
    • 因为你在nuspec文件中说的。如果您想使用System.Net.Http,请将&lt;dependency id="Microsoft.Net.Http" version="2.2.29" /&gt; 更改为&lt;dependency id="System.Net.Http" version="4.3.3" /&gt; 但不建议这样做,因为nuget 包是为Microsoft.Net.Http 而不是System.Net.Http 创建的。如果您真的想使用System.Net.Http,只需删除Microsoft.Net.Http 并引入System.Net.Http
    • 但是为什么我需要一个 nuget 包,因为当我说“创建新项目”时,我的目标是自动引用?
    • 如果您的项目中已经引用了依赖项(具有正确的版本),则完全可以,否则将被下载。
    • 更多 System.Net 没有实现 Http ref - msdn.microsoft.com/en-us/library/system.net(v=vs.110).aspx 您需要将 nuget.org/packages/System.Net.Http 引用到您在问题中提到的 .csproj 文件
    猜你喜欢
    • 1970-01-01
    • 2019-06-11
    • 2017-05-27
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多