1

I am trying to get my hands around using Dependency Injection with DNN. I looked at the DotNetNuke.Startup class and see the following:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<WebFormsModuleControlFactory>();
    services.AddSingleton<Html5ModuleControlFactory>();
    services.AddSingleton<ReflectedModuleControlFactory>();
    services.AddSingleton<IDnnContext, DotNetNukeContext>();
    services.AddScoped<IEventLogger, EventLogController>();
    services.AddScoped<IEventLogConfigService, EventLogController>();
    services.AddScoped<IEventLogService, EventLogController>();
    services.AddTransient((IServiceProvider x) => ServiceLocator<IPortalController, PortalController>.Instance);
    services.AddScoped<IHostSettingsService, HostController>();
    services.AddScoped<INavigationManager, NavigationManager>();
    services.AddScoped<ISerializationManager, SerializationManager>();
    services.AddScoped<IApplicationInfo, DotNetNuke.Application.Application>();
    services.AddScoped<IApplicationStatusInfo, ApplicationStatusInfo>();
    services.AddScoped<IPortalAliasService, PortalAliasController>();
}

I have an understanding on how to wire up those services, but my confusion comes into play on how to register other dependencies that are not specific to DNN services for a Web Forms application (a new custom service).

Does anyone have any experience with this? In my Web Forms project, do I have to create a new Startup class that inherits the DNN Interface for Startup? Any examples would be greatly appreciated.

James
  • 63
  • 4

2 Answers2

0

Utilizing Dependency Injection from WebForms with DNN is ever so slightly different, but this article provides the step-by-step instructions on using it in your project.

The short summary is you create your own registration class using IDnnStartup and then use the Service Locator to get the instance.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
  • Thank you for the reply. Can you please help me understand how the registration class will get invoked? For example, if I created a new class named Startup.cs that uses IDnnStartup, where do I place that file and how does it get invoked? If I have a Web.UI project and want to utilize DI in that project, I am missing the concept of where to place Startup.cs and invoking it? Thank you! – James Feb 11 '22 at 16:40
0

Here is some code I use to inject a specific dependency, in this case the event logger. I know this is not answering your question directly, but it should point you in the right direction.

using DotNetNuke.Abstractions;
using DotNetNuke.Abstractions.Logging;
using Microsoft.Extensions.DependencyInjection;

namespace myCompany.DNN.Modules.myModule {
   private readonly IEventLogger _eventLogger;
   public class myControl {
      public myControl() {      // this is the constructor of the class
         _eventlogger = DependencyProvider.GetRequiredService<IEventLogger>();
      }
   }

   protected override void someEvent(object sender, EventArgs e) {
      try {
         // some code
      } catch(Exception ex) {
         _eventLogger.AddLog("Problem Getting Product Description, Title, or Image URL.", ex.ToString(), EventLogController.EventLogType.ADMIN_ALERT);
      }
   }
}

And the link that Mitchel provided is very useful to understand Dependency Injection in DNN.

Michael Tobisch
  • 1,034
  • 6
  • 15