2

I have a normal class library on Windows, using .NET 4.7

I would like to include web-push-csharp.

The package lists 2 acceptable dependencies (Newtonsoft Json and BouncyCastle). It also wants to install 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. 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. 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. Yet my Create New Project .NET 4.7 project already comes with such a reference without touching nuget at all.

Copying all the packages files into a new ClassLibrary1 works great with just NewtonsoftJson and BouncyCastle.

What am I missing here? Why did the .NET Framework lose it's System.Net.Http from 4.5 to 4.6? And why is there no FrameworkTarget available where I can say "this is fine, I have a full 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?


Additional information:

I cloned the web-push-csharp repository and added a new target framework net47 without any dependency on 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)


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.

3
  • Is it really related to .net 4.7? I think you cannot compile that project if you remove dependency on that package even for net45 and net46? Commented Oct 17, 2017 at 13:32
  • @Evk hm, true it won't work for the others either, I'll remove that tag. Commented Oct 17, 2017 at 13:35
  • Remove that PackageReference, add <Reference Include="System.Net.Http" /> (regular, non-nuget reference) and it will compile for both 4.5\6 and 4.7. Commented Oct 17, 2017 at 13:38

2 Answers 2

3

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. 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>
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

6 Comments

But the point is that I don't want "Microsoft.Net.Http". But I already went and added net47 to the frameworks and web-push-csharp won't compile without that reference. What leads me to my question: why. I have System.Net.Http in 4.7 already.
Its because you said in the nuspec file. If you want to use System.Net.Http alter the <dependency id="Microsoft.Net.Http" version="2.2.29" /> as <dependency id="System.Net.Http" version="4.3.3" /> But this is not recommended as the nuget package is created for Microsoft.Net.Http not for System.Net.Http. If you really really want to use System.Net.Http just rewrite the entire thing with the removal of Microsoft.Net.Http and introduce System.Net.Http
But why would I need a nuget package at all, for something that in my target is an automatic reference when I say "Create new project"?
If you already have the dependency referenced (with the correct version) in your project it will be totally fine, else it will be downloaded.
Further more System.Net does not have an implementation for Http ref - msdn.microsoft.com/en-us/library/system.net(v=vs.110).aspx you need to reference the nuget.org/packages/System.Net.Http to the .csproj file you've mentioned in your question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.