Is there a built in way of using snake case as the naming policy for JSON in ASP.NET Core 3?

Update .Net 8 has been released and it has built in support for different naming policies. var serializeOptions = new JsonSerializerOptions { // This can be changed to other naming policies like SnakeCaseLower, KebabCaseLower PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true }; jsonString = JsonSerializer.Serialize(weatherForecast, serializeOptions); Old Answer Just slight modification in pfx code to remove … Read more

How to fix ‘The current thread is not associated with the renderer’s synchronization context’?

I have just implemented a State Container like this and ran into the same error – but my service needs to be a singleton. So I found an example on the aspnetcore git that does exactly what the error message says to do. Call InvokeAsync — not from your state container but when you try … Read more

Migrating .NET Core 2 to .NET Core 3: HttpContent does not contain a definition for “ReadAsAsync”

ReadAsAsync is a .NET Standard extension that’s actually shared between ASP.NET Core and ASP.NET Web Api (via a NuGet library). However, it uses JSON.NET to do the deserialization, and as of .NET Core 3.0, ASP.NET Core now uses System.Text.Json instead. As such, this library (and the extension it contains) is not included in the .NET … Read more

Getting HttpRequestExceptions: The response ended prematurely

You just need to keep digging. The exception “The response ended prematurely” isn’t the root cause. Keep digging into the inner exceptions until you find the last one. You’ll find this: System.IO.IOException: Authentication failed because the remote party has closed the transport stream. So it’s not about your code. It seems the server you’re hitting … Read more

Cannot add appsettings.json inside WPF project .net core 3.0

Steps: To Add the following nuget packages Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration.FileExtensions Microsoft.Extensions.Configuration.Json Microsoft.Extensions.DependencyInjection You would need to create and add appsettings.json manually and set copy it to output directory as copy if newer AppSetting.json { “ConnectionStrings”: { “BloggingDatabase”: “Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;” }, } Program.cs (For .NetCore Console App) static void Main(string[] args) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) … Read more

JsonConverter equivalent in using System.Text.Json

System.Text.Json now supports custom type converters in .NET 3.0 preview-7 and above. You can add converters that match on type, and use the JsonConverter attribute to use a specific converter for a property. Here’s an example to convert between long and string (because javascript doesn’t support 64-bit integers). public class LongToStringConverter : JsonConverter<long> { public … Read more

How to use class fields with System.Text.Json.JsonSerializer?

In .NET Core 3.x, System.Text.Json does not serialize fields. From the docs: Fields are not supported in System.Text.Json in .NET Core 3.1. Custom converters can provide this functionality. In .NET 5 and later, public fields can be serialized by setting JsonSerializerOptions.IncludeFields to true or by marking the field to serialize with [JsonInclude]: using System.Text.Json; static … Read more

.NET Core 3 preview 4: ‘AddNewtonsoftJson’ is not defined

In order to switch ASP.NET Core 3.0 back to use JSON.NET, you will need to reference the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package. That will contain the AddNewtonsoftJson extension method. In C#, this would look like this: services.AddControllers() .AddNewtonsoftJson(); So assuming that I understand enough of F#, I would say that your call would be correct if you … Read more