C# – How to change StreamWriter’s buffer size

StreamWriter buffers by writing data to an internal char array with a default size of 1024 (and a minimum size of 128). Once the buffer is full (or when you dispose the StreamWriter), it flushes the buffer to the underlying stream. You can change StreamWriter’s buffer size by passing in the bufferSize parameter. Here’s an … Read more

C# – How to create a file and write to it

There are a few ways to create a file and write to it using the .NET File API (in System.IO). The simplest way is to use high-level methods like File.WriteAllText() and File.WriteAllLines(), specifying the file path and string(s) to write to the file. Here’s an example of using these (and their async equivalents): These high-level … Read more

Error: Synchronous operations are disallowed

Problem When you try to do a synchronous IO operation in ASP.NET Core (such as writing to a file), you get the following exception: System.InvalidOperationException: ‘Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true The reason you’re getting this is because server option AllowSynchronousIO is false. Starting in ASP.NET Core 3.0, this is … Read more

ASP.NET – Async SSE endpoint

Server-Sent Events (SSE) allow a client to subscribe to messages on a server. The server pushes new messages to the client as they happen. This is an alternative to the client polling the server for new messages. In this article I’ll show how to implement the messaging system shown in the diagram below. This uses … Read more