package seems to break functional testing with a Microsoft.AspNetCore.TestHost.Testserver
See original GitHub issueI’m using Quartz like this in my code and everything works as expected:
services.AddQuartz(q =>
{
q.SchedulerId = "CutOff-time-scheduler";
q.UseMicrosoftDependencyInjectionScopedJobFactory(options =>
{
options.CreateScope = true;
options.AllowDefaultConstructor = false;
});
q.ScheduleJob<SendEventIfMarketCutOffTimePassedJob>(trigger => trigger
.WithIdentity("daily-trigger")
.WithCronSchedule(configuration["CutOffTimeCronSchedule"])
.WithDescription("triggers the 'SendEventIfMarketECutOffTimePassed' job every day")
.StartNow()
);
});
services.AddQuartzHostedService(options =>
{
// when shutting down we want jobs to complete gracefully
options.WaitForJobsToComplete = true;
});
At the same time there are a bunch of functional tests using an IClassFixture like this:
[Collection("Sequential")]
public class AssetFunctionalTest : IClassFixture<TestFixture>
{
private readonly IMarketRepository _marketRepository;
private readonly HttpClient _client;
private const string AssetUrl = "/WENGRIN/MicroService-Asset/Asset";
public AssetFunctionalTest(TestFixture fixture)
{
_client = fixture.Client;
_marketRepository = fixture.MarketRepository;
}
Where the TestFixture class creates a test server like this:
protected TestFixture(string relativeTargetProjectParentDir)
{
var startupAssembly = typeof(Startup).GetTypeInfo().Assembly;
var contentRoot = GetProjectPath(relativeTargetProjectParentDir, startupAssembly);
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(contentRoot)
.AddJsonFile("appsettings.Development.json");
var webHostBuilder = new WebHostBuilder()
.UseContentRoot(contentRoot)
.ConfigureServices(InitializeServices)
.UseConfiguration(configurationBuilder.Build())
.UseEnvironment("Development")
.UseStartup(typeof(Startup));
// Create instance of test server
_server = new TestServer(webHostBuilder);
Client = _server.CreateClient();
MarketRepository = _server.Services.CreateScope().ServiceProvider.GetService<IMarketRepository>();
Client.BaseAddress = new Uri("http://localhost:44347");
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
And cleans it up after one test class has finished running all tests.
public void Dispose()
{
Client?.Dispose();
_server?.Dispose();
}
When I start the tests, the first test class runs fine, but all others fail with the same error message while trying to create the test server.
Message: System.AggregateException : One or more errors occurred. (Cannot access a disposed object. Object name: ‘LoggerFactory’.) (The following constructor parameters did not have matching fixture data: TestFixture fixture) ---- System.ObjectDisposedException : Cannot access a disposed object. Object name: ‘LoggerFactory’. ---- The following constructor parameters did not have matching fixture data: TestFixture fixture Stack Trace: ----- Inner Stack Trace #1 (System.ObjectDisposedException) ----- LoggerFactory.CreateLogger(String categoryName) MicrosoftLoggingProvider.GetLogger(String name) LogProvider.GetLogger(String name) LogProvider.GetLogger(Type type, String fallbackTypeName) XMLSchedulingDataProcessor.ctor(ITypeLoadHelper typeLoadHelper) ContainerConfigurationProcessor.ctor(ITypeLoadHelper typeLoadHelper, IOptions`1 options)
When I don’t start the scheduler, the tests run fine again.
I’m using this version:
<PackageReference Include="Quartz" Version="3.2.4" />
<PackageReference Include="Quartz.Extensions.DependencyInjection" Version="3.2.4" />
<PackageReference Include="Quartz.Extensions.Hosting" Version="3.2.4" />
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
Perhaps this could help.
https://github.com/HangfireIO/Hangfire/issues/1099
I will think about it. Thank you very much.