How to globally set default options for System.Text.Json.JsonSerializer?

You can create an extension method. Here’s an example I use separate methods vs having to build special settings, so that all the settings will be in a single spot and easily reusable. public static class DeserializeExtensions { private static JsonSerializerOptions defaultSerializerSettings = new JsonSerializerOptions(); // set this up how you need to! private static … Read more

Net Core: Execute All Dependency Injection in Xunit Test for AppService, Repository, etc

You are mixing unit test with integration test. TestServer is for integration test and if you want to reuse Startup class to avoid register dependencies again, you should use HttpClient and make HTTP call to controller and action that use IDepartmentAppService. If you want do unit test, you need to setup DI and register all … Read more

How to resolve .net core build error NETDSDK1061 and warning MSB3277

I’ve tried to find any help on that issue, finding some GitHub issues (e.g. this one) looking pretty similar, but were actually different. I’ve found a descriptive doc, but that didn’t really helped me. I found a pretty helpful blog post from Rick Strahl, explaining what packages are available and what the purpose of each … Read more

Getting nested properties with System.Text.Json

You could add a couple of extension methods that access a child JsonElement value by property name or array index, returning a nullable value if not found: public static partial class JsonExtensions { public static JsonElement? Get(this JsonElement element, string name) => element.ValueKind != JsonValueKind.Null && element.ValueKind != JsonValueKind.Undefined && element.TryGetProperty(name, out var value) ? … Read more

Conditional dependency resolver on run-time (.net Core)

There are several options here, but the two that to me are the most obvious are using a factory or the adapter pattern. 1.1 Use a factory public class OrderService { private readonly IPaymentGatewayFactory _factory; public void DoOrder(bool isFoo) { IPaymentGateway service = _factory.Create(isFoo); this._service.Pay(); } } Where the factory can be: public class PaymentGatewayFactory … Read more

Asp.net core MVC post parameter always null

This could be because of how the null values are being handled. Set NullValueHandling to Ignore in AddJsonOptions and see if that works. public void ConfigureServices(IServiceCollection services) { services .AddMvc() .AddJsonOptions(jsonOptions=> { jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; }); }

P-Invoke in .net core with Linux

PInvoke is certainly supported (that is how all of the code that interfaces with the OS works), but not in the specific case you listed. You cannot PInvoke to a win32 binary on Linux, unless you have somehow built a function-compatible version of it for Linux yourself. More realistically, you need to find a Linux … Read more

Is project.json deprecated?

Update: As of Visual Studio 2017 and the latest dotnet CLI, project.json is officially dead. Visual Studio will migrate projects automatically, and there is a comparison chart here: https://learn.microsoft.com/en-us/dotnet/articles/core/tools/project-json-to-csproj project.json is indeed going away. As part of a future update to the .NET Core tooling, .xproj/project.json will be merged back into .csproj. However, the team … Read more

ASP.NET Core modify/substitute request body

Take the request body, read its content, make whatever changes are necessary if at all, then create a new stream to pass down the pipeline. Once accessed, the request stream has to be replaced. public async Task Invoke(HttpContext context) { var request = context.Request; if (request.Path.Value.Contains(“DataSourceResult”)) { //get the request body and put it back … Read more