Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
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(
"ASP0029",
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;

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))
{
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))
{
// 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)
{
// Check if this is WebHost.CreateDefaultBuilder (not other WebHost methods)
if (method.Name == "CreateDefaultBuilder" &&
method.ContainingType?.Name == "WebHost" &&
method.ContainingType?.ContainingNamespace?.ToDisplayString() == "Microsoft.AspNetCore")
{
return true;
}

return false;
}

private static bool IsWebHostBuilderReturnType(IMethodSymbol method)
{
// Check if the return type is IWebHostBuilder
var returnType = method.ReturnType;
return returnType.Name == "IWebHostBuilder" &&
returnType.ContainingNamespace?.ToDisplayString() == "Microsoft.AspNetCore.Hosting";
}

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