https://github.com/umbraco/Umbraco-CMS/issues/10699 Changed HasSet<Uri> to ConcurrentHashSet<Uri>

and changed Add method to TryAdd in AspNetCoreHostingEnviroment.EnsureApplicationMainUrl()
to fix concurrency errors
This commit is contained in:
Zeegaan
2021-08-03 14:04:19 +02:00
parent c1dcdfa634
commit 338e1e0d6d
2 changed files with 19 additions and 4 deletions

View File

@@ -2,6 +2,7 @@
// See LICENSE for more details.
using System;
using System.Threading.Tasks;
using NUnit.Framework;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Tests.UnitTests.AutoFixture;
@@ -36,5 +37,15 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Website
Assert.AreEqual("~/Views/Template.cshtml", PathUtility.EnsurePathIsApplicationRootPrefixed("/Views/Template.cshtml"));
Assert.AreEqual("~/Views/Template.cshtml", PathUtility.EnsurePathIsApplicationRootPrefixed("~/Views/Template.cshtml"));
}
[AutoMoqData]
[Test]
public void EnsureApplicationMainUrl(AspNetCoreHostingEnvironment sut)
{
var url = new Uri("http://localhost:5000");
sut.EnsureApplicationMainUrl(url);
Assert.AreEqual(sut.ApplicationMainUrl, url);
}
}
}

View File

@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Collections;
using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Extensions;
@@ -13,7 +15,7 @@ namespace Umbraco.Cms.Web.Common.AspNetCore
{
public class AspNetCoreHostingEnvironment : IHostingEnvironment
{
private readonly ISet<Uri> _applicationUrls = new HashSet<Uri>();
private readonly ConcurrentHashSet<Uri> _applicationUrls = new ConcurrentHashSet<Uri>();
private readonly IServiceProvider _serviceProvider;
private readonly IOptionsMonitor<HostingSettings> _hostingSettings;
private readonly IOptionsMonitor<WebRoutingSettings> _webRoutingSettings;
@@ -168,6 +170,7 @@ namespace Umbraco.Cms.Web.Common.AspNetCore
// (this is a simplified version of what was in 7.x)
// note: should this be optional? is it expensive?
if (currentApplicationUrl is null)
{
return;
@@ -181,9 +184,10 @@ namespace Umbraco.Cms.Web.Common.AspNetCore
var change = !_applicationUrls.Contains(currentApplicationUrl);
if (change)
{
_applicationUrls.Add(currentApplicationUrl);
ApplicationMainUrl = currentApplicationUrl;
if (_applicationUrls.TryAdd(currentApplicationUrl))
{
ApplicationMainUrl = currentApplicationUrl;
}
}
}
}