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

CS0234 'Http' does not exist in the namespace 'System.Net' when targeting .NET Fx 4.x with implicit usings #24146

Closed
chucker opened this issue Mar 1, 2022 · 10 comments
Labels
Area-NetSDK untriaged Request triage from a team member

Comments

@chucker
Copy link

chucker commented Mar 1, 2022

Describe the bug

C# 10 introduces global usings. The .NET 6 SDK ships with a default template for implicit usings. For the regular SDK (e.g., non-Web), this generated C# looks like:

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

However, System.Net.Http is a namespace that does not exist in .NET Framework 4.x. Therefore, this namespace should not be imported implicitly.

To Reproduce

A csproj like this will suffice:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <Nullable>enable</Nullable>
    <LangVersion>10.0</LangVersion>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project>

This will fail to build with:

Error | CS0234 | The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-NetSDK untriaged Request triage from a team member labels Mar 1, 2022
@chucker
Copy link
Author

chucker commented Mar 1, 2022

(As a workaround, you can put namespace System.Net.Http { } somewhere in a C# file.)

@pranavkm
Copy link
Contributor

pranavkm commented Mar 2, 2022

Therefore, this namespace should not be imported implicitly.

The default list of usings are primarily tailored for .NET 6 and newer apps, and aren't enabled by default. You can use the Using itemgroup to specify a custom list of global usings to be used for your app, or use MSBuild to remove ones added by ImplicitUsings=Enable e.g. <Using Remove="System.Net.Http" />.

@pranavkm pranavkm closed this as completed Mar 2, 2022
@Paul-Williams
Copy link

<Using Remove="System.Net.Http" /> does the trick.

@aprillove2858

This comment has been minimized.

@xiaoxstz
Copy link

Which node to put <Using Remove="System.Net.Http" /> ?

@xiaoxstz
Copy link

I solved it by installing the nuget package "System.Net.Http"

@hrumhurum
Copy link

An automatic workaround for the whole solution is to put near .sln file a Directory.Build.targets file with the following content:

<Project>

  <!-- Workaround for https://github.com/dotnet/sdk/issues/24146 -->
  <ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' ">
    <Using Remove="System.Net.Http" />
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' AND $([MSBuild]::VersionLessThan($(TargetFrameworkVersion), '4.0')) ">
    <Using Remove="System.Threading.Tasks" />
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' AND $([MSBuild]::VersionLessThan($(TargetFrameworkVersion), '3.5')) ">
    <Using Remove="System.Linq" />
  </ItemGroup>

</Project>

@Tyler-H
Copy link

Tyler-H commented Aug 9, 2024

An automatic workaround for the whole solution is to put near .sln file a Directory.Build.targets file with the following content:

<Project>

  <!-- Workaround for https://github.com/dotnet/sdk/issues/24146 -->
  <ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' ">
    <Using Remove="System.Net.Http" />
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' AND $([MSBuild]::VersionLessThan($(TargetFrameworkVersion), '4.0')) ">
    <Using Remove="System.Threading.Tasks" />
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' AND $([MSBuild]::VersionLessThan($(TargetFrameworkVersion), '3.5')) ">
    <Using Remove="System.Linq" />
  </ItemGroup>

</Project>

Put "near" it? What does that mean? Do you mean put that content in the .sln file?

@KalleOlaviNiemitalo
Copy link
Contributor

KalleOlaviNiemitalo commented Aug 9, 2024

A Directory.Build.targets file in the same directory as the solution file would be "near" it in this sense.

It isn't really related to the solution file, though. The important thing is that Directory.Build.targets should be in a directory that contains all the per-project subdirectories, so that it affects all of those projects.

@Varorbc
Copy link

Varorbc commented Aug 15, 2024

This issue has been fixed by #32630

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-NetSDK untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

9 participants