Hello everyone,
I'm currently developing a module for DNN and I've encountered a problem regarding Dependency Injection (DI) within my search components.
The Core Problem
My module's search controller class inherits from <code>ModuleSearchBase</code>.
Challenge: I am unable to use Constructor Injection to inject my required services (such as <code>IMyService</code> or core DNN services) into this class.
It appears that since <code>ModuleSearchBase</code> is a legacy base class, it is not instantiated by the core DNN DI container, meaning its constructor is not called, and thus injection fails. This often happens because the search indexing process runs in a context where a standard HTTP Request Scope is not available.
My Question
In DNN version 9.x (specifically 9.13.9), what is the best and most recommended approach for accessing services registered via DI (which I have registered in my <code>IDnnStartup</code> implementation) inside a class that inherits from <code>ModuleSearchBase</code>?
Should I rely on one of the following patterns?
Singleton Helper Class: Creating a Singleton helper class that resolves the services during application startup, making them globally accessible.
Scoped Resolution: If my required services have Scoped Dependencies (e.g., a database context), how can I reliably create a new, dedicated <code>IServiceScope</code> within the search context to properly resolve and dispose of these services?
I have attempted methods based on resolving the provider via <code>HttpContextSource.Current?.GetScope()?.ServiceProvider</code>, but this often fails in the non-HTTP execution context of the search indexing process.
Please advise on how to safely access injected services within the DNN Search Indexing context, while adhering to DI best practices where possible.
Thank you very much for your guidance.
These Forums are for the discussion of the open source CMS DNN platform and ecosystem.
For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:
Awesome! Simply post in the forums using the link below and we'll get you started.