Skip to content

Commit aa0cf93

Browse files
authored
Make Extensions.FileProviders supported in browser and only unsupport FSW usage (#56189)
* Make Extensions.FileProviders supported in browser and only unsupport FSW usage * Disable CA1416 only for TryEnableFileSystemWatcher
1 parent d20fdf1 commit aa0cf93

File tree

6 files changed

+56
-39
lines changed

6 files changed

+56
-39
lines changed

src/libraries/Microsoft.Extensions.FileProviders.Physical/Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
<Import Project="..\Directory.Build.props" />
33
<PropertyGroup>
44
<IsAspNetCoreApp>true</IsAspNetCoreApp>
5-
<UnsupportedOSPlatforms>browser</UnsupportedOSPlatforms>
5+
<IncludePlatformAttributes>true</IncludePlatformAttributes>
66
</PropertyGroup>
7-
</Project>
7+
</Project>

src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Microsoft.Extensions.FileProviders.Physical.csproj

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<RootNamespace>Microsoft.Extensions.FileProviders</RootNamespace>
5-
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppCurrent)-Browser;netstandard2.0;net461</TargetFrameworks>
5+
<TargetFrameworks>$(NetCoreAppCurrent);netstandard2.0;net461</TargetFrameworks>
66
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
77
<EnableDefaultItems>true</EnableDefaultItems>
88
<PackageDescription>File provider for physical files for Microsoft.Extensions.FileProviders.</PackageDescription>
99
</PropertyGroup>
10-
11-
<PropertyGroup Condition="'$(TargetsBrowser)' == 'true'">
12-
<EnableDefaultItems>false</EnableDefaultItems>
13-
<GeneratePlatformNotSupportedAssemblyMessage>SR.FileProvidersPhysical_PlatformNotSupported</GeneratePlatformNotSupportedAssemblyMessage>
14-
</PropertyGroup>
1510

1611
<ItemGroup>
1712
<Compile Include="$(CommonPath)Extensions\EmptyDisposable.cs"

src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFileProvider.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,13 @@ internal PhysicalFilesWatcher CreateFileWatcher()
161161
string root = PathUtils.EnsureTrailingSlash(Path.GetFullPath(Root));
162162

163163
// When both UsePollingFileWatcher & UseActivePolling are set, we won't use a FileSystemWatcher.
164-
FileSystemWatcher watcher = UsePollingFileWatcher && UseActivePolling ? null : new FileSystemWatcher(root);
164+
FileSystemWatcher watcher = UsePollingFileWatcher && UseActivePolling ? null :
165+
#if NETCOREAPP
166+
OperatingSystem.IsBrowser() ? throw new PlatformNotSupportedException(SR.Format(SR.FileSystemWatcher_PlatformNotSupported, typeof(FileSystemWatcher))) : new FileSystemWatcher(root);
167+
#else
168+
new FileSystemWatcher(root);
169+
#endif
170+
165171
return new PhysicalFilesWatcher(root, watcher, UsePollingFileWatcher, _filters)
166172
{
167173
UseActivePolling = UseActivePolling,

src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFilesWatcher.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Concurrent;
66
using System.IO;
7+
using System.Runtime.Versioning;
78
using System.Security;
89
using System.Threading;
910
using System.Threading.Tasks;
@@ -88,6 +89,13 @@ public PhysicalFilesWatcher(
8889

8990
if (fileSystemWatcher != null)
9091
{
92+
#if NETCOREAPP
93+
if (OperatingSystem.IsBrowser())
94+
{
95+
throw new PlatformNotSupportedException(SR.Format(SR.FileSystemWatcher_PlatformNotSupported, typeof(FileSystemWatcher)));
96+
}
97+
#endif
98+
9199
_fileWatcher = fileSystemWatcher;
92100
_fileWatcher.IncludeSubdirectories = true;
93101
_fileWatcher.Created += OnChanged;
@@ -140,7 +148,10 @@ public IChangeToken CreateFileChangeToken(string filter)
140148
}
141149

142150
IChangeToken changeToken = GetOrAddChangeToken(filter);
151+
// We made sure that browser never uses FileSystemWatcher.
152+
#pragma warning disable CA1416 // Validate platform compatibility
143153
TryEnableFileSystemWatcher();
154+
#pragma warning restore CA1416 // Validate platform compatibility
144155

145156
return changeToken;
146157
}
@@ -264,6 +275,7 @@ protected virtual void Dispose(bool disposing)
264275
}
265276
}
266277

278+
[UnsupportedOSPlatform("browser")]
267279
private void OnRenamed(object sender, RenamedEventArgs e)
268280
{
269281
// For a file name change or a directory's name change notify registered tokens.
@@ -296,11 +308,13 @@ ex is DirectoryNotFoundException ||
296308
}
297309
}
298310

311+
[UnsupportedOSPlatform("browser")]
299312
private void OnChanged(object sender, FileSystemEventArgs e)
300313
{
301314
OnFileSystemEntryChange(e.FullPath);
302315
}
303316

