fluentmigrator: Unable to resolve service for type 'FluentMigrator.Runner.Processors.ProcessorOptions'

Describe the bug Hopefully something I’m doing wrong or missing when upgrading. Please can you assist.

Upgraded FluentMigrator from version 5.2.0 to version 6.0.

Previous AddFluentMigratorCore configuration no longer works and throws an invalid operation exception.

 ---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: FluentMigrator.Runner.MigrationScopeHandler Lifetime: Scoped ImplementationType: FluentMigrator.Runner.MigrationScopeHandler': Unable to resolve service for type 'FluentMigrator.Runner.Processors.ProcessorOptions' while attempting to activate 'FluentMigrator.Runner.MigrationScopeHandler'.
 ---> System.InvalidOperationException: Unable to resolve service for type 'FluentMigrator.Runner.Processors.ProcessorOptions' while attempting to activate 'FluentMigrator.Runner.MigrationScopeHandler'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(ServiceIdentifier serviceIdentifier, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, ServiceIdentifier serviceIdentifier, Type implementationType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain, Int32 slot)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
   at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
   at Program.<Main>$(String[] args)

To Reproduce Use the IWebHostBuilder configuration below

 builder.ConfigureServices( ( _, services ) =>
     {
		//Configure FluentMigrator
		services.AddFluentMigratorCore()
			.ConfigureRunner( rb => rb.AddSqlServer() )
			.AddLogging( x => x.AddSerilog() )
			.Configure<FluentMigratorLoggerOptions>( cfg =>
			{
				cfg.ShowSql = true;
				cfg.ShowElapsedTime = true;
			} )
			.BuildServiceProvider();

		//Configure FluentMigrator ConnectionStringReader
		services
			.AddSingleton<RepositoryConnectionStringReader>()
			.AddScoped<IConnectionStringReader>(
				sp => sp.GetRequiredService<RepositoryConnectionStringReader>() )
			.BuildServiceProvider( true );
	});

Expected behavior The configuration to work and build.

Information (please complete the following information):

  • OS: Windows 11
  • Platform: .NET 8
  • FluentMigrator version 6.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 17 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I’ve checked the code, seems like it’s simply should not work since wrong type used in MigrationScopeHandler constructor. ProcessorOptions should be replaced with actual Options wrapper, e.g. IOptions<ProcessorOptions>. Workaround worked for me to resolve this issue: services.AddSingleton(sp => sp.GetRequiredService<IOptions<ProcessorOptions>>().Value);

New approach: In AddFluentMigratorCore() add the following line:

.TryAddSingleton(sp => sp.GetRequiredService<IOptionsSnapshot<ProcessorOptions>>().Value);

This depends on IOptionsSnapshot<ProcessorOptions> being always registered - is this the case to be guaranteed? -> EDIT 3: I guess so, because SQLiteRunnerBuilderExtensions.AddSQLite(...) already depends on it. ✅

EDIT: Fixed registration. ⚠️ But then still it seems you cannot override ProcessorOptions or IOptions<ProcessorOptions> by registering it manually…

EDIT 2: Well, the problem with “you cannot override” seems unrelated, because I tried to override with IOptions<ProcessorOptions> but FluentMigrator resolves IOptionsSnapshot<ProcessorOptions>, so this approach might work I think. ✅

I am migrating from version 5.2.0.