.NET 7 - Minimal APIs - Configurations
.NET 7 - Minimal APIs - Configurations

.NET 7 – Using Configurations with Minimal API

In this article, let’s try to move these configurations to the config file – appsettings.json. I expect the modifications required for this to be same as in regular .NET API project with controllers. But let’s see how it goes.

Before we begin !

I would recommend to follow the steps mentioned in previous articles to get the solutions up and running.

Connection String Configurations

In AppBuilder.cs, the connection string is currently hardcoded. Let’s try to add the connection string to appsettings.json and then try to read it from there.

For reading the configurations, the application would need IConfiguration instance. How to get that ?

We can access the configuration object by using WebApplicationBuilder instance. It has a property Configuration to expose a ConfigurationManager instance. Once we got this object, we can use various ways to read the configuration values.

The code snippet given below shows the contents of both appsettings.json and AppBuilder.cs file. The class is updated to read the connection string from config file and then pass it to the DbContext.

After making this change, we can run and verify that the application is still working.

Logging Configurations

We also have used Serilog for diagnostics logging to the files. Let’s try to add the configurations related to it in appsettings.json.

Serilog configurations allow adding “WriteTo” configurations in the configuration file. In that configuration, we can specify the sink that we want to use and additional configurations required for that sink.

For example,

  • for Console sink, no additional configurations are needed
  • for File sink, file path argument is required, which will specify where the file should be created
  • for SQL sink, the connection string of the database is required to be configured, which will specify where the logs should be written.

In our example, we are going to add only two Sinks, console and file.

After this configuration is added to the config file, we can modify our code to read the logging configuration. For that we can use – UseSerilog overload, which accepts host context and logging configuration parameters. The code snippet (file – AppBuilder.cs) given below shows how the loggers can be initialized from the configuration.

Wrapping Up

As you can see, we have started with very basic minimal API project template and we have added many capabilities in that project. The project now uses EF Core code first to connect to the database and Serilog file sinks for diagnostics logging. We also have seen how to use dependency injection and configurations with minimal API project.

In the coming articles, we will try to build Single Page Application UI. Then we will try to deploy this application on a cloud platform. Do you have any suggestions on what do you want me to use for deploying it to Azure ? Do you want to know about any specific things related to minimal APIs ? Let me know your thoughts.

Leave a ReplyCancel reply