317+
[UnsupportedOSPlatform("browser")]
304318
private void OnError(object sender, ErrorEventArgs e)
305319
{
306320
// Notify all cache entries on error.
@@ -310,6 +324,7 @@ private void OnError(object sender, ErrorEventArgs e)
310324
}
311325
}
312326

327+
[UnsupportedOSPlatform("browser")]
313328
private void OnFileSystemEntryChange(string fullPath)
314329
{
315330
try
@@ -332,6 +347,7 @@ ex is SecurityException ||
332347
}
333348
}
334349

350+
[UnsupportedOSPlatform("browser")]
335351
private void ReportChangeForMatchedEntries(string path)
336352
{
337353
if (string.IsNullOrEmpty(path))
@@ -368,6 +384,7 @@ private void ReportChangeForMatchedEntries(string path)
368384
}
369385
}
370386

387+
[UnsupportedOSPlatform("browser")]
371388
private void TryDisableFileSystemWatcher()
372389
{
373390
if (_fileWatcher != null)
@@ -385,6 +402,7 @@ private void TryDisableFileSystemWatcher()
385402
}
386403
}
387404

405+
[UnsupportedOSPlatform("browser")]
388406
private void TryEnableFileSystemWatcher()
389407
{
390408
if (_fileWatcher != null)

src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Resources/Strings.resx

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<root>
33
<!--
4-
Microsoft ResX Schema
5-
4+
Microsoft ResX Schema
5+
66
Version 2.0
7-
8-
The primary goals of this format is to allow a simple XML format
9-
that is mostly human readable. The generation and parsing of the
10-
various data types are done through the TypeConverter classes
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
1111
associated with the data types.
12-
12+
1313
Example:
14-
14+
1515
... ado.net/XML headers & schema ...
1616
<resheader name="resmimetype">text/microsoft-resx</resheader>
1717
<resheader name="version">2.0</resheader>
@@ -26,36 +26,36 @@
2626
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
2727
<comment>This is a comment</comment>
2828
</data>
29-
30-
There are any number of "resheader" rows that contain simple
29+
30+
There are any number of "resheader" rows that contain simple
3131
name/value pairs.
32-
33-
Each data row contains a name, and value. The row also contains a
34-
type or mimetype. Type corresponds to a .NET class that support
35-
text/value conversion through the TypeConverter architecture.
36-
Classes that don't support this are serialized and stored with the
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
3737
mimetype set.
38-
39-
The mimetype is used for serialized objects, and tells the
40-
ResXResourceReader how to depersist the object. This is currently not
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
4141
extensible. For a given mimetype the value must be set accordingly:
42-
43-
Note - application/x-microsoft.net.object.binary.base64 is the format
44-
that the ResXResourceWriter will generate, however the reader can
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
4545
read any of the formats listed below.
46-
46+
4747
mimetype: application/x-microsoft.net.object.binary.base64
4848
value : The object must be serialized with
4949
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
5050
: and then encoded with base64 encoding.
51-
51+
5252
mimetype: application/x-microsoft.net.object.soap.base64
53-
value : The object must be serialized with
53+
value : The object must be serialized with
5454
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
5555
: and then encoded with base64 encoding.
5656
5757
mimetype: application/x-microsoft.net.object.bytearray.base64
58-
value : The object must be serialized into a byte array
58+
value : The object must be serialized into a byte array
5959
: using a System.ComponentModel.TypeConverter
6060
: and then encoded with base64 encoding.
6161
-->
@@ -129,7 +129,7 @@
129129
<data name="UnexpectedFileSystemInfo" xml:space="preserve">
130130
<value>Unexpected type of FileSystemInfo</value>
131131
</data>
132-
<data name="FileProvidersPhysical_PlatformNotSupported" xml:space="preserve">
133-
<value>Microsoft.Extensions.FileProviders.Physical is not supported on this platform.</value>
132+
<data name="FileSystemWatcher_PlatformNotSupported" xml:space="preserve">
133+
<value>The type {0} is not supported on this platform, use polling instead.</value>
134134
</data>
135135
</root>

src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ private void CreateHostingEnvironment()
192192
_hostingEnvironment.ApplicationName = Assembly.GetEntryAssembly()?.GetName().Name;
193193
}
194194

195-
#pragma warning disable CA1416 // 'PhysicalFileProvider' is unsupported on: 'browser' https://github.com/dotnet/runtime/issues/56178
196195
_hostingEnvironment.ContentRootFileProvider = _defaultProvider = new PhysicalFileProvider(_hostingEnvironment.ContentRootPath);
197-
#pragma warning restore CA1416 // 'PhysicalFileProvider' is unsupported on: 'browser'
198196
}
199197

200198
private string ResolveContentRootPath(string contentRootPath, string basePath)

0 commit comments

Comments
 (0)