diff --git a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
index 6082e0ec3f6b86..f511ebeb21810d 100644
--- a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
+++ b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
@@ -403,12 +403,6 @@
Byte array for Guid must be exactly {0} bytes long.
-
- Handle does not support asynchronous operations. The parameters to the FileStream constructor may need to be changed to indicate that the handle was opened synchronously (that is, it was not opened for overlapped I/O).
-
-
- Handle does not support synchronous operations. The parameters to the FileStream constructor may need to be changed to indicate that the handle was opened asynchronously (that is, it was opened explicitly for overlapped I/O).
-
The number styles AllowHexSpecifier and AllowBinarySpecifier are not supported on floating point data types.
diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs b/src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs
index cee3482ea93579..50f15b4165c117 100644
--- a/src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs
@@ -46,9 +46,9 @@ public FileStream(IntPtr handle, FileAccess access, bool ownsHandle, int bufferS
SafeFileHandle safeHandle = new SafeFileHandle(handle, ownsHandle: ownsHandle);
try
{
- ValidateHandle(safeHandle, access, bufferSize, isAsync);
+ ValidateHandle(safeHandle, access, bufferSize);
- _strategy = FileStreamHelpers.ChooseStrategy(this, safeHandle, access, bufferSize, isAsync);
+ _strategy = FileStreamHelpers.ChooseStrategy(this, safeHandle, access, bufferSize, safeHandle.IsAsync);
}
catch
{
@@ -84,20 +84,6 @@ private static void ValidateHandle(SafeFileHandle handle, FileAccess access, int
}
}
- private static void ValidateHandle(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
- {
- ValidateHandle(handle, access, bufferSize);
-
- if (isAsync && !handle.IsAsync)
- {
- ThrowHelper.ThrowArgumentException_HandleNotAsync(nameof(handle));
- }
- else if (!isAsync && handle.IsAsync)
- {
- ThrowHelper.ThrowArgumentException_HandleNotSync(nameof(handle));
- }
- }
-
public FileStream(SafeFileHandle handle, FileAccess access)
: this(handle, access, DefaultBufferSize)
{
@@ -112,9 +98,9 @@ public FileStream(SafeFileHandle handle, FileAccess access, int bufferSize)
public FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
{
- ValidateHandle(handle, access, bufferSize, isAsync);
+ ValidateHandle(handle, access, bufferSize);
- _strategy = FileStreamHelpers.ChooseStrategy(this, handle, access, bufferSize, isAsync);
+ _strategy = FileStreamHelpers.ChooseStrategy(this, handle, access, bufferSize, handle.IsAsync);
}
public FileStream(string path, FileMode mode)
diff --git a/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs b/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs
index a477e06af0e413..d87f6b114a9cf6 100644
--- a/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs
@@ -335,18 +335,6 @@ internal static void ThrowArgumentException(ExceptionResource resource, Exceptio
throw GetArgumentException(resource, argument);
}
- [DoesNotReturn]
- internal static void ThrowArgumentException_HandleNotSync(string paramName)
- {
- throw new ArgumentException(SR.Arg_HandleNotSync, paramName);
- }
-
- [DoesNotReturn]
- internal static void ThrowArgumentException_HandleNotAsync(string paramName)
- {
- throw new ArgumentException(SR.Arg_HandleNotAsync, paramName);
- }
-
[DoesNotReturn]
internal static void ThrowArgumentNullException(ExceptionArgument argument)
{
diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/ctor_sfh_fa_buffer_async.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/ctor_sfh_fa_buffer_async.cs
index 810287764e3293..665bf08dbb3892 100644
--- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/ctor_sfh_fa_buffer_async.cs
+++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/ctor_sfh_fa_buffer_async.cs
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Threading.Tasks;
using Microsoft.Win32.SafeHandles;
using Xunit;
@@ -28,17 +29,31 @@ public void MatchedAsync()
}
}
- [Fact]
- public void UnmatchedAsyncThrows()
+ [Theory]
+ [InlineData(true)]
+ [InlineData(false)]
+ public async Task UnmatchedAsyncIsAllowed(bool isAsync)
{
- using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, 4096, true))
+ using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, 4096, isAsync))
{
- Assert.Throws(() => CreateFileStream(fs.SafeFileHandle, FileAccess.ReadWrite, 4096, false));
- }
+ // isAsync parameter is now ignored, handle.IsAsync is used instead
+ using (FileStream newFs = CreateFileStream(fs.SafeFileHandle, FileAccess.ReadWrite, 4096, !isAsync))
+ {
+ // Verify that the new FileStream uses handle's IsAsync, not the parameter
+ Assert.Equal(isAsync, newFs.IsAsync);
- using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, 4096, false))
- {
- Assert.Throws(() => CreateFileStream(fs.SafeFileHandle, FileAccess.ReadWrite, 4096, true));
+ // Perform async write, seek to beginning, and async read to verify functionality
+ byte[] writeBuffer = new byte[] { 1, 2, 3, 4, 5 };
+ await newFs.WriteAsync(writeBuffer, 0, writeBuffer.Length);
+
+ newFs.Seek(0, SeekOrigin.Begin);
+
+ byte[] readBuffer = new byte[writeBuffer.Length];
+ int bytesRead = await newFs.ReadAsync(readBuffer, 0, readBuffer.Length);
+
+ Assert.Equal(writeBuffer.Length, bytesRead);
+ Assert.Equal(writeBuffer, readBuffer);
+ }
}
}
}