question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make ImplicitUsings does not work out of the box with .NET Framework project

See original GitHub issue

Apologies if this is not the right repo! Reporting here because there are at least some issues in this repo referencing <ImplicitUsings>

Is your feature request related to a problem? Please describe.

Whenever I create a new class library project, it defaults to setting <ImplicitUsings>enable</ImplicitUsings>. This is good. However, when I multi-target or re-target the project to hit .NET Framework, I get the following compiler error:

The type or namespace name ‘Http’ does not exist in the namespace ‘System.Net’ (are you missing an assembly reference?)

This can be resolved by adding a framework reference, but it would be nice if it “just worked”.

Describe the solution you’d like

I’d like for this to “just work” out of the box. One easy way to do this would be to change the generated usings file (ProjectName.GlobalUsings.g.cs) to look like the following:

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

// new addition
#if NETFRAMEWORK
namespace System.Net.Http { }
#endif

Potentially we could define all the referenced namespaces to ensure that the auto-generated code never gives an error regardless of what framework is targeted and what references are imported.

Additional context

Example .csproj to reproduce the issue:

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

  <PropertyGroup>
    <TargetFrameworks>net472;netstandard2.0;net6.0</TargetFrameworks>
    <ImplicitUsings>enable</ImplicitUsings>
    <LangVersion>10</LangVersion>
  </PropertyGroup>

</Project>

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
baronfelcommented, Apr 26, 2022

At minimum, we should condition the Using for System.Net.Http so that it is only added for TFMs that natively include the reference to System.Net.Http. Perhaps it’s safe enough to use .NET Standard2.0+, .Net Core 3.1, and .Net 5+? This condition would need to be added here

1reaction
baronfelcommented, Jun 14, 2023

Yes, thanks for bringing this to my attention. We should attempt to dedupe these items as a mitigation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Problem with C# 10 Implicit Usings
I discovered the first problem while multi-targetting a class library project for a NuGet package. I had targetted .NET 4.7.2 as well as...
Read more >
C# console app template generates top-level statements
The .NET 6+ project template for C# console apps uses top-level statements. Understand what changed and how to use existing learning ...
Read more >
Implicit Usings in .NET 6 - Scott Hanselman's Blog
First, for new projects this is on by default but for existing projects this will be off by default. This offers the best...
Read more >
Implicit Using Statements In .NET 6 - NET Core Tutorials
When enabled, implicit usings are actually a hidden auto generated file, inside your obj folder, that declares global using statements behind the scenes....
Read more >
Is it possible to use class library written in CORE 6 ...
Yes it is possible. First you should use multiple frameworks and then you need to make nuget package of class library here is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found