From a600783b5008c9f91bbf81e368bef77c7bf6de83 Mon Sep 17 00:00:00 2001 From: David Cantu Date: Thu, 22 Jul 2021 15:31:36 -0700 Subject: [PATCH 1/2] Make Extensions.FileProviders supported in browser and only unsupport FSW usage --- .../Directory.Build.props | 4 +- ...t.Extensions.FileProviders.Physical.csproj | 9 +--- .../src/PhysicalFileProvider.cs | 8 ++- .../src/PhysicalFilesWatcher.cs | 10 ++++ .../src/Resources/Strings.resx | 54 +++++++++---------- .../src/HostBuilder.cs | 2 - 6 files changed, 48 insertions(+), 39 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.FileProviders.Physical/Directory.Build.props b/src/libraries/Microsoft.Extensions.FileProviders.Physical/Directory.Build.props index 8ec207de7d8b3f..1ade7c7070766b 100644 --- a/src/libraries/Microsoft.Extensions.FileProviders.Physical/Directory.Build.props +++ b/src/libraries/Microsoft.Extensions.FileProviders.Physical/Directory.Build.props @@ -2,6 +2,6 @@ true - browser + true - \ No newline at end of file + diff --git a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Microsoft.Extensions.FileProviders.Physical.csproj b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Microsoft.Extensions.FileProviders.Physical.csproj index 534a08b11ff87a..242f042fb3262a 100644 --- a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Microsoft.Extensions.FileProviders.Physical.csproj +++ b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Microsoft.Extensions.FileProviders.Physical.csproj @@ -1,17 +1,12 @@ - + Microsoft.Extensions.FileProviders - $(NetCoreAppCurrent);$(NetCoreAppCurrent)-Browser;netstandard2.0;net461 + $(NetCoreAppCurrent);netstandard2.0;net461 true true File provider for physical files for Microsoft.Extensions.FileProviders. - - - false - SR.FileProvidersPhysical_PlatformNotSupported - filter = filter.Replace('\\', '/'); diff --git a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Resources/Strings.resx b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Resources/Strings.resx index 2ff2106bca01fe..1eb0026a4f2a76 100644 --- a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Resources/Strings.resx +++ b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Resources/Strings.resx @@ -1,17 +1,17 @@  @@ -129,7 +129,7 @@ Unexpected type of FileSystemInfo - - Microsoft.Extensions.FileProviders.Physical is not supported on this platform. + + The type {0} is not supported on this platform, use polling instead. \ No newline at end of file diff --git a/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs b/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs index 927a287da77e17..3827761382a135 100644 --- a/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs +++ b/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs @@ -192,9 +192,7 @@ private void CreateHostingEnvironment() _hostingEnvironment.ApplicationName = Assembly.GetEntryAssembly()?.GetName().Name; } -#pragma warning disable CA1416 // 'PhysicalFileProvider' is unsupported on: 'browser' https://github.com/dotnet/runtime/issues/56178 _hostingEnvironment.ContentRootFileProvider = _defaultProvider = new PhysicalFileProvider(_hostingEnvironment.ContentRootPath); -#pragma warning restore CA1416 // 'PhysicalFileProvider' is unsupported on: 'browser' } private string ResolveContentRootPath(string contentRootPath, string basePath) From fd54a5156f6ea0cf6bf89601aa0aef3d1a2abd3e Mon Sep 17 00:00:00 2001 From: David Cantu Date: Thu, 22 Jul 2021 16:26:23 -0700 Subject: [PATCH 2/2] Disable CA1416 only for TryEnableFileSystemWatcher --- .../src/PhysicalFilesWatcher.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFilesWatcher.cs b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFilesWatcher.cs index b72aa885fe809e..81df7c51532bf3 100644 --- a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFilesWatcher.cs +++ b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFilesWatcher.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Concurrent; using System.IO; +using System.Runtime.Versioning; using System.Security; using System.Threading; using System.Threading.Tasks; @@ -147,7 +148,10 @@ public IChangeToken CreateFileChangeToken(string filter) } IChangeToken changeToken = GetOrAddChangeToken(filter); +// We made sure that browser never uses FileSystemWatcher. +#pragma warning disable CA1416 // Validate platform compatibility TryEnableFileSystemWatcher(); +#pragma warning restore CA1416 // Validate platform compatibility return changeToken; } @@ -271,8 +275,7 @@ protected virtual void Dispose(bool disposing) } } -// We made sure that browser never uses FileSystemWatcher. -#pragma warning disable CA1416 // Validate platform compatibility + [UnsupportedOSPlatform("browser")] private void OnRenamed(object sender, RenamedEventArgs e) { // For a file name change or a directory's name change notify registered tokens. @@ -305,11 +308,13 @@ ex is DirectoryNotFoundException || } } + [UnsupportedOSPlatform("browser")] private void OnChanged(object sender, FileSystemEventArgs e) { OnFileSystemEntryChange(e.FullPath); } + [UnsupportedOSPlatform("browser")] private void OnError(object sender, ErrorEventArgs e) { // Notify all cache entries on error. @@ -319,6 +324,7 @@ private void OnError(object sender, ErrorEventArgs e) } } + [UnsupportedOSPlatform("browser")] private void OnFileSystemEntryChange(string fullPath) { try @@ -341,6 +347,7 @@ ex is SecurityException || } } + [UnsupportedOSPlatform("browser")] private void ReportChangeForMatchedEntries(string path) { if (string.IsNullOrEmpty(path)) @@ -377,6 +384,7 @@ private void ReportChangeForMatchedEntries(string path) } } + [UnsupportedOSPlatform("browser")] private void TryDisableFileSystemWatcher() { if (_fileWatcher != null) @@ -394,6 +402,7 @@ private void TryDisableFileSystemWatcher() } } + [UnsupportedOSPlatform("browser")] private void TryEnableFileSystemWatcher() { if (_fileWatcher != null) @@ -409,7 +418,6 @@ private void TryEnableFileSystemWatcher() } } } -#pragma warning restore CA1416 // Validate platform compatibility private static string NormalizePath(string filter) => filter = filter.Replace('\\', '/');