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.
Thank you so much! So that's it? Simpler than I imagined :-D S.
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.
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.