Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
| __`ASP0027`__ | Unnecessary public Program class declaration |
| __`ASP0028`__ | Consider using ListenAnyIP() instead of Listen(IPAddress.Any) |
| __`ASP0029`__ | Experimental warning for validations resolver APIs |
| __`ASP0030`__ | Use Host.CreateDefaultBuilder instead of WebHost.CreateDefaultBuilder |

### API (`API1000-API1003`)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,13 @@ internal static class DiagnosticDescriptors
DiagnosticSeverity.Info,
isEnabledByDefault: true,
helpLinkUri: AnalyzersLink);

internal static readonly DiagnosticDescriptor UseCreateHostBuilderInsteadOfCreateWebHostBuilder = new(
"ASP0030",
CreateLocalizableResourceString(nameof(Resources.Analyzer_UseCreateHostBuilderInsteadOfCreateWebHostBuilder_Title)),
CreateLocalizableResourceString(nameof(Resources.Analyzer_UseCreateHostBuilderInsteadOfCreateWebHostBuilder_Message)),
Usage,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
helpLinkUri: AnalyzersLink);
}
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,10 @@
<data name="Analyzer_KestrelShouldListenOnIPv6AnyInsteadOfIpAny_Message" xml:space="preserve">
<value>If the server does not specifically reject IPv6, IPAddress.IPv6Any is preferred over IPAddress.Any usage for safety and performance reasons. See https://aka.ms/aspnetcore-warnings/ASP0028 for more details.</value>
</data>
<data name="Analyzer_UseCreateHostBuilderInsteadOfCreateWebHostBuilder_Title" xml:space="preserve">
<value>Use Host.CreateDefaultBuilder instead of WebHost.CreateDefaultBuilder</value>
</data>
<data name="Analyzer_UseCreateHostBuilderInsteadOfCreateWebHostBuilder_Message" xml:space="preserve">
<value>WebHost is deprecated. Use Host.CreateDefaultBuilder and ConfigureWebHostDefaults instead.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using System.Linq;
using Microsoft.AspNetCore.App.Analyzers.Infrastructure;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;

using WellKnownType = Microsoft.AspNetCore.App.Analyzers.Infrastructure.WellKnownTypeData.WellKnownType;

namespace Microsoft.AspNetCore.Analyzers.WebApplicationBuilder;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class UseCreateHostBuilderInsteadOfCreateWebHostBuilderAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
DiagnosticDescriptors.UseCreateHostBuilderInsteadOfCreateWebHostBuilder
);

public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(context =>
{
var compilation = context.Compilation;
var wellKnownTypes = WellKnownTypes.GetOrCreate(compilation);

context.RegisterOperationAction(context =>
{
var invocation = (IInvocationOperation)context.Operation;
var targetMethod = invocation.TargetMethod;

// Check if this is WebHost.CreateDefaultBuilder
if (IsWebHostCreateDefaultBuilderCall(targetMethod, wellKnownTypes))
{
var diagnostic = Diagnostic.Create(
DiagnosticDescriptors.UseCreateHostBuilderInsteadOfCreateWebHostBuilder,
invocation.Syntax.GetLocation()
);
context.ReportDiagnostic(diagnostic);
}
}, OperationKind.Invocation);

context.RegisterSyntaxNodeAction(context =>
{
var methodDeclaration = (MethodDeclarationSyntax)context.Node;
var semantic = context.SemanticModel;
var symbol = semantic.GetDeclaredSymbol(methodDeclaration);

// Check if this method returns IWebHostBuilder
if (symbol != null && IsWebHostBuilderReturnType(symbol, wellKnownTypes))
{
// Check if the method body contains WebHost.CreateDefaultBuilder
if (ContainsWebHostCreateDefaultBuilder(methodDeclaration))
{
var diagnostic = Diagnostic.Create(
DiagnosticDescriptors.UseCreateHostBuilderInsteadOfCreateWebHostBuilder,
methodDeclaration.ReturnType.GetLocation()
);
context.ReportDiagnostic(diagnostic);
}
}
}, SyntaxKind.MethodDeclaration);
});
}

private static bool IsWebHostCreateDefaultBuilderCall(IMethodSymbol method, WellKnownTypes wellKnownTypes)
{
// Check if this is WebHost.CreateDefaultBuilder (not other WebHost methods)
if (method.Name == "CreateDefaultBuilder" &&
SymbolEqualityComparer.Default.Equals(method.ContainingType, wellKnownTypes.Get(WellKnownType.Microsoft_AspNetCore_WebHost)))
{
return true;
}

return false;
}

private static bool IsWebHostBuilderReturnType(IMethodSymbol method, WellKnownTypes wellKnownTypes)
{
// Check if the return type is IWebHostBuilder
var returnType = method.ReturnType;
return SymbolEqualityComparer.Default.Equals(returnType, wellKnownTypes.Get(WellKnownType.Microsoft_AspNetCore_Hosting_IWebHostBuilder));
}

private static bool ContainsWebHostCreateDefaultBuilder(MethodDeclarationSyntax methodDeclaration)
{
// Check if the method contains WebHost.CreateDefaultBuilder calls
var descendants = methodDeclaration.DescendantNodes().OfType<InvocationExpressionSyntax>();

foreach (var invocation in descendants)
{
if (invocation.Expression is MemberAccessExpressionSyntax memberAccess &&
memberAccess.Expression is IdentifierNameSyntax identifier &&
identifier.Identifier.ValueText == "WebHost" &&
memberAccess.Name.Identifier.ValueText == "CreateDefaultBuilder")
{
return true;
}
}

return false;
}
}
Loading
Loading