Skip to content

Commit 6902785

Browse files
author
Mark Hall
committed
Fix settings Dictionary
Raise event for remote servers to update its dictionary. Fixes: 895
1 parent 16bed8c commit 6902785

File tree

4 files changed

+98
-17
lines changed

4 files changed

+98
-17
lines changed

src/Foundation/Infrastructure/Cms/Initialize.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using EPiServer.Framework;
1+
using EPiServer.Events.Clients;
2+
using EPiServer.Framework;
23
using EPiServer.Framework.Initialization;
34
using Foundation.Infrastructure.Cms.ModelBinders;
45
using Foundation.Infrastructure.Cms.Settings;
@@ -8,9 +9,12 @@
89

910
namespace Foundation.Infrastructure.Cms
1011
{
11-
[ModuleDependency(typeof(InitializationModule))]//, typeof(SetupBootstrapRenderer))]
12+
[ModuleDependency(typeof(InitializationModule))]
1213
public class Initialize : IConfigurableModule
1314
{
15+
public static readonly Guid SettingsRaiserId = Guid.NewGuid();
16+
public static readonly Guid SettingsEventId = new Guid("c3b20325-5aa8-4430-b33f-2a74c3e5b807");
17+
1418
void IConfigurableModule.ConfigureContainer(ServiceConfigurationContext context)
1519
{
1620
context.Services.AddTransient<IsInEditModeAccessor>(locator => () => locator.GetInstance<IContextModeResolver>().CurrentMode.EditOrPreview());
@@ -23,10 +27,14 @@ void IConfigurableModule.ConfigureContainer(ServiceConfigurationContext context)
2327

2428
void IInitializableModule.Initialize(InitializationEngine context)
2529
{
30+
context.Locate.Advanced.GetInstance<IEventRegistry>().Get(SettingsEventId).Raised += context.Locate.Advanced.GetInstance<ISettingsService>().SettingsEvent_Raised;
31+
context.Locate.Advanced.GetInstance<ISettingsService>().InitializeSettings();
2632
}
2733

2834
void IInitializableModule.Uninitialize(InitializationEngine context)
2935
{
36+
context.Locate.Advanced.GetInstance<IEventRegistry>().Get(SettingsEventId).Raised -= context.Locate.Advanced.GetInstance<ISettingsService>().SettingsEvent_Raised;
37+
context.Locate.Advanced.GetInstance<ISettingsService>().UnintializeSettings();
3038
}
3139
}
3240
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using EPiServer.Events;
2+
using System.Runtime.Serialization;
3+
4+
namespace Foundation.Infrastructure.Cms.Settings
5+
{
6+
[DataContract]
7+
[EventsServiceKnownType]
8+
public class SettingsEvent
9+
{
10+
[DataMember]
11+
public Guid SiteId { get; set; }
12+
13+
[DataMember]
14+
public string ContentReference { get; set; }
15+
16+
[DataMember]
17+
public bool IsPublished { get; set; }
18+
19+
[DataMember]
20+
public bool IsDelete { get; set; }
21+
}
22+
}

src/Foundation/Infrastructure/Cms/Settings/SettingsService.cs

+65-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using EPiServer.DataAccess;
2+
using EPiServer.Events;
3+
using EPiServer.Events.Clients;
24
using EPiServer.Framework.TypeScanner;
35
using EPiServer.Globalization;
46
using EPiServer.Logging;
57
using EPiServer.Security;
68
using System.Collections.Concurrent;
9+
using System.Security.Policy;
710

811
namespace Foundation.Infrastructure.Cms.Settings
912
{
@@ -16,6 +19,7 @@ public interface ISettingsService
1619
void UnintializeSettings();
1720
void UpdateSettings(Guid siteId, IContent content, bool isContentNotPublished);
1821
void UpdateSettings();
22+
void SettingsEvent_Raised(object sender, EventNotificationEventArgs e);
1923
}
2024

2125
public static class ISettingsServiceExtensions
@@ -55,6 +59,7 @@ public class SettingsService : ISettingsService
5559
private readonly ISiteDefinitionResolver _siteDefinitionResolver;
5660
private readonly IHttpContextAccessor _httpContextAccessor;
5761
private readonly IContextModeResolver _contextModeResolver;
62+
private readonly IEventRegistry _eventRegistry;
5863

5964
public SettingsService(
6065
IContentRepository contentRepository,
@@ -67,7 +72,8 @@ public SettingsService(
6772
ISiteDefinitionRepository siteDefinitionRepository,
6873
ISiteDefinitionResolver siteDefinitionResolver,
6974
IHttpContextAccessor httpContextAccessor,
70-
IContextModeResolver contextModeResolver)
75+
IContextModeResolver contextModeResolver,
76+
IEventRegistry eventRegistry)
7177
{
7278
_contentRepository = contentRepository;
7379
_contentVersionRepository = contentVersionRepository;
@@ -80,6 +86,7 @@ public SettingsService(
8086
_siteDefinitionResolver = siteDefinitionResolver;
8187
_httpContextAccessor = httpContextAccessor;
8288
_contextModeResolver = contextModeResolver;
89+
_eventRegistry = eventRegistry;
8390
}
8491

8592
public ConcurrentDictionary<string, Dictionary<Type, object>> SiteSettings { get; } = new ConcurrentDictionary<string, Dictionary<Type, object>>();
@@ -140,6 +147,45 @@ public T GetSiteSettings<T>(Guid? siteId = null)
140147
return default;
141148
}
142149

150+
public void SettingsEvent_Raised(object sender, EventNotificationEventArgs e)
151+
{
152+
if (e.RaiserId == Initialize.SettingsRaiserId)
153+
{
154+
return;
155+
}
156+
157+
var eventParameter = e.Param as SettingsEvent;
158+
if (eventParameter == null)
159+
{
160+
return;
161+
}
162+
163+
if (eventParameter.IsDelete)
164+
{
165+
if (eventParameter.SiteId == Guid.Empty)
166+
{
167+
return;
168+
}
169+
170+
foreach(var key in SiteSettings.Keys.Where(x => x.Contains(eventParameter.SiteId.ToString(), StringComparison.OrdinalIgnoreCase)))
171+
{
172+
SiteSettings.TryRemove(key, out var value);
173+
}
174+
}
175+
176+
if (!ContentReference.TryParse(eventParameter.ContentReference, out var reference))
177+
{
178+
return;
179+
}
180+
181+
if (!_contentRepository.TryGet<IContent>(reference, out var content))
182+
{
183+
return;
184+
}
185+
186+
UpdateSettings(eventParameter.SiteId, content, !eventParameter.IsPublished);
187+
}
188+
143189
public void UpdateSettings(Guid siteId, IContent content, bool isContentNotPublished)
144190
{
145191
var contentType = content.GetOriginalType();
@@ -192,6 +238,12 @@ public void UpdateSettings(Guid siteId, IContent content, bool isContentNotPubli
192238
SiteSettings[siteId.ToString() + $"-{contentLanguage}"][contentType] = content;
193239
SiteSettings[$"{siteId}-common-draft-{contentLanguage}"][contentType] = content;
194240
}
241+
_eventRegistry.Get(Initialize.SettingsEventId).Raise(Initialize.SettingsRaiserId, new SettingsEvent
242+
{
243+
ContentReference = content.ContentLink.ToString(),
244+
SiteId = siteId,
245+
IsPublished = !isContentNotPublished
246+
});
195247
}
196248
catch (KeyNotFoundException keyNotFoundException)
197249
{
@@ -241,6 +293,7 @@ public void UpdateSettings()
241293
return;
242294
}
243295

296+
SiteSettings.Clear();
244297
GlobalSettingsRoot = root.ContentLink;
245298
var children = _contentRepository.GetChildren<SettingsFolder>(GlobalSettingsRoot).ToList();
246299
foreach (var site in _siteDefinitionRepository.List())
@@ -326,6 +379,17 @@ private void SiteDeleted(object sender, SiteDefinitionEventArgs e)
326379
}
327380

328381
_contentRepository.Delete(folder.ContentLink, true, AccessLevel.NoAccess);
382+
foreach (var key in SiteSettings.Keys.Where(x => x.Contains(e.Site.Id.ToString(), StringComparison.OrdinalIgnoreCase)))
383+
{
384+
SiteSettings.TryRemove(key, out var value);
385+
}
386+
_eventRegistry.Get(Initialize.SettingsEventId).Raise(Initialize.SettingsRaiserId, new SettingsEvent
387+
{
388+
ContentReference = null,
389+
SiteId = e.Site.Id,
390+
IsPublished = false,
391+
IsDelete = true
392+
});
329393
}
330394

