Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.NotSupportedException F# snippets #7801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module BadState

// <Snippet1>
open System.IO
open System.Text

let main = task {
let enc = Encoding.Unicode
let value = "This is a string to persist."
let bytes = enc.GetBytes value

let fs = new FileStream(@".\TestFile.dat", FileMode.Open, FileAccess.Read)
let t = fs.WriteAsync(enc.GetPreamble(), 0, enc.GetPreamble().Length)
let t2 = t.ContinueWith(fun a -> fs.WriteAsync(bytes, 0, bytes.Length))
let! _ = t2
fs.Close()
}
main.Wait()

// The example displays the following output:
// Unhandled Exception: System.NotSupportedException: Stream does not support writing.
// at System.IO.Stream.BeginWriteInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state
// , Boolean serializeAsynchronously)
// at System.IO.FileStream.BeginWrite(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object sta
// teObject)
// at System.IO.Stream.<>c.<BeginEndWriteAsync>b__53_0(Stream stream, ReadWriteParameters args, AsyncCallback callback,
// Object state)
// at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet
// hod, Func`3 endMethod)
// at System.IO.Stream.BeginEndWriteAsync(Byte[] buffer, Int32 offset, Int32 count)
// at System.IO.FileStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
// at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count)
// at <StartupCode:fs>.main()
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module TestProp1

// <Snippet3>
open System
open System.IO

module FileUtilities =
type EncodingType =
| None = 0
| Unknown = -1
| Utf8 = 1
| Utf16 = 2
| Utf32 = 3

let getEncodingType (fs: FileStream) =
task {
let bytes = Array.zeroCreate<byte> 4
let! bytesRead = fs.ReadAsync(bytes, 0, 4)
if bytesRead < 2 then
return EncodingType.None

elif bytesRead >= 3 && bytes[0] = 0xEFuy && bytes[1] = 0xBBuy && bytes[2] = 0xBFuy then
return EncodingType.Utf8
else
let value = BitConverter.ToUInt32(bytes, 0)
if bytesRead = 4 && (value = 0x0000FEFFu || value = 0xFEFF0000u) then
return EncodingType.Utf32
else
let value16 = BitConverter.ToUInt16(bytes, 0)
if value16 = 0xFEFFus || value16 = 0xFFFEus then
return EncodingType.Utf16
else
return EncodingType.Unknown
}

let main _ =
task {
let name = @".\TestFile.dat"
let fs = new FileStream(name, FileMode.Create, FileAccess.Write)
let! et = FileUtilities.getEncodingType fs
printfn $"Filename: {name}, Encoding: {et}"
}

// The example displays the following output:
// Unhandled Exception: System.NotSupportedException: Stream does not support reading.
// at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback callback, Object state)
// at System.IO.Stream.<>c.<BeginEndReadAsync>b__46_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state)
// at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance, TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod)
// at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
// at System.IO.FileStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
// at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count)
// at FileUtilities.GetEncodingType(FileStream fs)
// at Example.Main()
// at Example.<Main>()
// </Snippet3>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module TestProp2

open System
open System.IO

module FileUtilities =
type EncodingType =
| None = 0
| Unknown = -1
| Utf8 = 1
| Utf16 = 2
| Utf32 = 3

// <Snippet4>
let getEncodingType (fs: FileStream) =
task {
if not fs.CanRead then
return EncodingType.Unknown
else
let bytes = Array.zeroCreate<byte> 4
let! bytesRead = fs.ReadAsync(bytes, 0, 4)
if bytesRead < 2 then
return EncodingType.None

elif bytesRead >= 3 && bytes[0] = 0xEFuy && bytes[1] = 0xBBuy && bytes[2] = 0xBFuy then
return EncodingType.Utf8
else
let value = BitConverter.ToUInt32(bytes, 0)
if bytesRead = 4 && (value = 0x0000FEFFu || value = 0xFEFF0000u) then
return EncodingType.Utf32
else
let value16 = BitConverter.ToUInt16(bytes, 0)
if value16 = 0xFEFFus || value16 = 0xFFFEus then
return EncodingType.Utf16
else
return EncodingType.Unknown
}
// The example displays the following output:
// Filename: .\TestFile.dat, Encoding: Unknown
// </Snippet4>

let main _ =
task {
let name = @".\TestFile.dat"
let fs = new FileStream(name, FileMode.Create, FileAccess.Write)
let! et = FileUtilities.getEncodingType fs
printfn $"Filename: {name}, Encoding: {et}"
}
12 changes: 12 additions & 0 deletions snippets/fsharp/System/NotSupportedException/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="BadState1.fs" />
<Compile Include="TestProp1.fs" />
<Compile Include="TestProp2.fs" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions xml/System/NotSupportedException.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
- You know the state of the object in advance (usually because your code has instantiated it), but the object is mis-configured. The following example illustrates this issue. It creates a read-only <xref:System.IO.FileStream> object and then attempts to write to it.

:::code language="csharp" source="~/snippets/csharp/System/NotSupportedException/Overview/BadState1.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/NotSupportedException/Overview/BadState1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.NotSupportedException/vb/BadState1.vb" id="Snippet1":::

You can eliminate the exception by ensuring that the instantiated object supports the functionality you intend. The following example addresses the problem of the read-only <xref:System.IO.FileStream> object by providing the correct arguments to the <xref:System.IO.FileStream.%23ctor%28System.String%2CSystem.IO.FileMode%2CSystem.IO.FileAccess%29?displayProperty=nameWithType> constructor.
Expand All @@ -107,11 +108,13 @@
The following example defines a `DetectEncoding` method that throws a <xref:System.NotSupportedException> exception when it attempts to read from the beginning of a stream that does not support read access.

:::code language="csharp" source="~/snippets/csharp/System/NotSupportedException/Overview/TestProp1.cs" id="Snippet3":::
:::code language="fsharp" source="~/snippets/fsharp/System/NotSupportedException/Overview/TestProp1.fs" id="Snippet3":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.NotSupportedException/vb/TestProp1.vb" id="Snippet3":::

You can eliminate the exception by examining the value of the <xref:System.IO.FileStream.CanRead%2A?displayProperty=nameWithType> property and exiting the method if the stream is read-only.

:::code language="csharp" source="~/snippets/csharp/System/NotSupportedException/Overview/TestProp2.cs" id="Snippet4":::
:::code language="fsharp" source="~/snippets/fsharp/System/NotSupportedException/Overview/TestProp2.fs" id="Snippet4":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.NotSupportedException/vb/TestProp2.vb" id="Snippet4":::

## Related exception types
Expand Down