DNN Forums

Ask questions about your website to get help learning DNN and help resolve issues.

Dependency Injection (DI) issue in classes inheriting from ModuleSearchBase

 3 Replies
 2 Subscribed to this topic
 30 Subscribed to this forum
Sort:
Author
Messages
New Around Here
Posts: 3
New Around Here

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?

  1. Singleton Helper Class: Creating a Singleton helper class that resolves the services during application startup, making them globally accessible.

  2. 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.

Veteran Member
Posts: 349
Veteran Member
3 Helpful Replier
Helpful Replier
Thanks for being such a helpful replier!
MVP
MVP
You're an MVP!
Engaged Reader
Engaged Reader
You are an engaged reader!
Avid Reader
Avid Reader
Avid Reader art thou!
Constructor injection for that particular entry point will just work in DNN 10 when you upgrade. If you need DI there before upgrading to 10, you could do resolve the internal ServiceProvider using reflection like this:

[code]
var globalsType = typeof(DotNetNuke.Common.Globals);
var serviceProviderProperty = globalsType.GetProperty("DependencyProvider", BindingFlags.NonPublic | BindingFlags.Static);
var serviceProvider = (IServiceProvider)serviceProviderProperty.GetValue(null);
this.navigationManager = serviceProvider.GetRequiredService();
[/code]

Note: This is not best practice but a temporary solution until you get to DNN10, I would put a TODO in there to refactor that for simplicity and performance one you can target DNN10.
New Around Here
Posts: 3
New Around Here
I reviewed your approach, but unfortunately I wasn’t able to use it.
The issue is with **IPortalSettings** — it remains **NULL** with every method I have tried.
Do you have any alternative solution?
Veteran Member
Posts: 349
Veteran Member
3 Helpful Replier
Helpful Replier
Thanks for being such a helpful replier!
MVP
MVP
You're an MVP!
Engaged Reader
Engaged Reader
You are an engaged reader!
Avid Reader
Avid Reader
Avid Reader art thou!
I will be talking, doing a demo of DI in DNN in tomorrows co-coding session on discord. You can join our discord using a link we have on the home page of this site. If that interests you I'll also do a bit of Q&A there and we could look more deeply into it.

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:

  1. If you have (suspected) security issues, please DO NOT post them in the forums but instead follow the official DNN security policy
  2. No Advertising. This includes the promotion of commercial and non-commercial products or services which are not directly related to DNN.
  3. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  4. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  5. No Flaming or Trolling.
  6. No Profanity, Racism, or Prejudice.
  7. Site Moderators have the final word on approving / removing a thread or post or comment.
  8. English language posting only, please.

Would you like to help us?

Awesome! Simply post in the forums using the link below and we'll get you started.

Get Involved