DNN Forums

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

How to replace HostController by IHostSettingsService?

Sort:
You are not authorized to post a reply.





Veteran Member





    Hi all,

    in a custom module I get a warning saying

    Obsolete("Deprecated in 9.7.1. Scheduled for removal in v11.0.0, use DotNetNuke.Abstractions.IHostSettingsService instead.")

    The line that throws this warning is

    DotNetNuke.Entities.Controllers.HostController.Instance.GetSettingsDictionary().TryGetValue("FileExtensions", out allowedExtensions)

    Can anyone provide an example of how to replace this? (beside the fact that the warning is misleading, as it should be DotNetNuke.Abstractions.Application.IHostSettingsService)

    Happy DNNing!
    Michael

    Michael Tobisch
    DNN★MVP

    dnnWerk Austria
    DNN Connect





    Growing Member





      Hi Michael,

      If I'm not mistaken, you should simply use
      DotNetNuke.Entities.Host.Host.AllowedExtensionWhitelist
      .
      There are more properties on the Host class, to access other settings.

      Hope this helps.





      Advanced Member





        Assuming that this is a WebForms module, you will want to introduce a field to store the IHostSettingsService, and typically you would initialize it in the constructor, using the DependecyProvider property that you inherit from PortalModuleBase. See https://www.andrewhoeflin...ion-webforms-modules for more details.
        FYI, there's a pending pull request which would make WebForms modules work the same way as Web API controllers and MVC controllers, i.e. just introducing a constructor with IHostSettingsService as a parameter.
        DNN partner specializing in custom, enterprise DNN development https://engagesoftware.com/showcase





        Veteran Member





          Thank you both,

          Brian, if I understand that right, it is done like with the INavigationManager.

          public MyControl()
          {
            _hostSettings = DependencyProvider.GetRequiredService< IHostSettingsService >();
          }

          private readonly IHostSettingsService _hostSettings;

          protected void SomeMethod()
          {

            string allowedExtensions;
            if (!(_hostSettings.GetSettingsDictionary().TryGetValue("FileExtensions", out allowedExtensions)))
            {
             …

          That did work (without the spaces inside the angle brackets), thank you...

          Happy DNNing!
          Michael

          Michael Tobisch
          DNN★MVP

          dnnWerk Austria
          DNN Connect





          New Around Here





            Hi,

            When I try something like this:

            Snippet <!--StartFragment-->

            IHostSettingsService hostSettingsService = DotNetNuke.Common.Globals.DependencyProvider.GetRequiredService < IHostSettingsService > ();
            

            <!--EndFragment-->

            It doesn't work. Error: "'Globals' does not contain a definition for 'DependencyProvider'"

            This error makes sense (cannot access it from my code) because DependencyProvider is internal in Dotnetnuke.dll

            internal static IServiceProvider DependencyProvider
                    {
                        get
                        {
                            return Globals.dependencyProvider;
                        }
                        set
                        {
                            Globals.dependencyProvider = value;
                            INotifyPropertyChanged notifyPropertyChanged = Globals.dependencyProvider as INotifyPropertyChanged;
                            INotifyPropertyChanged notifyPropertyChanged1 = notifyPropertyChanged;
                            if (notifyPropertyChanged == null)
                            {
                                Globals.OnDependencyProviderChanged(null, null);
                                return;
                            }
                            notifyPropertyChanged1.PropertyChanged += new PropertyChangedEventHandler(Globals.OnDependencyProviderChanged);
                        }
                    }

             

            Therefore, I don't understand how can I get and save host settings without using deprecated code. 

            Any ideas?

             

            EDIT:

            It does work for example in this DNN's code:  \Dnn.Platform\Dnn.AdminExperience\Dnn.PersonaBar.Extensions\Components\TaskScheduler\TaskSchedulerController.cs, because \Dnn.Platform\DNN Platform\Library\Properties\AssemblyInfo.cs says "[assembly: InternalsVisibleTo("Dnn.PersonaBar.Library")] "

            I coudn't find any way to get & save host settings from non-dnn core code. 

             

            Thanks

             

             

             

             

             

             

             






            Advanced Member





              It depends on where in your code you're referencing it. From a Web API controller, an MVC controller, or a scheduled task, you just need to add IHostSettingsService as a constructor argument (you may need to add the constructor altogether). From a control inheriting from PortalModuleBase, you'll need to use the DependencyProvider property. Otherwise, at the moment, those services are not available outside of internal code, and you'll need to stay with the obsolete code for now. The current plan is to adjust a bunch of underlying code in DNN 10 to use the dependency injection container, but there will be slight differences around object lifetime that we can't make until a major version change. Hopefully at that time everything that DNN creates will be available to use constructor injection to access the services in the DI container.
              DNN partner specializing in custom, enterprise DNN development https://engagesoftware.com/showcase





              New Around Here





                I was easier than I thought, and without using IHostSettingsService.

                At least for now, I don't get any obsolete warning when using code like this:

                Snippet <!--StartFragment-->

                var ctl = new HostController();
                var myValue = ctl.GetString("myKey");
                          
                ctl.Update(key: "mykey",
                            value: myValue,
                            clearCache: true);

                <!--EndFragment-->

                Thanks,

                H

                You are not authorized to post a reply.

                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:

                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