Console app with configuration, dependency injection and logging

I was working on a web application build on .NET core that is using the built-in dependency injection, configuration, and logging. Some functionality needs to be shared in both this web app and also in a console application. The services project used in the web app having this functionality was already using Microsoft extensions logging. To use in the console app the logging functionality and to resolve the dependencies from the services project the simplest way is to use Microsoft extensions logging(asp.net core DI). For simplicity reasons, all the setup from the following examples is done in Program.cs

Configuration in a console application

To use the configuration in console application several configuration providers packages need to be added. I am using the following packages:

  • Microsoft.Extensions.Configuration.CommandLine
  • Microsoft.Extensions.Configuration.EnvironmentVariables
  • Microsoft.Extensions.Configuration.Json

Also configuration needs to be setup but it is pretty easy. Please take a look below:

Dependency injection in a console app

So it seems that is pretty easy to add Ms dependency injection in a console app. Two things need to be done:

  • Add package: Microsoft.Extensions.DependencyInjection
  • Use ServiceCollection to add dependencies and after to return the service provider. Create a method called RegisterServices please take a look below:

Logging in a console app

Again two steps are needed to use logging in a console app. First step is to add the logging providers and the second is to make the setup. For simplicity, I am adding only the console logging provider but any provider can be added like for example the excellent Serilog provider.

  • Add package Microsoft.Extensions.Logging.Console
  • Add inside RegisterServices method that was created for DI the line:
    • serviceCollection.AddLogging(cfg => cfg.AddConsole());

RegisterServices should look now like this:

Putting all together

Add a new file called appsettings.json to test the that json config providers are working fine. In the file add the following:

 {
    "github":{
        "apiUrl":"https://api.github.com/"
    }
} 

In the Main method we setup everything. Main should look like this:

If everything is set up correctly when running the application the following output should be displayed:

info: DI_Configuration_Logging_ConsoleApp.Program[0]
       Github api url is: https://api.github.com/

Github sample repository

All the code used here is available on github at https://github.com/emanuelpaul/DI-Configuration-Logging-ConsoleApp

Share with:


Use HttpClient to call Maigun API in ASP.NET Core

I had to send some emails from an ASP.NET Core 2.2 web application. One option to send emails is to use Mailgun. To send email using Mailgun you need to create a free account. You cand send 10,000 email per month for free.

STMP or HTTP API

For sending SMTP or HTTP can be used. HTTP API has 2 main advantages over SMTP:

  • It’s faster
  • You don’t have to deal with MIME types

HTTP API using C#

So for sending emails, I chose to use the HTTP API. On the Mailgun website, there are examples for sending emails using C#. These examples are using the RestSharp library which is an excellent option but I didn’t want to use another library. I wanted to use HttpClientFactory and HttpClient to call the Mailgun HTTP API. HttpClientFactory already has great integration with DI and the rest of ASP.NET Core.

Sending emails

I created a class to retain the email config.
MailgunEmailSender class is responsible to send emails by calling the Mailgun HTTP API. It takes as parameters in the constructor the HttpClient that will be used to call the API and the email config. It has just one public method called SendMail. SendMail takes 3 parameters: to the email address, email subject, and email body. It calls the Mailgun API using HttpClient that is configured in Startup.cs. Please take a look below at the code:

In Startup.cs the email config is loaded and setup for MailgunEmailSender how to use HttpClient is made.

Github sample repository

All the sample code is available on github at https://github.com/emanuelpaul/asp-net-core-mailgun-httpclient

Share with: