DNN Forums

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

Dependency Injection (DI) in DNN 10 in custom modules: the right way

 10 Replies
 2 Subscribed to this topic
 26 Subscribed to this forum
Sort:
Page 1 of 212 > >>
Author
Messages
Growing Member
Posts: 69
Growing Member

Hi,
I'm upgrading to DNN 10 and would like to know the correct way to approach DI in DNN.
In this context, I have a custom module that exposes a Web Service.
I currently have some legacy code from DNN 7, and I'd like to port it to DNN 10 using the official patterns:

[CODE]
            new PortalAliasController().DeletePortalAlias(httpAliasId);
            List allRolesInTargetPortal = new RoleController().GetRoles().OfType().ToList();
[CODE]

Is it correct to inject the necessary services/data structure this way to convert this code?

[CODE]
        private readonly IServiceScope currentScope = HttpContext.Current.GetScope();
        private readonly IPortalAliasService portalAliasService;
        private readonly IPortalAliasInfo portalAliasInfo;
        private readonly IHostSettings hostSettings;
        private readonly IEventLogService eventLogService;
        private readonly IHostSettingsService hostSettingsService;

        public MyWebService()
        {
            this.portalAliasService = portalAliasService ?? this.currentScope.ServiceProvider.GetRequiredService();
            this.portalAliasInfo = portalAliasInfo ?? this.currentScope.ServiceProvider.GetRequiredService();
            this.hostSettings = hostSettings ?? this.currentScope.ServiceProvider.GetRequiredService();
            this.eventLogService = eventLogService ?? this.currentScope.ServiceProvider.GetRequiredService();
            this.hostSettingsService = hostSettingsService ?? this.currentScope.ServiceProvider.GetRequiredService();
        }
[CODE]

Thanks all.
S.

Veteran Member
Posts: 352
Veteran Member
3 Helpful Replier
Helpful Replier
Thanks for being such a helpful replier!
MVP
MVP
You're an MVP!
2 Engaged Reader
Engaged Reader
You are an engaged reader!
Avid Reader
Avid Reader
Avid Reader art thou!
No, Ideally you would use constructor injection and those services will be injected by DNN for you.

[code]
private readonly IPortalAliasService portalAliasService

public MyWebService(IPortalAliasService portalAliasService)
{
this.portalAliasService = portalAliasService
}

[/code]

Now that will only work if MyWebService is one of DNN entry points, so it will vary a bit but if DNN instantiates it, it will provide any service you ask that is registered in DI.

Then it will also resolve services your services need down from that entry point.

There are a lot of small details to explain it all in a quick forum reply but if you need a demo, I do co-coding sessions on Discord almost every Friday and happy to show you in more details.
Growing Member
Posts: 69
Growing Member

Thank you so much!
So that's it?
Simpler than I imagined :-D

S.

Advanced Member
Posts: 234
Advanced Member
4 Helpful Replier
Helpful Replier
Thanks for being such a helpful replier!
New Poster
New Poster
Congrats on posting!
4 Engaged Reader
Engaged Reader
You are an engaged reader!
Posted By Daniel Valadas on 10/15/2025 4:49 PM

Now that will only work if MyWebService is one of DNN entry points, so it will vary a bit but if DNN instantiates it, it will provide any service you ask that is registered in DI.
Then it will also resolve services your services need down from that entry point.
 

Hi Daniel, can you explain what this means "if MyWebService is one of DNN entry points" ? How is that defined? Thanks.

 

Advanced Member
Posts: 159
Advanced Member
MVP
MVP
You're an MVP!
If we're talking about DNN 10, the short answer is that if DNN or ASP.NET is creating the type, it will have its dependencies filled via Dependency Injection. This includes ascx controls, ashx handlers, MVC controllers, Web API controllers, scheduler clients, business controller classes, etc.

The list is shorter and changes throughout DNN 9.x. WebForms support isn't there at all until DNN 10, but starting in DNN 9.4 there is support for Web API and MVC.
Growing Member
Posts: 69
Growing Member
I read quickly, and I missed this part:
"This includes ascx controls, ashx handlers, MVC controllers, Web API controllers."

So if my class is defined as
[code]
public abstract class ServiceBase: System.Web.Services.WebService
public class MyService: ServiceBase
[/code]

should it already support automatic injection?
Advanced Member
Posts: 159
Advanced Member
MVP
MVP
You're an MVP!
Yes, I believe WebService should be supported in DNN 10 as well.
Veteran Member
Posts: 352
Veteran Member
3 Helpful Replier
Helpful Replier
Thanks for being such a helpful replier!
MVP
MVP
You're an MVP!
2 Engaged Reader
Engaged Reader
You are an engaged reader!
Avid Reader
Avid Reader
Avid Reader art thou!
I have not used WebService much but I'll trust Brian on this ;)

You can put a breakpoint in your constructor and check if you receive instances or null though to be 100% sure.
Growing Member
Posts: 69
Growing Member
How to refactor this?
[code]
[Obsolete("Deprecated in DotNetNuke 10.0.2. Please use overload with IHostSettings. Scheduled removal in v12.0.0.")]
public RoleController()
[/code]
Advanced Member
Posts: 159
Advanced Member
MVP
MVP
You're an MVP!
Rather than using the RoleController constructor directly, add an IRoleController parameter to your constructor, and it'll be supplied via DI.

If there's some reason you can't do that, or a similar situation that's not a constructor, you can add an IHostSettings parameter to your constructor, and then pass it through to the method that needs it.
Page 1 of 212 > >>

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