Hi all,
how can I use dependency injection in a controller class (of a Webforms module) in DNN 9.8.0?
What I am trying to do is using IEventLogger (instead of EventLogController) in a Scheduler client. Something like
IEventLogger _eventLogger = DependencyProvider.GetRequiredService< IEventLogger >();
in the constructor. Any help is appreciated.
Happy DNNing! Michael
Michael TobischDNN★MVP
And another question: How can it be used in the IUpgradeable interface?
public string UpgradeModule(string version)
If you're talking about the module's business controller class, it's not currently supported to use dependency injection with a business controller class (e.g. when implementing IUpgradeable).
IUpgradeable
In a case where you have a controller, like WidgetsController, that your module and scheduler client use, you can register it with the dependency injection container and then retrieve it in any place where you have access. For places where the framework doesn't support DI yet, you'll need to manually supply an instance. For example:
WidgetsController
public class WidgetsController { private readonly IEventLogger eventLogger; public WidgetsController(IEventLogger eventLogger) { this.eventLogger = eventLogger; } public WidgetsController() : this(new EventLogController()) { } }
Startup
public class Startup : IDnnStartup { public void ConfigureServices(IServiceCollection services) { services.AddTransient(); } }
WidgetsSchedulerClient
public class WidgetsSchedulerClient : SchedulerClient { private readonly WidgetsController widgetsController; public WidgetsSchedulerClient(SchedulerHistoryItem item, WidgetsController widgetsController) { this.ScheduleHistoryItem = item; this.widgetsController = widgetsController; } }
ViewWidgets
public class ViewWidgets : PortalModuleBase { private readonly WidgetsController widgetsController; public ViewWidgets(WidgetsController widgetsController) { this.widgetsController = widgetsController ?? this.DependencyProvider.GetRequiredService≪WidgetsController≫(); } public ViewWidgets() : this(null) { } }
Does that address your questions? Once we're able to add support for constructor injection for WebForms and for the business controller class, you can remove those parameterless constructors.
Thanks Brian,
that answers my question. It was just because I got this warning 'EventLogController.AddLog(string, string, EventLogController.EventLogType)' is obsolete: 'Deprecated in 9.8.0. Use Dependency Injection to resolve 'DotNetNuke.Abstractions.Logging.IEventLogger' instead. Scheduled for removal in v11.0.0.'in the BusinessController class - I thought there is a already a way to get rid of it.
'EventLogController.AddLog(string, string, EventLogController.EventLogType)' is obsolete: 'Deprecated in 9.8.0. Use Dependency Injection to resolve 'DotNetNuke.Abstractions.Logging.IEventLogger' instead. Scheduled for removal in v11.0.0.'
Posted By Brian Dukes on 7/17/2023 5:36 AM Nothing has changed at this point, other than that we know that DNN 10 includes support for WebForms constructor injection and business controller class constructor injection.
Awesome... That's great to know! :)
These Forums are dedicated to the discussion of DNN Platform.
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.