This allows me to automatically upgrade my document types and content to match changes for a particular application release, e.g. new properties, migrating data, etc.
So, I have created a migration step with the following code:
When I inject the IArticleService dependency then the migration just doesn't work and no exception is generated. In debug, I can see that the Upgrader is executing the MigrationPlan but the Migrate method in this individual migration step is not being called.
When I remove the IArticleService injection then the Migrate method is being called correctly, so it's an issue with injecting that article service into the migration step.
The article service is being registered as follows:
When I change this to AddSingleton, then the Migrate method is being called with the injected article service but then I have to make the EF ApplicationDBContext a singleton and that's not advisable.
But, even when I make this all singleton, the call to the article service fails without an error when executing the migration plan, so that doesn't seem to be the solution either.
What happens if you register IArticleService as Transient rather than Scoped? I can't entirely recall what the different levels are (well more importantly their order).
Additionally, it might not be possible to inject it in the way you are, you might need to inject in the IServiceProvider (I think that's it's name) class which is the DI container, and use that to resolve the specific service during the method call not the constructor call. I had to do this to access the IPublishedContentQuery in some custom code in the past.
I tried registering the service as Transient but it still failed to enter the Migrate method so I assume it can't inject the service.
I also tried injecting IServiceProvider and then getting the ArticleService directly but it fails with an exception "Cannot resolve scoped service from root provider".
I have managed to get it working using Singleton but then I have to setup the EF DBContext as Singleton too and that is apparently a very bad idea for an ASP.NET application.
I got it working by injecting the IServiceProvider, creating a scope, and then obtaining the service directly:
var scope = _serviceProvider.CreateScope();
var articleService = scope.ServiceProvider.GetRequiredService<IArticleService>();
I guess the Umbraco migration is a singleton which is why I couldn't inject the scoped service into my migration step, so once I created a scope in the migration step, then I was able to resolve the scoped service.
Dependency Injection in MigrationPlan using Composer and Component
I am creating a custom Umbraco MigrationPlan using a Composer and Component method as outlined in this article:
https://cornehoskam.com/posts/how-to-create-run-migrations-umbraco-v10
This allows me to automatically upgrade my document types and content to match changes for a particular application release, e.g. new properties, migrating data, etc.
So, I have created a migration step with the following code:
When I inject the IArticleService dependency then the migration just doesn't work and no exception is generated. In debug, I can see that the Upgrader is executing the MigrationPlan but the Migrate method in this individual migration step is not being called.
When I remove the IArticleService injection then the Migrate method is being called correctly, so it's an issue with injecting that article service into the migration step.
The article service is being registered as follows:
When I change this to AddSingleton, then the Migrate method is being called with the injected article service but then I have to make the EF ApplicationDBContext a singleton and that's not advisable.
But, even when I make this all singleton, the call to the article service fails without an error when executing the migration plan, so that doesn't seem to be the solution either.
Hi Jason,
What happens if you register IArticleService as Transient rather than Scoped? I can't entirely recall what the different levels are (well more importantly their order).
Additionally, it might not be possible to inject it in the way you are, you might need to inject in the IServiceProvider (I think that's it's name) class which is the DI container, and use that to resolve the specific service during the method call not the constructor call. I had to do this to access the IPublishedContentQuery in some custom code in the past.
Thanks
Nik
I tried registering the service as Transient but it still failed to enter the Migrate method so I assume it can't inject the service.
I also tried injecting IServiceProvider and then getting the ArticleService directly but it fails with an exception "Cannot resolve scoped service from root provider".
I have managed to get it working using Singleton but then I have to setup the EF DBContext as Singleton too and that is apparently a very bad idea for an ASP.NET application.
I got it working by injecting the IServiceProvider, creating a scope, and then obtaining the service directly:
I guess the Umbraco migration is a singleton which is why I couldn't inject the scoped service into my migration step, so once I created a scope in the migration step, then I was able to resolve the scoped service.
is working on a reply...