JWT Authentication and Swagger with .NET Core 3.0

After some research, I eventually found the answer here Before seeing this page, I knew that I should use AddSecurityRequirement after AddSecurityDefinition because of many samples, but it was a problem that the function parameters have changed on .NET Core 3.0. By the way, the final answer is as below: services.AddSwaggerGen(c => { c.SwaggerDoc(“v1”, new … Read more

How to setup app settings in a .Net Core 3 Worker Service

If for example the worker class needed access to some data stored in your appsettings public class Worker : BackgroundService { private readonly ILogger<Worker> logger; private readonly WorkerOptions options; public Worker(ILogger<Worker> logger, WorkerOptions options) { this.logger = logger; this.options = options; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { //do something that … Read more

ASP.NET MVC Core API Serialize Enums to String

New System.Text.Json serialization ASP.NET MVC Core 3.0 uses built-in JSON serialization. Use System.Text.Json.Serialization.JsonStringEnumConverter (with “Json” prefix): services .AddMvc() // Or .AddControllers(…) .AddJsonOptions(opts => { var enumConverter = new JsonStringEnumConverter(); opts.JsonSerializerOptions.Converters.Add(enumConverter); }) More info here. The documentation can be found here. If you prefer Newtonsoft.Json You can also use “traditional” Newtonsoft.Json serialization: Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson And then: … Read more

Can WPF applications be run in Linux or Mac with .Net Core 3?

No, they have clearly stated that these are windows only. In one of the .NET Core 3.0 discussions, they have also clarified that they do not intend to make these features cross-platform in the future since the whole concept is derived from windows specific features. They talked about thinking of a whole new idea for … Read more

ASP.NET Core 3.0 not showing on Visual Studio 2019

There is an option in Tools -> Options that enables preview versions of the .NET Core SDK. In the VS Preview shipping channel, it is on (by default, and not settable). In the VS Release channel, it defaults to off and you can opt-in. (Note: it’s disabled in the screenshot because I have a Preview … Read more

Is polymorphic deserialization possible in System.Text.Json?

Is polymorphic deserialization possible in System.Text.Json? The answer is yes and no, depending on what you mean by “possible”. There is no polymorphic deserialization (equivalent to Newtonsoft.Json’s TypeNameHandling) support built-in to System.Text.Json. This is because reading the .NET type name specified as a string within the JSON payload (such as $type metadata property) to create … Read more

System.Text.Json.JsonElement ToObject workaround

I came across the same issue, so I wrote some extension methods which work fine for now. It would be nice if they provided this as built in to avoid the additional allocation to a string. public static T ToObject<T>(this JsonElement element) { var json = element.GetRawText(); return JsonSerializer.Deserialize<T>(json); } public static T ToObject<T>(this JsonDocument … Read more

“The project ‘Web’ must provide a value for Configuration” error after migrating to .NET Core 3

The issue turned out to be that I was still referencing Microsoft.AspNetCore.Razor.Design Version=”2.2.0″ in the .proj file’s package references. Deleting that reference (which isn’t needed at all as Razor.Design is now part of AspNetCore library) fixed the issue. Once I’d done that, I then got hundreds of errors about nullable objects being a new feature … Read more