Monday, August 23, 2021

.NET 6 Preview 7: Introducing Implicit Namespaces

If you have created a Console Application Project, an ASP.NET Core Web App/Web API project, or a Worker Service Project using Visual Studio 2022 latest preview (17.0.0 Preview 3.1) targetting the latest .NET 6 Preview (.NET 6 Preview 7), you will see a significant change to the template code/boilerplate code that is generated.

All of them now uses Top-Level statements and most importantly you won't see all the usings that were previously there. 

Console Application

Console Application Project (Microsoft.NET.Sdk)

ASP.NET Core Web API

ASP.NET Core Web API Project (Microsoft.NET.Sdk.Web)

Worker Service

Worker Service Project (Microsoft.NET.Sdk.Worker)
But all the projects are compiling fine, so how is this possible, in this post, we are going to find it out.

This is all made possible via Global Usings that got introduced as part of C# 10. The .NET SDK  now implicitly includes a set of default namespaces for C# projects which targets   .NET 6 or later and use one of the following SDKs:

  • Microsoft.NET.Sdk
  • Microsoft.NET.Sdk.Web
  • Microsoft.NET.Sdk.Worker
Microsoft.NET.Sdk
// <autogenerated />
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;
Microsoft.NET.Sdk.Web
// <autogenerated />
// Microsoft.NET.Sdk related global usings plus the following
global using global::System.Net.Http.Json;
global using global::Microsoft.AspNetCore.Builder;
global using global::Microsoft.AspNetCore.Hosting;
global using global::Microsoft.AspNetCore.Http;
global using global::Microsoft.AspNetCore.Routing;
global using global::Microsoft.Extensions.Configuration;
global using global::Microsoft.Extensions.DependencyInjection;
global using global::Microsoft.Extensions.Hosting;
global using global::Microsoft.Extensions.Logging;
Microsoft.NET.Sdk.Worker
// <autogenerated />
// Microsoft.NET.Sdk related global usings plus the following
global using global::Microsoft.Extensions.Configuration;
global using global::Microsoft.Extensions.DependencyInjection;
global using global::Microsoft.Extensions.Hosting;
global using global::Microsoft.Extensions.Logging;
You can find these global usings inside in the projects obj directory in {ProjectName}.ImplicitNamespaceImports.cs file.

You can use DisableImplicitNamespaceImports property to disable this feature completely,
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
Or alternatively, you can disable only a set of implicit namespaces, using one of the following SDK-specific properties.
<!--Disable implicit namespaces in Microsoft.NET.Sdk-->
<DisableImplicitNamespaceImports_DotNet>true</DisableImplicitNamespaceImports_DotNet>

<!--Disable implicit namespaces in Microsoft.NET.Sdk.Web-->
<DisableImplicitNamespaceImports_Web>true</DisableImplicitNamespaceImports_Web>

<!--Disable implicit namespaces in Microsoft.NET.Sdk.Worker-->
<DisableImplicitNamespaceImports_Worker>true</DisableImplicitNamespaceImports_Worker>
or you can add or remove individual namespaces using the <Import> item group. For example, let's in a project that targets Microsoft.NET.Sdk, I need to remove System.Net.Http and add System.Text.Json to Implicit Namespaces.
<ItemGroup>
  <Import Remove="System.Net.Http" />
  <Import Include="System.Text.Json" />
</ItemGroup>
Then the implicit usings would be as follows.
// <autogenerated />
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Threading;
global using global::System.Threading.Tasks;
global using global::System.Text.Json;
Hope this helps.

More read:

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment