DNN Forums

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

How to replace HostController by IHostSettingsService?

 6 Replies
 2 Subscribed to this topic
 26 Subscribed to this forum
Sort:
Author
Messages
Veteran Member
Posts: 1182
Veteran Member
MVP
MVP
You're an MVP!

Hi all,

in a custom module I get a warning saying

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

The line that throws this warning is

<code>DotNetNuke.Entities.Controllers.HostController.Instance.GetSettingsDictionary().TryGetValue("FileExtensions", out allowedExtensions)</code>

Can anyone provide an example of how to replace this? (beside the fact that the warning is misleading, as it should be <code>DotNetNuke.Abstractions.<span style="background-color:Yellow;">Application.</span>IHostSettingsService</code>)

Happy DNNing!
Michael

Michael Tobisch
DNN★MVP

DNN Connect
Growing Member
Posts: 51
Growing Member
Hi Michael,

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

Hope this helps.
Advanced Member
Posts: 159
Advanced Member
MVP
MVP
You're an MVP!
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.
Veteran Member
Posts: 1182
Veteran Member
MVP
MVP
You're an MVP!

Thank you both,

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

<code>public MyControl()<br /> {<br />   _hostSettings = DependencyProvider.GetRequiredService< IHostSettingsService >();<br /> }<br /> <br /> private readonly IHostSettingsService _hostSettings;<br /> <br /> protected void SomeMethod()<br /> {<br /> …<br />   string allowedExtensions;<br />   if (!(_hostSettings.GetSettingsDictionary().TryGetValue("FileExtensions", out allowedExtensions)))<br />   {<br />    … </code>

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

Happy DNNing!
Michael

Michael Tobisch
DNN★MVP

DNN Connect
New Around Here
Posts: 10
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
Posts: 159
Advanced Member
MVP
MVP
You're an MVP!
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.
New Around Here
Posts: 10
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

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:

  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