diff --git a/src/Workleap.Extensions.MediatR.Analyzers/Internals/KnownSymbolNames.cs b/src/Workleap.Extensions.MediatR.Analyzers/Internals/KnownSymbolNames.cs index b61e0d5..25f6ec4 100644 --- a/src/Workleap.Extensions.MediatR.Analyzers/Internals/KnownSymbolNames.cs +++ b/src/Workleap.Extensions.MediatR.Analyzers/Internals/KnownSymbolNames.cs @@ -1,4 +1,4 @@ -namespace Workleap.Extensions.MediatR.Analyzers.Internals; +namespace Workleap.Extensions.MediatR.Analyzers.Internals; internal static class KnownSymbolNames { @@ -8,7 +8,8 @@ internal static class KnownSymbolNames public const string WorkleapExtMediatRAssembly = "Workleap.Extensions.MediatR"; public const string ServiceCollectionInterface = "Microsoft.Extensions.DependencyInjection.IServiceCollection"; - public const string ServiceCollectionExtensionsClass = "Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions"; + public const string MediatROldServiceCollectionExtensionsClass = "Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions"; + public const string MediatRServiceCollectionExtensionsClass = "Microsoft.Extensions.DependencyInjection.MediatRServiceCollectionExtensions"; public const string WorkleapMediatorExtensionsClass = "MediatR.MediatorExtensions"; public const string BaseRequestInterface = "MediatR.IBaseRequest"; diff --git a/src/Workleap.Extensions.MediatR.Analyzers/ServiceRegistrationAnalyzer.cs b/src/Workleap.Extensions.MediatR.Analyzers/ServiceRegistrationAnalyzer.cs index fcb8387..6334f1f 100644 --- a/src/Workleap.Extensions.MediatR.Analyzers/ServiceRegistrationAnalyzer.cs +++ b/src/Workleap.Extensions.MediatR.Analyzers/ServiceRegistrationAnalyzer.cs @@ -1,4 +1,4 @@ -using System.Collections.Immutable; +using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Operations; @@ -44,7 +44,11 @@ private sealed class AnalyzerImplementation public AnalyzerImplementation(Compilation compilation) { this._serviceCollectionType = compilation.GetBestTypeByMetadataName(KnownSymbolNames.ServiceCollectionInterface, KnownSymbolNames.MsExtDIAbstractionsAssembly)!; - this._serviceCollectionExtensionsType = compilation.GetBestTypeByMetadataName(KnownSymbolNames.ServiceCollectionExtensionsClass, KnownSymbolNames.MediatRAssembly)!; + + // Class name changed in MediatR 13.1, this is for backward compatibility + this._serviceCollectionExtensionsType = + compilation.GetBestTypeByMetadataName(KnownSymbolNames.MediatRServiceCollectionExtensionsClass, KnownSymbolNames.MediatRAssembly) ?? + compilation.GetBestTypeByMetadataName(KnownSymbolNames.MediatROldServiceCollectionExtensionsClass, KnownSymbolNames.MediatRAssembly)!; } public bool IsValid => this._serviceCollectionExtensionsType != null; diff --git a/src/Workleap.Extensions.MediatR.Tests/HostBuilderTests.cs b/src/Workleap.Extensions.MediatR.Tests/HostBuilderTests.cs index ae942e9..5cf8ba0 100644 --- a/src/Workleap.Extensions.MediatR.Tests/HostBuilderTests.cs +++ b/src/Workleap.Extensions.MediatR.Tests/HostBuilderTests.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace Workleap.Extensions.MediatR.Tests; public sealed class HostBuilderTests @@ -11,6 +12,12 @@ public sealed class HostBuilderTests public async Task SetLicenseFromConfiguration() { var builder = WebApplication.CreateBuilder(); + builder.Host.UseDefaultServiceProvider(options => + { + // Disabling build on validation since some private classes (like TestState) are not registered + options.ValidateOnBuild = false; + }); + builder.Services.AddMediator(typeof(HostBuilderTests).Assembly); builder.Configuration.AddInMemoryCollection([KeyValuePair.Create("MEDIATR_LICENSE_KEY", "License")]);