Skip to content

Commit e8b2739

Browse files
authored
Merge pull request #6964 from donker/cdf-fix-x
Fix to resolution of ClientResourceController in ServicesFramework
2 parents 05d8fb8 + 9c56b80 commit e8b2739

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

DNN Platform/DotNetNuke.Web.Client.ResourceManager/ClientResourceController.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ namespace DotNetNuke.Web.Client.ResourceManager
1111

1212
using DotNetNuke.Abstractions.Application;
1313
using DotNetNuke.Abstractions.ClientResources;
14+
using DotNetNuke.Instrumentation;
1415

1516
/// <inheritdoc />
1617
public class ClientResourceController : IClientResourceController
1718
{
19+
private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(ClientResourceController));
1820
private readonly IHostSettings hostSettings;
1921
private readonly IApplicationStatusInfo appStatus;
22+
private readonly Guid controllerId;
23+
private bool hasBegunRendering;
2024

2125
/// <summary>Initializes a new instance of the <see cref="ClientResourceController"/> class.</summary>
2226
/// <param name="hostSettings">The host settings.</param>
@@ -34,6 +38,8 @@ public ClientResourceController(IHostSettings hostSettings, IApplicationStatusIn
3438
this.hostSettings = hostSettings;
3539
this.appStatus = appStatus;
3640
this.RegisterPathNameAlias("SharedScripts", "~/Resources/Shared/Scripts/");
41+
this.controllerId = Guid.NewGuid();
42+
Logger.Debug($"ClientResourceController initialized with ID {this.controllerId}");
3743
}
3844

3945
private List<IFontResource> Fonts { get; set; } = [];
@@ -198,6 +204,8 @@ public void RemoveStylesheetByPath(string stylesheetPath, string pathNameAlias)
198204
/// <inheritdoc />
199205
public string RenderDependencies(ResourceType resourceType, string provider, string applicationPath)
200206
{
207+
this.hasBegunRendering = true;
208+
Logger.Debug($"Rendering dependencies for CRC id {this.controllerId} with ResourceType={resourceType}, Provider={provider}, ApplicationPath={applicationPath}. We have {this.Scripts.Count} scripts, {this.Stylesheets.Count} stylesheets and {this.Fonts.Count} fonts.");
201209
var sortedList = new List<IResource>();
202210
if (resourceType is ResourceType.Font or ResourceType.All)
203211
{
@@ -264,6 +272,15 @@ private List<T> AddResource<T>(List<T> resources, T resource)
264272
where T : IResource
265273
{
266274
resource.ResolvedPath = this.ResolvePath(resource.FilePath, resource.PathNameAlias);
275+
Logger.Debug($"Adding resource {resource.ResolvedPath} to CRC id {this.controllerId} which currently has {resources.Count} resources");
276+
277+
if (this.hasBegunRendering)
278+
{
279+
Logger.Error($"Cannot add resource {resource.ResolvedPath} to CRC id {this.controllerId} because rendering has already begun");
280+
281+
////throw new InvalidOperationException("Cannot add resources after rendering has begun.");
282+
}
283+
267284
resources.RemoveAll(l => string.Equals(l.ResolvedPath, resource.ResolvedPath, StringComparison.OrdinalIgnoreCase)); // remove any existing link with the same key (i.e. exactly the same resolved path)
268285
if (!string.IsNullOrEmpty(resource.Name))
269286
{

DNN Platform/Library/Framework/ServicesFrameworkImpl.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ internal class ServicesFrameworkImpl : IServicesFramework, IServiceFrameworkInte
2727
{
2828
private const string AntiForgeryKey = "dnnAntiForgeryRequested";
2929
private const string ScriptKey = "dnnSFAjaxScriptRequested";
30-
private readonly IClientResourceController clientResourceController;
3130
private readonly IApplicationStatusInfo appStatus;
3231
private readonly IEventLogger eventLogger;
3332

@@ -44,9 +43,9 @@ public ServicesFrameworkImpl()
4443
/// <param name="eventLogger">The event logger.</param>
4544
public ServicesFrameworkImpl(IClientResourceController clientResourceController, IApplicationStatusInfo appStatus, IEventLogger eventLogger)
4645
{
47-
this.clientResourceController = clientResourceController ?? Globals.GetCurrentServiceProvider().GetRequiredService<IClientResourceController>();
48-
this.appStatus = appStatus ?? Globals.GetCurrentServiceProvider().GetRequiredService<IApplicationStatusInfo>();
49-
this.eventLogger = eventLogger ?? Globals.GetCurrentServiceProvider().GetRequiredService<IEventLogger>();
46+
var servicesProvider = Globals.GetCurrentServiceProvider();
47+
this.appStatus = appStatus ?? servicesProvider.GetRequiredService<IApplicationStatusInfo>();
48+
this.eventLogger = eventLogger ?? servicesProvider.GetRequiredService<IEventLogger>();
5049
}
5150

5251
/// <inheritdoc />
@@ -105,7 +104,7 @@ public void RegisterAjaxScript(Page page)
105104
scriptPath = "~/js/dnn.servicesframework.js";
106105
}
107106

108-
this.clientResourceController.RegisterScript(scriptPath);
107+
GetClientResourcesController().RegisterScript(scriptPath);
109108
}
110109

111110
private static void SetKey(string key)
@@ -117,5 +116,11 @@ private static bool CheckKey(string antiForgeryKey)
117116
{
118117
return HttpContextSource.Current.Items.Contains(antiForgeryKey);
119118
}
119+
120+
private static IClientResourceController GetClientResourcesController()
121+
{
122+
var serviceProvider = Globals.GetCurrentServiceProvider();
123+
return serviceProvider.GetRequiredService<IClientResourceController>();
124+
}
120125
}
121126
}

0 commit comments

Comments
 (0)