Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6.0 RC1: ImplicitUsings with .NET Framework 4.x - System.Net.Http is not available #59163

Closed
buvinghausen opened this issue Sep 15, 2021 · 15 comments

Comments

@buvinghausen
Copy link

buvinghausen commented Sep 15, 2021

I have two NuGet packages I maintain with legacy .NET Framework support for 4.6.2 when I enabled ImplicitUsings on those projects they both failed to compile due to the inclusion of System.Net.Http which the legacy .NET Framework doesn't have available without adding a package dependency on System.Net.Http

Error Message: error CS0234: The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)

Failed workaround:

<ItemGroup>
  <Using Remove="System.Net.Http" />
</ItemGroup>

(Updated)
Workaround:

<ItemGroup Condition="'$(TargetFramework)'=='net462'">
  <Reference Include="System.Net.Http" />
</ItemGroup>

Suggested fix:

// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
#if !NETFRAMEWORK
global using global::System.Net.Http;
#endif
global using global::System.Threading;
global using global::System.Threading.Tasks;

Reproduced here for convenience just remove the package reference

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Net.Http untriaged New issue has not been triaged by the area owner labels Sep 15, 2021
@ghost
Copy link

ghost commented Sep 15, 2021

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

I have two NuGet packages I maintain with legacy .NET Framework support for 4.6.2 when I enabled ImplicitUsings on those projects they both failed to compile due to the inclusion of System.Net.Http which the legacy .NET Framework doesn't have available without adding a package dependency on System.Net.Http

Error Message: error CS0234: The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)

Failed workaround:

<ItemGroup>
  <Using Remove="System.Net.Http" />
</ItemGroup>

Workaround:

<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
 <PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>

Suggested fix:

// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
#if !NETFRAMEWORK
global using global::System.Net.Http;
#endif
global using global::System.Threading;
global using global::System.Threading.Tasks;

Reproduced here for convenience just remove the package reference

Author: buvinghausen
Assignees: -
Labels:

area-System.Net.Http, untriaged

Milestone: -

@MichalPetryka
Copy link
Contributor

MichalPetryka commented Sep 15, 2021

.Net Framework 4.6.2 does include System.Net.Http, especially since it's in .Net Standard 2.0 which .NF 4.6.2 covers. The docs confirm it.

@buvinghausen
Copy link
Author

@MichalPetryka that would be a no.....

image

@karelz
Copy link
Member

karelz commented Sep 15, 2021

@buvinghausen I am not sure I fully understand.
System.Net.Http is empty nuget package and we recommend people to drop reference to it if they can. You seem to claim that it is somehow needed, which is suspicious.
Do you have a small repro demonstrating why is it needed? Which APIs do you use?
They should be part of System.dll (or System.Net.dll or System.Net.Http.dll) on .NET Framework -- all those are part of .NET Framework itself, no need to have NuGet packages (though you may need to play games with binding redirects).

@buvinghausen
Copy link
Author

@karelz please see the OP the bottom has everything you need

@karelz
Copy link
Member

karelz commented Sep 15, 2021

@buvinghausen I see your code link now.
I don't understand what is ImplicitUsings in your context, and why is it needed.

Also, I am not sure what this comment means:
https://github.com/buvinghausen/SequentialGuid/blob/master/src/SequentialGuid/SequentialGuid.csproj#L9

... System.Net.Http is not included in .net framework legacy

Is there a chance to create HelloWorld-style CS file with project which would demonstrate the problem? That will help me identify where to route this. It does not seem to be problem of the NuGet package itself ...

@teo-tsirpanis
Copy link
Contributor

Here it is. Try to dotnet build it.

@buvinghausen
Copy link
Author

@karelz sorry I was on a conference call but it looks like @teo-tsirpanis got it covered.

@AraHaan
Copy link
Member

AraHaan commented Sep 15, 2021

Normally System.Net.Http is not referenced by the .NET SDK by default I think on .NET Framework 4.6.2. I remember before I had to explicitly add <Reference Include=System.Net.Http" /> back when I had projects that targeted .NET Framework at all just to be safe. However that problem went away when I retargeted them all to .NET Standard 2.0 and get away with only targeting a single framework (that would work for all 4.x versions of .NET Framework that implemented standard 2.0, and the newer .NET Core versions that supports standard 2.0 at the same time). After that it made my life easier when targeting old frameworks. I even got a lot of C# 10's features working with standard 2.0 as well like file scoped namespaces, property inits (reference the IsExternalInit nuget package), switch expressions, pattern matching, global usings, nullable reference types (only the language syntax ones like ?, !, etc).

But yes System.Net.Http exists by default with no need to download it separately, reference from a file external from what gets installed when you install the reference assemblies (or the ones provided by the .NET SDK that references a nuget package that acts as if you installed the targeting packs for the .NET Frameworks), etc.

@buvinghausen
Copy link
Author

@AraHaan thank you for the input I was able to add the following lines to my Directory.Build.props files and now everything compiles and tests

<ItemGroup Condition="'$(TargetFramework)'=='net462'">
  <Reference Include="System.Net.Http" />
</ItemGroup>

You can see the commit here

@karelz I don't know if this is something you still want to triage so I will leave it open for the time being but I don't need it kept open on my behalf as I should be able to publish to nuget without the added dependency. This was more an exploration of implicit usings than a must have deal in the end.

@teo-tsirpanis
Copy link
Contributor

If I target net20, I get these errors (trimmed for brevity) and no extra reference can save me if I use implicit usings:

Error CS0234 : The type or namespace name 'Linq' does not exist in the namespace 'System'
Error CS0234 : The type or namespace name 'Http' does not exist in the namespace 'System.Net'
Error CS0234 : The type or namespace name 'Tasks' does not exist in the namespace 'System.Threading'

I think this issue belongs dotnet/sdk.

@buvinghausen
Copy link
Author

@teo-tsirpanis Yeah I would imagine anything below net45 would be SOL. Task<T> came with .NET 4 and System.Linq came with .NET 3.5.

@martincostello
Copy link
Member

FYI I bought this up a few weeks ago in the SDK issue for the global using feature and the response I got was that it was by-design as this feature is only supported for .NET 6 dotnet/sdk#19521 (comment)

@karelz
Copy link
Member

karelz commented Sep 16, 2021

Duplicate of dotnet/sdk#19521

@karelz karelz marked this as a duplicate of dotnet/sdk#19521 Sep 16, 2021
@karelz karelz closed this as completed Sep 16, 2021
@karelz
Copy link
Member

karelz commented Sep 16, 2021

Thanks @martincostello for referencing that. It seems like dupe, so I marked it so. Not much we can do about it in System.Net.Http NuGet package ...

@karelz karelz added this to the 7.0.0 milestone Sep 16, 2021
@karelz karelz added bug and removed untriaged New issue has not been triaged by the area owner labels Sep 16, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants