C# – Global exception event handlers

There are two global exception event available in all .NET applications: FirstChanceException: When any exception is thrown, this event is fired before anything else. UnhandledException: When there’s an unhandled exception, this event is fired right before the process is terminated. You wire up these event handlers in Main() (before anything else has executed), like this: … Read more

ASP.NET Core – Log messages to the database in the background

I was reading about logging in ASP.NET when I came across this statement about logging to the database: When logging to SQL Server, don’t do so directly. Instead, add log messages to an in-memory queue and have a background worker dequeue and insert data to SQL Server. Paraphrased from https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0#no-asynchronous-logger-methods In this article, I’ll show … Read more

C# – How to supply IOptions

The options pattern is an indirect way to dependency inject settings into a registered service. If you’re using code that implements the options pattern, then you’re required to supply an IOptions<T> object. For example, let’s say you’re using the MovieService class and it has the following constructor: This requires you to supply the IOptions<MovieSettings> parameter. … Read more

ASP0000 Calling BuildServiceProvider from application code results in an additional copy of singleton services being created

When you try to call services.BuildServiceProvider(), you’ll get the following warning: Warning ASP0000 Calling ‘BuildServiceProvider’ from application code results in an additional copy of singleton services being created. Consider alternatives such as dependency injecting services as parameters to ‘Configure’. There are two scenarios where you may be calling BuildServiceProvider() because you want to resolve services … Read more

ASP.NET Core – How to change the JSON serialization settings

System.Text.Json is the default JSON serializer in ASP.NET Core. It uses the following default serialization settings: To change the settings at the service level for all controllers, call AddJsonOptions() in Startup.ConfigureServices() like this: Note: This example is passing in JsonStringEnumConverter, which makes JsonSerializer use the enum name instead of enum value. When you change the … Read more

ASP.NET Core – How to make the controllers use Newtonsoft

By default, ASP.NET Core uses System.Text.Json for JSON serialization. If you want to use Newtonsoft instead, you can add the Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget package, then call AddNewtonsoftJson() in Startup.ConfigureServices() like this: In this article, I’ll show how to configure the Newtonsoft serializer options. Install the right nuget package Microsoft packaged up Microsoft.AspNetCore.Mvc.NewtonsoftJson based on the framework … Read more

System.InvalidOperationException: Unable to resolve service for type while attempting to activate

When the built-in dependency injection functionality is trying to create a type, it tries to resolve all of the constructor parameters. If it can’t resolve one of the parameters, it’ll throw a variation of one of these exceptions: InvalidOperationException: Unable to resolve service for type <Type Name> while attempt to activate <Type Name>. ArgumentException: Cannot … Read more

ASP.NET – How to add User Secrets

To add User Secrets in an ASP.NET project in Visual Studio do the following steps: Right-click the project and click Manage User Secrets. This will create and open a file called secrets.json. Override appsettings.json settings by putting them in secrets.json For example, let’s say your appsettings.json looks like this: You want to override ApiKey. In … Read more