VS 2022 – Convert to file-scoped namespace in all files

Adding a rule to use file scoped namespaces in .editorconfig worked for me: create an .editorconfig file in the solution directory add following line/content below (docs, code – IDE0161) Example .editorconfig file content: [*.cs] csharp_style_namespace_declarations = file_scoped:warning After that the preview changes dialog had an option to apply the fix to the whole project/solution:

Throwing ArgumentNullException

I prefer the ArgumentNullException over the NullReferenceException that not checking the argument would provide. In general, my preference is to always check for nullity before trying to invoke a method on a potentially null object. If the method is a constructor, then it would depend on a couple of different factors: is there also a … Read more

Equivalent to UserSettings / ApplicationSettings in WPF .NET 5, .NET 6 or .Net Core

You can add the same old good settings file e.g. via the right click on the Properties -> Add -> New Item and search for the “Settings”. The file can be edited in the settings designer and used as in the .net framework projects before (ConfigurationManager, Settings.Default.Upgrade(), Settings.Default.Save, etc. works). Add also the app.config file … Read more

How to use appsettings.json in Asp.net core 6 Program.cs file

While the examples above work, the way to do this is the following: var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); builder.Services.AddStackExchangeRedisCache(options => { options.Configuration = builder.Configuration[“Redis”]; }); The WebApplicationBuilder has a configuration object as a property that you can use.

Publish error: Found multiple publish output files with the same relative path

Issue: The issue raises after .NET 6 migration. There’s a new feature that blocks multiple files from being copied to the same target directory with the same file name. See https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/6.0/duplicate-files-in-output Solution #1 (workaround): You can add the following build property to all your publishable (*.Web) projects’ *.csproj files. This property will bypass this check … Read more

ASP.NET Core 6 how to access Configuration during startup

WebApplicationBuilder returned by WebApplication.CreateBuilder(args) exposes Configuration and Environment properties: var builder = WebApplication.CreateBuilder(args); // Add services to the container. … ConfigurationManager configuration = builder.Configuration; // allows both to access and to set up the config IWebHostEnvironment environment = builder.Environment; WebApplication returned by WebApplicationBuilder.Build() also exposes Configuration and Environment: var app = builder.Build(); IConfiguration configuration = … Read more

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