From 9414d571355001d796e11c74588389dd7387d836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Tue, 15 Jun 2021 11:46:34 +0200 Subject: [PATCH] Add NetCoreAppCurrent configuration to three ref projects (#54146) System.ComponentModel.Composition, System.Diagnostics.PerformanceCounter and System.Runtime.Caching. Helps with https://github.com/dotnet/runtime/issues/54012 --- .../System.ComponentModel.Composition.csproj | 5 ++++- ...System.Diagnostics.PerformanceCounter.csproj | 7 ++++++- ...System.Diagnostics.PerformanceCounter.csproj | 2 +- .../ref/System.Runtime.Caching.csproj | 7 ++++++- .../src/System.Runtime.Caching.csproj | 2 +- .../Configuration/CachingSectionGroup.cs | 4 ++++ .../Caching/Configuration/MemoryCacheElement.cs | 4 ++++ .../Caching/Configuration/MemoryCacheSection.cs | 4 ++++ .../MemoryCacheSettingsCollection.cs | 4 ++++ .../src/System/Runtime/Caching/Counters.cs | 4 ++++ .../Caching/FileChangeNotificationSystem.cs | 4 ++++ .../Runtime/Caching/HostFileChangeMonitor.cs | 7 +++++++ .../src/System/Runtime/Caching/MemoryCache.cs | 17 +++++++++++++++-- .../Runtime/Caching/MemoryCacheStatistics.cs | 11 +++++++++-- .../System/Runtime/Caching/MemoryCacheStore.cs | 17 ++++++++++++----- 15 files changed, 85 insertions(+), 14 deletions(-) diff --git a/src/libraries/System.ComponentModel.Composition/ref/System.ComponentModel.Composition.csproj b/src/libraries/System.ComponentModel.Composition/ref/System.ComponentModel.Composition.csproj index c0fcd7902fa..5b6fcbd2306 100644 --- a/src/libraries/System.ComponentModel.Composition/ref/System.ComponentModel.Composition.csproj +++ b/src/libraries/System.ComponentModel.Composition/ref/System.ComponentModel.Composition.csproj @@ -1,12 +1,15 @@ - netcoreapp3.1;netstandard2.0 + $(NetCoreAppCurrent);netcoreapp3.1;netstandard2.0 enable + + + diff --git a/src/libraries/System.Diagnostics.PerformanceCounter/ref/System.Diagnostics.PerformanceCounter.csproj b/src/libraries/System.Diagnostics.PerformanceCounter/ref/System.Diagnostics.PerformanceCounter.csproj index 8e37074e8cd..b01b83e7327 100644 --- a/src/libraries/System.Diagnostics.PerformanceCounter/ref/System.Diagnostics.PerformanceCounter.csproj +++ b/src/libraries/System.Diagnostics.PerformanceCounter/ref/System.Diagnostics.PerformanceCounter.csproj @@ -1,12 +1,17 @@ - netcoreapp3.1;netstandard2.0;net461 + $(NetCoreAppCurrent);netcoreapp3.1;netstandard2.0;net461 true + + + + + diff --git a/src/libraries/System.Diagnostics.PerformanceCounter/src/System.Diagnostics.PerformanceCounter.csproj b/src/libraries/System.Diagnostics.PerformanceCounter/src/System.Diagnostics.PerformanceCounter.csproj index 3265bc1713e..598ae8322b0 100644 --- a/src/libraries/System.Diagnostics.PerformanceCounter/src/System.Diagnostics.PerformanceCounter.csproj +++ b/src/libraries/System.Diagnostics.PerformanceCounter/src/System.Diagnostics.PerformanceCounter.csproj @@ -1,7 +1,7 @@ true - $(NetCoreAppCurrent)-windows;netcoreapp3.1-windows;netcoreapp3.1;netstandard2.0;net461 + $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);netcoreapp3.1-windows;netcoreapp3.1;netstandard2.0;net461 diff --git a/src/libraries/System.Runtime.Caching/ref/System.Runtime.Caching.csproj b/src/libraries/System.Runtime.Caching/ref/System.Runtime.Caching.csproj index 32f5458551f..2083b821737 100644 --- a/src/libraries/System.Runtime.Caching/ref/System.Runtime.Caching.csproj +++ b/src/libraries/System.Runtime.Caching/ref/System.Runtime.Caching.csproj @@ -1,10 +1,15 @@ - netcoreapp3.1;netstandard2.0 + $(NetCoreAppCurrent);netcoreapp3.1;netstandard2.0 + + + + + diff --git a/src/libraries/System.Runtime.Caching/src/System.Runtime.Caching.csproj b/src/libraries/System.Runtime.Caching/src/System.Runtime.Caching.csproj index 49cecf419b6..f9977c0d0f5 100644 --- a/src/libraries/System.Runtime.Caching/src/System.Runtime.Caching.csproj +++ b/src/libraries/System.Runtime.Caching/src/System.Runtime.Caching.csproj @@ -1,7 +1,7 @@ true - $(NetCoreAppCurrent)-windows;netcoreapp3.1-windows;netcoreapp3.1;netstandard2.0-windows;netstandard2.0 + $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);netcoreapp3.1-windows;netcoreapp3.1;netstandard2.0-windows;netstandard2.0 true diff --git a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/CachingSectionGroup.cs b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/CachingSectionGroup.cs index 3452f76c6ee..02f8559b06f 100644 --- a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/CachingSectionGroup.cs +++ b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/CachingSectionGroup.cs @@ -3,9 +3,13 @@ using System; using System.Configuration; +using System.Runtime.Versioning; namespace System.Runtime.Caching.Configuration { +#if NET5_0_OR_GREATER + [UnsupportedOSPlatform("browser")] +#endif internal sealed class CachingSectionGroup : ConfigurationSectionGroup { public CachingSectionGroup() diff --git a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/MemoryCacheElement.cs b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/MemoryCacheElement.cs index 26d0880d0ea..cf798f20416 100644 --- a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/MemoryCacheElement.cs +++ b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/MemoryCacheElement.cs @@ -4,9 +4,13 @@ using System; using System.ComponentModel; using System.Configuration; +using System.Runtime.Versioning; namespace System.Runtime.Caching.Configuration { +#if NET5_0_OR_GREATER + [UnsupportedOSPlatform("browser")] +#endif internal sealed class MemoryCacheElement : ConfigurationElement { private static readonly ConfigurationProperty s_propName = diff --git a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/MemoryCacheSection.cs b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/MemoryCacheSection.cs index 563f3ccfa4e..df535cd6bc4 100644 --- a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/MemoryCacheSection.cs +++ b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/MemoryCacheSection.cs @@ -5,6 +5,7 @@ using System.Collections.Specialized; using System.Configuration; using System.Runtime.Caching.Resources; +using System.Runtime.Versioning; namespace System.Runtime.Caching.Configuration { @@ -20,6 +21,9 @@ namespace System.Runtime.Caching.Configuration */ +#if NET5_0_OR_GREATER + [UnsupportedOSPlatform("browser")] +#endif internal sealed class MemoryCacheSection : ConfigurationSection { private static readonly ConfigurationProperty s_propNamedCaches = new ConfigurationProperty("namedCaches", diff --git a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/MemoryCacheSettingsCollection.cs b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/MemoryCacheSettingsCollection.cs index 7a34adf7b07..8c08a0bfcda 100644 --- a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/MemoryCacheSettingsCollection.cs +++ b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Configuration/MemoryCacheSettingsCollection.cs @@ -3,9 +3,13 @@ using System; using System.Configuration; +using System.Runtime.Versioning; namespace System.Runtime.Caching.Configuration { +#if NET5_0_OR_GREATER + [UnsupportedOSPlatform("browser")] +#endif [ConfigurationCollection(typeof(MemoryCacheElement), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)] internal sealed class MemoryCacheSettingsCollection : ConfigurationElementCollection diff --git a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Counters.cs b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Counters.cs index a6845ec85cb..5e8a3712054 100644 --- a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Counters.cs +++ b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Counters.cs @@ -5,9 +5,13 @@ using System.Collections.Generic; using System.Diagnostics.Tracing; using System.Threading; +using System.Runtime.Versioning; namespace System.Runtime.Caching { +#if NET5_0_OR_GREATER + [UnsupportedOSPlatform("browser")] +#endif internal sealed class Counters : EventSource { #if NETCOREAPP3_1_OR_GREATER diff --git a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/FileChangeNotificationSystem.cs b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/FileChangeNotificationSystem.cs index 339169a4c45..497017c14e7 100644 --- a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/FileChangeNotificationSystem.cs +++ b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/FileChangeNotificationSystem.cs @@ -7,9 +7,13 @@ using System.Collections; using System.IO; using System.Security; +using System.Runtime.Versioning; namespace System.Runtime.Caching { +#if NET5_0_OR_GREATER + [UnsupportedOSPlatform("browser")] +#endif internal sealed class FileChangeNotificationSystem : IFileChangeNotificationSystem { private readonly Hashtable _dirMonitors; diff --git a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/HostFileChangeMonitor.cs b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/HostFileChangeMonitor.cs index c354604ea89..c80daaac7a8 100644 --- a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/HostFileChangeMonitor.cs +++ b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/HostFileChangeMonitor.cs @@ -97,6 +97,13 @@ private static void InitFCN() } if (fcn == null) { +#if NET5_0_OR_GREATER + if (OperatingSystem.IsBrowser()) + { + throw new PlatformNotSupportedException(); + } +#endif + fcn = new FileChangeNotificationSystem(); } Interlocked.CompareExchange(ref s_fcn, fcn, null); diff --git a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCache.cs b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCache.cs index 31d3db6e697..72d92119acd 100644 --- a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCache.cs +++ b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCache.cs @@ -13,6 +13,7 @@ using System.Security; using System.Threading; using System.Diagnostics; +using System.Runtime.Versioning; namespace System.Runtime.Caching { @@ -38,6 +39,12 @@ public class MemoryCache : ObjectCache, IEnumerable, IDisposable private bool _useMemoryCacheManager = true; private EventHandler _onAppDomainUnload; private UnhandledExceptionEventHandler _onUnhandledException; +#if NET5_0_OR_GREATER + [UnsupportedOSPlatformGuard("browser")] + private static bool _countersSupported => !OperatingSystem.IsBrowser(); +#else + private static bool _countersSupported => true; +#endif private bool IsDisposed { get { return (_disposed == 1); } } internal bool ConfigLess { get { return _configLess; } } @@ -196,7 +203,10 @@ private void InitDisposableMembers(NameValueCollection config) { try { - _perfCounters = new Counters(_name); + if (_countersSupported) + { + _perfCounters = new Counters(_name); + } } catch { @@ -481,7 +491,10 @@ public void Dispose() } if (_perfCounters != null) { - _perfCounters.Dispose(); + if (_countersSupported) + { + _perfCounters.Dispose(); + } } GC.SuppressFinalize(this); } diff --git a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCacheStatistics.cs b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCacheStatistics.cs index b5cb20db392..107c46f0158 100644 --- a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCacheStatistics.cs +++ b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCacheStatistics.cs @@ -8,6 +8,7 @@ using System.Globalization; using System.Runtime.Caching.Configuration; using System.Runtime.InteropServices; +using System.Runtime.Versioning; using System.Security; using System.Threading; @@ -36,6 +37,12 @@ internal sealed class MemoryCacheStatistics : IDisposable private CacheMemoryMonitor _cacheMemoryMonitor; private readonly MemoryCache _memoryCache; private readonly PhysicalMemoryMonitor _physicalMemoryMonitor; +#if NET5_0_OR_GREATER + [UnsupportedOSPlatformGuard("browser")] + private static bool _configSupported => !OperatingSystem.IsBrowser(); +#else + private static bool _configSupported => true; +#endif // private @@ -117,7 +124,7 @@ private int GetPercentToTrim() private void InitializeConfiguration(NameValueCollection config) { MemoryCacheElement element = null; - if (!_memoryCache.ConfigLess) + if (!_memoryCache.ConfigLess && _configSupported) { MemoryCacheSection section = ConfigurationManager.GetSection("system.runtime.caching/memoryCache") as MemoryCacheSection; if (section != null) @@ -126,7 +133,7 @@ private void InitializeConfiguration(NameValueCollection config) } } - if (element != null) + if (element != null && _configSupported) { _configCacheMemoryLimitMegabytes = element.CacheMemoryLimitMegabytes; _configPhysicalMemoryLimitPercentage = element.PhysicalMemoryLimitPercentage; diff --git a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCacheStore.cs b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCacheStore.cs index 47e5de99262..047bc8fd80e 100644 --- a/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCacheStore.cs +++ b/src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCacheStore.cs @@ -8,6 +8,7 @@ using System.Diagnostics; using System.Security; using System.Diagnostics.CodeAnalysis; +using System.Runtime.Versioning; namespace System.Runtime.Caching { @@ -25,6 +26,12 @@ internal sealed class MemoryCacheStore : IDisposable private volatile bool _useInsertBlock; private readonly MemoryCache _cache; private readonly Counters _perfCounters; +#if NET5_0_OR_GREATER + [UnsupportedOSPlatformGuard("browser")] + private static bool _countersSupported => !OperatingSystem.IsBrowser(); +#else + private static bool _countersSupported => true; +#endif internal MemoryCacheStore(MemoryCache cache, Counters perfCounters) { @@ -73,7 +80,7 @@ private void AddToCache(MemoryCacheEntry entry) } entry.CallNotifyOnChanged(); - if (_perfCounters != null) + if (_perfCounters != null && _countersSupported) { _perfCounters.Increment(CounterName.Entries); _perfCounters.Increment(CounterName.Turnover); @@ -108,7 +115,7 @@ private void RemoveFromCache(MemoryCacheEntry entry, CacheEntryRemovedReason rea { entry.Release(_cache, reason); } - if (_perfCounters != null) + if (_perfCounters != null && _countersSupported) { _perfCounters.Decrement(CounterName.Entries); _perfCounters.Increment(CounterName.Turnover); @@ -136,7 +143,7 @@ internal void UpdateExpAndUsage(MemoryCacheEntry entry, bool updatePerfCounters // keep the sentinel from expiring, which in turn would force a removal of this entry from the cache. entry.UpdateSlidingExpForUpdateSentinel(); - if (updatePerfCounters && _perfCounters != null) + if (updatePerfCounters && _perfCounters != null && _countersSupported) { _perfCounters.Increment(CounterName.Hits); _perfCounters.Increment(CounterName.HitRatio); @@ -145,7 +152,7 @@ internal void UpdateExpAndUsage(MemoryCacheEntry entry, bool updatePerfCounters } else { - if (updatePerfCounters && _perfCounters != null) + if (updatePerfCounters && _perfCounters != null && _countersSupported) { _perfCounters.Increment(CounterName.Misses); _perfCounters.Increment(CounterName.HitRatioBase); @@ -404,7 +411,7 @@ internal long TrimInternal(int percent) trimmedOrExpired += trimmed; } - if (trimmed > 0 && _perfCounters != null) + if (trimmed > 0 && _perfCounters != null && _countersSupported) { // Update values for perfcounters _perfCounters.IncrementBy(CounterName.Trims, trimmed); -- GitLab