C# – How to change the HttpClient timeout per request

It’s best practice to reuse the same HttpClient instance for multiple requests. When you’re using the same instance repeatedly, and you want to change the timeout per request, you can pass in a CancellationToken, like this: You can’t change HttpClient.Timeout after the instance has been used. You have to pass in a CancellationToken instead. There … Read more

ASP.NET Core – How to make the controllers use Newtonsoft

By default, ASP.NET Core uses System.Text.Json for JSON serialization. If you want to use Newtonsoft instead, you can add the Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget package, then call AddNewtonsoftJson() in Startup.ConfigureServices() like this: In this article, I’ll show how to configure the Newtonsoft serializer options. Install the right nuget package Microsoft packaged up Microsoft.AspNetCore.Mvc.NewtonsoftJson based on the framework … Read more

WinForms – How to remove checked items from CheckedListBox

Normally the items in a CheckedListBox are hardcoded or added programmatically (from an enum, or from some data source). But sometimes you may want to allow the user to add and remove items from a CheckedListItem. In this article I’ll show how to add items and remove them. I’ll be working with text, and will … Read more

ASP.NET – InvalidOperationException: Synchronous operations are disallowed

Problem You’re trying to do synchronous IO operations in ASP.NET and 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 false by default. Solution Microsoft turned this setting … Read more