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

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

Just slight modification in pfx code to remove the dependency on Newtonsoft Json.Net. String extension method to convert the given string to SnakeCase. public static class StringUtils { public static string ToSnakeCase(this string str) { return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? “_” + x.ToString() : x.ToString())).ToLower(); } } Then in our … Read more

Deserialization of reference types without parameterless constructor is not supported

You can achieve your desired result. You need to switch to NewtonsoftJson serialization (from package Microsoft.AspNetCore.Mvc.NewtonsoftJson) Call this in Startup.cs in the ConfigureServices method: services.AddControllers().AddNewtonsoftJson(); After this, your constructor will be called by deserialization. Extra info: I am using ASP Net Core 3.1 Later Edit: I wanted to give more info on this, as it … Read more

Parsing a JSON file with .NET core 3.0/System.text.Json

Update 2019-10-13: Rewritten the Utf8JsonStreamReader to use ReadOnlySequences internally, added wrapper for JsonSerializer.Deserialize method. I have created a wrapper around Utf8JsonReader for exactly this purpose: public ref struct Utf8JsonStreamReader { private readonly Stream _stream; private readonly int _bufferSize; private SequenceSegment? _firstSegment; private int _firstSegmentStartIndex; private SequenceSegment? _lastSegment; private int _lastSegmentEndIndex; private Utf8JsonReader _jsonReader; private bool … 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

Enum type no longer working in .Net core 3.0 FromBody request object

For those who are looking for a snippet when using System.Text.Json public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddJsonOptions(opt => { opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); }); } .NET 6 / Top-level statement style using System.Text.Json.Serialization; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers() //convert strings to enums .AddJsonOptions(options => options.JsonSerializerOptions.Converters .Add(new JsonStringEnumConverter()));

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