简体   繁体   English

net47 中的 System.Net.Http

[英]System.Net.Http in net47

I have a normal class library on Windows, using .NET 4.7我在 Windows 上有一个普通的类库,使用 .NET 4.7

I would like to include web-push-csharp .我想包括web-push-csharp

The package lists 2 acceptable dependencies (Newtonsoft Json and BouncyCastle).该软件包列出了 2 个可接受的依赖项(Newtonsoft Json 和 BouncyCastle)。 It also wants to install Microsoft.Net.Http .它还想安装Microsoft.Net.Http And I have no idea why.我不知道为什么。 I don't need it.我不需要它。 My target framework comes with a perfectly fine System.Net.Http to reference.我的目标框架带有一个非常好的System.Net.Http供参考。 As that Microsoft package has a list of other dependencies that clash with other dependencies higher up in my chain, this is a huge inconvenience.由于该 Microsoft 包有一个其他依赖项列表,这些依赖项与我链中更高的其他依赖项发生冲突,这是一个巨大的不便。 Why would I include a package I don't need?为什么我要包含一个我不需要的包?

Now I thought that that should not be a problem, went and cloned the web push package repository, to put my target framework in and... it's correct, you cannot compile for target framework net47 without an outside *.Net.Http reference.现在我认为这应该不是问题,然后克隆了 Web 推送包存储库,将我的目标框架放入其中……这是正确的,如果没有外部*.Net.Http引用,您无法为目标框架net47进行编译。 Yet my Create New Project .NET 4.7 project already comes with such a reference without touching nuget at all.然而,我的Create New Project .NET 4.7 项目已经附带了这样的参考,而根本没有触及 nuget。

Copying all the packages files into a new ClassLibrary1 works great with just NewtonsoftJson and BouncyCastle.将所有包文件复制到新的 ClassLibrary1 中,仅适用于 NewtonsoftJson 和 BouncyCastle。

What am I missing here?我在这里缺少什么? Why did the .NET Framework lose it's System.Net.Http from 4.5 to 4.6?为什么 .NET Framework 从 4.5 到 4.6 失去了它的 System.Net.Http? And why is there no FrameworkTarget available where I can say "this is fine, I have a full 4.7"?为什么没有可用的 FrameworkTarget 可以说“这很好,我有一个完整的 4.7”?

Is there another option aside from just cloning the repository and creating a normal 4.7 class library from it for my use?除了克隆存储库并从中创建一个普通的 4.7 类库供我使用之外,还有其他选择吗?


Additional information:附加信息:

I cloned the web-push-csharp repository and added a new target framework net47 without any dependency on Microsoft.Net.Http :我克隆了 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>

That lead to multiple compile errors along the line of:这会导致以下方面的多个编译错误:

Error CS0234 The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?) WebPush(net47)错误 CS0234 命名空间“System.Net”中不存在类型或命名空间名称“Http”(您是否缺少程序集引用?)WebPush(net47)


Solution解决方案

For anyone reading this looking for a solution: After testing, I incorporated the accepted answer into a branch of a fork and sent the maintainer a Pull request so it can be used in the original package.对于任何阅读本文以寻求解决方案的人:经过测试,我将接受的答案合并到一个 分支的分支中,并向维护者发送了一个拉取请求,以便它可以在原始包中使用。

As you said, the .NET Framework (all mentioned versions: 4.5, 4.6, 4.7) comes with System.Net.Http assembly ready to use, so there is no need to reference any nuget packages.正如您所说,.NET Framework(所有提到的版本:4.5、4.6、4.7)随附了可供使用的System.Net.Http程序集,因此无需引用任何 nuget 包。 But you need to reference this assembly itself:但是你需要引用这个程序集本身:

<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>

In the nuspec file, they didnt mention to support .NET 4.7 ( https://github.com/web-push-libs/web-push-csharp/blob/master/WebPush.nuspec ) Possibly you can take a clone of the repository and add the modify the file as the below在 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>

and package your own nuget file and use it in your project.并打包您自己的 nuget 文件并在您的项目中使用它。 https://programmium.wordpress.com/2014/05/26/keen-io-windows-phone-8-1-and-nuget-package-creation/ this post will tell you how to create a new nuget package. https://programmium.wordpress.com/2014/05/26/keen-io-windows-phone-8-1-and-nuget-package-creation/这篇文章将告诉你如何创建一个新的 nuget 包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM