13

I got up and running with Visual Studio 2022 Preview for a couple of days now.

Got the first shock, there is no Startup.cs. Thats ok, a bit of reading, I know Startup is removed.

Today got another slap. I see no using statements. Here it is.

I just created a brand new .NET 6 web app and as I hover over the WebApplication class, I realized it stays in Microsoft.AspNetCore.Builder namespace. And the generated Program.cs class looks like this.

Program.cs class in ASP.NET core 6 Web app

So where is the using Microsoft.AspNetCore.Builder; statement?

Whats the magic? Why is .net becoming mystical by the day?

The full Program.cs file is as follows.

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();
VivekDev
  • 20,868
  • 27
  • 132
  • 202
  • Is there `global using Microsoft.AspNetCore.Builder;` somewhere in your application? Otherwise it would seem like [this feature](https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/6.0/implicit-namespaces) seems to add an assortment of global usings to your application which are unknowable unless you dig around in your obj folder. Fun! – Nathan Cooper Oct 27 '21 at 12:56
  • 4
    `using` *directive*. `using` statements are those which appear as statements in bodies and control Disposal. And if you use the right phrase and go the the documentation for [using directive](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive) you'd have seen this new behaviour explained. – Damien_The_Unbeliever Oct 27 '21 at 12:59
  • @Nathan Cooper, No I don't – VivekDev Oct 27 '21 at 13:07
  • @Damien_The_Unbeliever, yes its using directive. – VivekDev Oct 27 '21 at 13:08

3 Answers3

26

C# 10.0 introduces a new feature called global using directive (global using <fully-qualified-namespace>;) which allows to specify namespaces to be implicitly imported in all files in the compilation. .NET 6 RC1 has this feature enabled by default in new project templates (see <ImplicitUsings>enable</ImplicitUsings> property in your .csproj).

For Microsoft.NET.Sdk.Web next namespaces should be implicitly imported (plus the ones from Microsoft.NET.Sdk):

  • System.Net.Http.Json
  • Microsoft.AspNetCore.Builder
  • Microsoft.AspNetCore.Hosting
  • Microsoft.AspNetCore.Http
  • Microsoft.AspNetCore.Routing
  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Hosting
  • Microsoft.Extensions.Logging

UPD

To address your questions in comment:

At the moment of writing the generated file containing default imports will be inside the obj folder named something like ProjectName.GlobalUsings.g.cs.

To modify default imports you can add Using element to your .csproj file. Based on exposed attributes it allows several actions including addition and removal:

<ItemGroup>
    <Using Include="SomeFullyQualifiedNamespace"/>
</ItemGroup>

For just addition you can simply prefix your using directive with global modifier in any file (or create a separate one just for this):

global using SomeFullyQualifiedNamespace;
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Hi @Guru Stron, Thanks. Yes indeed there is a tag in enable in my WebApplication1.csproj file. So now, is there a file for this, located globally as I install .net 6 Sdk? If yes, where is the location? Can I customize that, that is, can I add some using directives that I feel I use regularly as a developer? – VivekDev Oct 28 '21 at 07:07
  • 3
    @VivekDev the generated file containing default imports will be inside the `obj` folder named something like `ProjectName.GlobalUsings.g.cs`. As for adding your global directives - you can just use `global` modifier on `using` directive in any file (or create separate one for global usings) or add `` to your .csproj. – Guru Stron Oct 28 '21 at 14:18
  • Related question [Should using directives be inside or outside the namespace?](https://stackoverflow.com/questions/125319) – surfmuggle Jun 21 '22 at 20:39
0

It seems like you can still add your own using directives at the top of Program.cs. I am aware that I am not showing you all my code, but I was able to build and run something that looks like this:

using MvcPrototype1.DataAccess;
using MvcPrototype1.DataAccess.Interfaces;


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews()
    .AddXmlSerializerFormatters();

builder.Services.AddScoped<IAddressRepository, AddressRepository>();
builder.Services.AddAutoMapper(typeof(Program));

var app = builder.Build();
...
user3042674
  • 187
  • 1
  • 7
0
  • In .net 6.0 you don't need to add some namespaces, because they are excited by default.

  • For example:-
    just hello world example

  • Here we don't have to use the namespace because it's exist by default.

  • So where to find it? and know all the namespaces that were installed by default in my project.

  • Just go to .csproj project files and you will find enable and that's it.

  • So by default :-

  • if your applications is a console app this packages installed by default:-
    1 - System
    2 - System.Collections.Generic
    3 - System.IO
    4 - System.Linq
    5 - System.Net.Http
    6 - System.Threading
    7 - System.Threading.Tasks

  • if your applications is a web app this packages installed by default:-
    1 - System.Net.Http.Json
    2 - Microsoft.AspNetCore.Builder
    3 - Microsoft.AspNetCore.Hosting
    4 - Microsoft.AspNetCore.Http
    5 - Microsoft.AspNetCore.Routing
    6 - Microsoft.Extensions.Configuration
    7 - Microsoft.Extensions.DependencyInjection
    8 - Microsoft.Extensions.Hosting
    9 - Microsoft.Extensions.Logging

Mahmmoud Kinawy
  • 551
  • 4
  • 11