Accessing Oqtane DI services/repositories during module install (IInstallable.Install method) #4238
-
When my module is installed, I want to automatically create a page in the Admin Dashboard (i.e. with path under admin/ ) that shows it (its a module for showing info about the state of the application to administrators). So my approach is to implement IModule, IInstallable and in the Install method, use various repository classes to check each site if the page already exists, create it, add module to page etc. As I am sure this code always runs on the server, I thought it more direct to use the repository classes (e.g. ISiteRepository) than the service classes (e.g. ISiteService). Also there was a useful method in the repository class that was not in the service class - GetPageModules(int siteId). However, I haven't found a way to get these class instances from DI ... I thought I could do it via static ServiceActivator class like this: var serviceScope = ServiceActivator.GetScope();
var serviceProvider = serviceScope.ServiceProvider;
var siteRepo = serviceProvider.GetRequiredService<ISiteRepository>(); ... although I get a serviceScope, serviceProvider is null Is there a way to do it? One more question - I notice that even when version 1.0.0 of my module is installed, if I install a new version (1.0.1), the Install method is called with version="1.0.0" ... shouldn't it only be called for versions after the last installed version? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
You might be able to do this with pagetemplates? When oqtane is created for the first time it uses a pagetemplate class! Search the server project. |
Beta Was this translation helpful? Give feedback.
-
@lanthonyneville I believe "routable modules" is what you should use in this case. Please see this blog post: https://www.oqtane.org/blog/!/62/routable-modules You can specify pages to be created within your ModuleInfo.cs class:
In regards to the issue with Repository services, you need to remember that Oqtane is multi-tenant and each tenant can have its own database. When a DI Repository service is instantiated dynamically, there is no HttpContext. Without HttpContext the DI Repository service is unable to determine which tenant it should interact with (as tenants are usually determined by the domain name in your HTTP request). So in this case you need to explicitly specify the tenant prior to calling the Repository service. This is what TenantManager provides:
and if you do not know the TenantId you can iterate through the list of tenants (the TenantRepository is tenant-agnostic) so that the logic is executed for every tenant in the installation:
|
Beta Was this translation helpful? Give feedback.
@lanthonyneville I believe "routable modules" is what you should use in this case. Please see this blog post:
https://www.oqtane.org/blog/!/62/routable-modules
You can specify pages to be created within your ModuleInfo.cs class:
In regards to the issue with Repository services, you need to remember that Oqtane is multi-tenant and each tenant can have its own dat…