How to configure and use Serilog in ASP.NET Core 6?

You’ll need to make sure you have the following packages installed: Serilog Serilog.Extensions.Hosting (this provides the .UseSerilog extension method. If you have the Serilog.AspNetCore package, you do not need to explicitly include this) Then you’ll need a using: using Serilog; Which should allow you to access .UseSerilog via builder.Host: using Serilog; var builder = WebApplication.CreateBuilder(args); … Read more

How can I use DateOnly/TimeOnly query parameters in ASP.NET Core 6?

Turns out, there are two solutions: Custom ModelBinder + Swagger configuration Custom TypeConverter (Swagger will automatically see it, and update UI accordingly) I went with TypeConverter, and everything worked! Since .Net team are not planning to add full support for DateOnly/TimeOnly in .Net 6, I’ve decided to create a NuGet to do so: https://www.nuget.org/packages/DateOnlyTimeOnly.AspNet (source … Read more

Subtract DateOnly in C#

Conceptually DateOnly represents an entire day, not midnight or any other specific time on a given day, such that subtracting one DateOnly from another cannot logically return a TimeSpan as with DateTime‘s subtraction operator. If you want to perform arithmetic on DateOnlys, you need to be explicit about the desired unit. DateOnly has a DayNumber … Read more

How to fix Visual Studio 2022 Warning CA1416 “Call site reachable by all platforms” but “only supported on: ‘windows'”?

I had success removing the CA1416 warnings by adding the following decorator to the top of the containing class: [System.Runtime.Versioning.SupportedOSPlatform(“windows”)] I’m only on VS2019 and using .net 5, but it may work for you. I tried this with VS2019 .net5 console project (top of class Program) and a .net5 class library (top of the class). … Read more

RNGCryptoServiceProvider is obsolete

You can use RandomNumberGenerator like this: var randomNumber = new byte[32]; string refreshToken = “”; using (var rng = RandomNumberGenerator.Create()) { rng.GetBytes(randomNumber); refreshToken = Convert.ToBase64String(randomNumber); } Example of result: fHu4SOyv03PhHvqWx5WGmI5xQFnH00CmLCV3IYJd5Dw= Just adjust this to your case.

How to use WebApplicationFactory in .net6 (without speakable entry point)

Note that if you are trying to use xUnit and its IClassFixture<T> pattern, you will run into problems if you just use the InternalsVisibleTo approach. Specifically, you’ll get something like this: “Inconsistent accessibility: base class WebApplicationFactory<Program> is less accessible than class CustomWebApplicationFactory.” Of course you can solve this by making CustomWebApplicationFactory internal but it only … Read more

How to change default port no of my .NET core 6 API

The port is defined in the endpoints and there are multiple ways to change them: For Development purposes You can change in launchSettings.json file inside Properties folder: { “iisSettings”: { “windowsAuthentication”: false, “anonymousAuthentication”: true, “iisExpress”: { “applicationUrl”: “http://localhost:22963”, “sslPort”: 44349 } }, “profiles”: { “UrlTest”: { “commandName”: “Project”, “dotnetRunMessages”: true, “launchBrowser”: true, “applicationUrl”: “https://localhost:7244;http://localhost:5053”, “environmentVariables”: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)