Browse Source

Disable conventional registration of InMemoryDynamicBackgroundWorker

InMemoryDynamicBackgroundWorker indirectly implements ISingletonDependency
via IBackgroundWorker, so conventional registration tries to register it
as a service. Its constructor takes a string workerName parameter that
the DI container cannot resolve, which crashes any host that runs
ServiceCollection validation (e.g. ASP.NET Core in Development, where
WebApplicationBuilder.Build() enables ValidateOnBuild). The dynamic worker
is created on demand by DefaultDynamicBackgroundWorkerManager and must
not be auto-registered, so mark it with [DisableConventionalRegistration].
pull/25355/head
maliming 3 months ago
parent
commit
9ee427e8a8
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 2
      framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/InMemoryDynamicBackgroundWorker.cs
  2. 33
      framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundWorkers/InMemoryDynamicBackgroundWorker_Registration_Tests.cs

2
framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/InMemoryDynamicBackgroundWorker.cs

@ -1,10 +1,12 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
namespace Volo.Abp.BackgroundWorkers;
[DisableConventionalRegistration]
public class InMemoryDynamicBackgroundWorker : AsyncPeriodicBackgroundWorkerBase
{
public string WorkerName { get; }

33
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundWorkers/InMemoryDynamicBackgroundWorker_Registration_Tests.cs

@ -0,0 +1,33 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
namespace Volo.Abp.BackgroundWorkers;
public class InMemoryDynamicBackgroundWorker_Registration_Tests
{
[Fact]
public async Task Should_Not_Be_Auto_Registered_To_DI_Container()
{
// Regression guard: IBackgroundWorker derives from ISingletonDependency, so without
// [DisableConventionalRegistration] the conventional registration would register
// InMemoryDynamicBackgroundWorker as a Singleton. Its constructor takes a `string
// workerName` parameter that the DI container cannot resolve, which broke any host
// running ServiceCollection validation (e.g. ASP.NET Core in Development, where
// WebApplicationBuilder.Build() enables ValidateOnBuild).
using var application = await AbpApplicationFactory.CreateAsync<AbpBackgroundWorkersModule>();
application.Services
.Any(d => d.ServiceType == typeof(InMemoryDynamicBackgroundWorker))
.ShouldBeFalse(
"InMemoryDynamicBackgroundWorker is created on demand by DefaultDynamicBackgroundWorkerManager " +
"and must not be auto-registered as a service.");
// Building a fresh provider with ValidateOnBuild = true must not throw.
await using var validatingProvider = application.Services.BuildServiceProvider(
new ServiceProviderOptions { ValidateOnBuild = true });
validatingProvider.ShouldNotBeNull();
}
}
Loading…
Cancel
Save