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
<code>IEventLogger _eventLogger = DependencyProvider.GetRequiredService< IEventLogger >();</code>
in the constructor. Any help is appreciated.
Happy DNNing! Michael
And another question: How can it be used in the IUpgradeable interface?
<code>public string UpgradeModule(string version)</code>
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 <code>IUpgradeable</code>).
In a case where you have a controller, like <code>WidgetsController</code>, 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:
<code>public class WidgetsController { private readonly IEventLogger eventLogger; public WidgetsController(IEventLogger eventLogger) { this.eventLogger = eventLogger; } public WidgetsController() : this(new EventLogController()) { } }</code>
<code>public class Startup : IDnnStartup { public void ConfigureServices(IServiceCollection services) { services.AddTransient<widgetscontroller>(); } }</widgetscontroller></code>
<code>public class WidgetsSchedulerClient : SchedulerClient { private readonly WidgetsController widgetsController; public WidgetsSchedulerClient(SchedulerHistoryItem item, WidgetsController widgetsController) { this.ScheduleHistoryItem = item; this.widgetsController = widgetsController; } }</code>
<code>public class ViewWidgets : PortalModuleBase { private readonly WidgetsController widgetsController; public ViewWidgets(WidgetsController widgetsController) { this.widgetsController = widgetsController ?? this.DependencyProvider.GetRequiredService≪WidgetsController≫(); } public ViewWidgets() : this(null) { } }</widgetscontroller></code>
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 <code>'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.'</code>in the BusinessController class - I thought there is a already a way to get rid of it.
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 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.