331395
private void SiteUpdated(object sender, SiteDefinitionEventArgs e)

src/Foundation/Infrastructure/ContentInstaller.cs

+1-14
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@ namespace Foundation.Infrastructure
1717
{
1818
public class ContentInstaller : IBlockingFirstRequestInitializer
1919
{
20-
private readonly UIUserProvider _uIUserProvider;
21-
private readonly UIRoleProvider _uIRoleProvider;
22-
private readonly UISignInManager _uISignInManager;
2320
private readonly ISiteDefinitionRepository _siteDefinitionRepository;
2421
private readonly ContentRootService _contentRootService;
2522
private readonly IContentRepository _contentRepository;
2623
private readonly IDataImporter _dataImporter;
2724
private readonly IScheduledJobExecutor _scheduledJobExecutor;
2825
private readonly IScheduledJobRepository _scheduledJobRepository;
29-
private readonly ISettingsService _settingsService;
3026
private readonly ILanguageBranchRepository _languageBranchRepository;
3127
private readonly IWebHostEnvironment _webHostEnvironment;
3228
private readonly EventedIndexingSettings _eventedIndexingSettings;
@@ -35,16 +31,12 @@ public class ContentInstaller : IBlockingFirstRequestInitializer
3531
private readonly IndexBuilder _indexBuilder;
3632
private readonly IPrincipalAccessor _principalAccessor;
3733

38-
public ContentInstaller(UIUserProvider uIUserProvider,
39-
UISignInManager uISignInManager,
40-
UIRoleProvider uIRoleProvider,
41-
ISiteDefinitionRepository siteDefinitionRepository,
34+
public ContentInstaller(ISiteDefinitionRepository siteDefinitionRepository,
4235
ContentRootService contentRootService,
4336
IContentRepository contentRepository,
4437
IDataImporter dataImporter,
4538
IScheduledJobExecutor scheduledJobExecutor,
4639
IScheduledJobRepository scheduledJobRepository,
47-
ISettingsService settingsService,
4840
ILanguageBranchRepository languageBranchRepository,
4941
IWebHostEnvironment webHostEnvironment,
5042
EventedIndexingSettings eventedIndexingSettings,
@@ -53,16 +45,12 @@ public ContentInstaller(UIUserProvider uIUserProvider,
5345
IndexBuilder indexBuilder,
5446
IPrincipalAccessor principalAccessor)
5547
{
56-
_uIUserProvider = uIUserProvider;
57-
_uISignInManager = uISignInManager;
58-
_uIRoleProvider = uIRoleProvider;
5948
_siteDefinitionRepository = siteDefinitionRepository;
6049
_contentRootService = contentRootService;
6150
_contentRepository = contentRepository;
6251
_dataImporter = dataImporter;
6352
_scheduledJobExecutor = scheduledJobExecutor;
6453
_scheduledJobRepository = scheduledJobRepository;
65-
_settingsService = settingsService;
6654
_languageBranchRepository = languageBranchRepository;
6755
_webHostEnvironment = webHostEnvironment;
6856
_eventedIndexingSettings = eventedIndexingSettings;
@@ -77,7 +65,6 @@ public ContentInstaller(UIUserProvider uIUserProvider,
7765
public async Task InitializeAsync(HttpContext httpContext)
7866
{
7967
InstallDefaultContent(httpContext);
80-
_settingsService.InitializeSettings();
8168
await Task.CompletedTask;
8269
}
8370

0 commit comments

Comments
 (0)