runtime: 6.0 RC1: ImplicitUsings with .NET Framework 4.x - System.Net.Http is not available
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
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (10 by maintainers)
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 …
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 https://github.com/dotnet/sdk/issues/19521#issuecomment-903819782
If I target
net20
, I get these errors (trimmed for brevity) and no extra reference can save me if I use implicitusing
s:I think this issue belongs
dotnet/sdk
.Here it is. Try to
dotnet build
it.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.