1

.NET 6 introduces a new bootstrap syntax that replaces the old Program.cs/Startup.cs mishmosh. The standard template looks like this:

using ThetaRex.Common;

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();

This is all great and good, until you start adding your own code or some other package. Then we start getting SA1200 errors telling us the 'using' statements should be inside a namespace.

warning SA1200: Using directive should appear within a namespace declaration

What is the recommended way of handling the new .NET 6 syntax for program.cs? As a rule, I try to avoid any and all customization of the rules, opting instead to change my code to work with StyleCop out-of-the-box if possible. Is this new bootstrap compatible with StyleCop?

Quark Soup
  • 4,272
  • 3
  • 42
  • 74
  • 1
    Nothing wrong (and completely supported) with going back to the startup.cs format: https://andrewlock.net/exploring-dotnet-6-part-12-upgrading-a-dotnet-5-startup-based-app-to-dotnet-6/ – mxmissile Apr 13 '22 at 17:18
  • I was not able to reproduce. Which version of `StyleCop` are you using? This issue should have been fixed [long ago](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3243) – Guru Stron Apr 13 '22 at 19:19
  • It is the C# compiler and not .NET that provides this feature. It'd be helpful to include the exact warning text to improve the discoverability of this question. – Lance U. Matthews Apr 13 '22 at 19:29
  • @GuruStron - I'm using StyleCop.Analyzers, 1.1.118 and Microsoft.CodeAnalysis.NetAnalyzers, 6.0.0. The message it produces is: **warning SA1200: Using directive should appear within a namespace declaration** – Quark Soup Apr 13 '22 at 20:06
  • @GuruStron It was reintroduced with C#10 and a new bug has been [opened](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3404). – Jacrys Apr 14 '22 at 17:22
  • And the fix, while merged abt 18 mo ago, still hasn't been released GA yet. It is currently in the 1.2.0-beta.304 tag, and will go out to GA when 1.2.0 is released from beta. In order to get this functionality now, one would have to include pre-release in your NuGet search/explorer. – Jacrys Apr 14 '22 at 17:29
  • 1
    @Jacrys OP is not using global usings though – Guru Stron Apr 14 '22 at 17:32
  • @GuruStron That doesn't negate the fact that as I said, the [fix](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/pull/3261) is NOT in production yet, and will not be for some time looking at their [milestones](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/milestone/50), which show 16% progression to 1.2 release. So again, they would have to include prerelease in NuGet to get this functionality. – Jacrys Apr 14 '22 at 17:49
  • @GuruStron - No, I'm not using a Globals.cs. When I tried it, I got the same error in the Globals.cs as I got in Prorgam.cs. The example is directly from the Visual Studio template. So, what's the story? Does StyleCop work with .NET 6 or not? – Quark Soup Apr 14 '22 at 18:19
  • It does if you exclude SA1200 through rules as I showed, if you use [1.2.0-beta.304 or later of the StyleCop.Analyzers package](https://www.nuget.org/packages/StyleCop.Analyzers/1.2.0-beta.304) (This version is only available in prerelease) as I previously explained, or if you use the pre-C#10 method of using startup.cs as @mxmissle explained. – Jacrys Apr 14 '22 at 18:36
  • @Quarkly I know that you are not using them. I have checked yesterday - fix was not released yet, though it should be available in the 1.20 beta package. Though I still was not able to repro your behaviour with the release version. – Guru Stron Apr 14 '22 at 19:16
  • @GuruStron - #1. Open Visual Studio #2. Create a new ASP.NET Core Web API application from the template. Take all the defaults. #3. Add StyleCop.Analyzer and Microsoft.CodeAnalysis.NetAnalyzers. #4 Update all the NuGet packages to the latest version #5 Fix every single StyleCop or FxCop warning #6 add this line of code to Program.cs: "builder.Services.AddTransient();" #7 Add the 'using' statement to resolve the reference. #8 There's your warning. – Quark Soup Apr 14 '22 at 19:33

1 Answers1

-2

You can also use the new global usings if you will be using the namespaces throughout your code.

Simply create a file called Imports.cs (and/or _Imports.razor for Blazor) and put your usings there.

Imports.cs:

global using Microsoft.AspNetCore.Components.Web;
global using System;
global using System.Collections.Generic;
global using System.Linq;

_Imports.razor:

@using System.Net.Http
@using System.Net.Http.Json
@using System.Text.Json
Jacrys
  • 692
  • 1
  • 7
  • 20
  • This gives the same error as having the directives in the Program.cs file. – Quark Soup Apr 13 '22 at 20:09
  • Do you have the following rule in your stylecop.json: "orderingRules": { "usingDirectivesPlacement": "outsideNamespace" } ? https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1200.md – Jacrys Apr 13 '22 at 20:38
  • No, I don't have it in my stylecop.json, Nor does it seem to work. – Quark Soup Apr 13 '22 at 22:41
  • I can confirm if you add that to stylecop.json with StyleCop.Analyzers, 1.1.118 , and Microsoft.CodeAnalysis.NetAnalyzers, 6.0.0.and enable stylecop.json per the documentation (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/EnableConfiguration.md) it does work(https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/Configuration.md#using-directives-placement) – Jacrys Apr 14 '22 at 00:13
  • Here is a repo showing an example: https://github.com/jacrys/StyleCopExample/ And Showing it working: https://github.com/jacrys/StyleCopExample/blob/main/README.md – Jacrys Apr 14 '22 at 15:44
  • Your example doesn't compile. It's impossible to say whether it's working with so many StyleCop and NetAnalyzer violations. And, no, that directive in the StyleCop.json file doesn't seem to do anything. I tried it on a fresh .net web API template. – Quark Soup Apr 14 '22 at 16:10
  • did you follow the instructions to enable the json? – Jacrys Apr 14 '22 at 16:59
  • Yes. My code is fastidious about file headers which doesn't work if stylecop.json isn't recognized as an additional C# analyzer file. Please clean up your code to the point that there aren't any compile or StyleCop errors and I'll take a look at it again, but right now your example isn't very useful. – Quark Soup Apr 14 '22 at 17:07
  • Code has been updated – Jacrys Apr 14 '22 at 17:50