Full service digital agency.

Wij ontwikkelen digital experiences die een glimlach op je gezicht toveren

Leer ons kennen

NL slash EN

Console applications in .Net Core

.Net Core 1.0 and ASP.NET Core 1.0 have been released more than three months ago and a lot has been written/spoken on how to do stuff with ASP.NET Core. But what about .NET Core Console Applications? Don’t they deserve the same kind of love? I believe they do.

Although most of .Net developers focus on ASP.NET, UWP or Xamarin, there are still a lot of developers who use/implement console applications to be used as scheduled task or as simple applications that are needed now and then to do stuff.

I felt the “lack of love” for .NET Core console applications when about three months ago I was assigned the task of implementing Dept’s gravatar server. One of the requirements of the “gravatar server” was to have a simple console app to download images from the Dept’s own Workplace (back then it was still in beta phase and was known by the name of Facebook @ Work) every night. These images would then be served by a web application. As geeks and technology addicts as we are at Dept, we felt the need to implement all this in ASP.NET Core and .NET Core.

While ASP.NET Core has built-in support for dependency injection, logging and environment configurations, you don’t have this by default on regular .NET Core console apps. Of course you can live and work without these features but I felt this was not the way I wanted to implement the console app and I went on searching for documentation, examples and/or tips on how to have these features on console apps. After a few hours of searching for different terms/keywords, most of the results that I got referred to ASP.NET core RC versions or to ASP.NET 5 (the pre ASP.NET core RC1 name). Luckily, after a few hours I came across this StackOverflow question and answer that saved my life! I have to give credit to the user that replied to that question , it helped me a lot!

How I did it? continue reading to find out!

Environment detection and configurations

ASP.NET Core as an in built way to determine the hosting environment (through HostingEnvironmentExtensions). In regular .NET Core there is no such thing. To overcome this, I implemented a simple string compare (like they do it in ASP.NET Core) and I’m loading the environment name either from an environment variable, a command line argument or lastly as a fallback from an hard coded variable.

Loading configuration

Just like in ASP.NET Core, you will have to load your configuration files and build the configuration yourself. (notice in the code below that I’m using my own environment variable to load the correct environment appsettings.json).

Configuring dependency injection

In ASP.NET Core, an IServiceCollection is injected into the ConfigureServices method and from there on, you just need to add the dependencies you‘ll need. This is not the case in .NET Core and therefore you have to instantiate your own ServiceCollection and add your dependencies to the collection.

Adding logging

Once more, unlike ASP.NET Core, logging is not in built in regular .NET Core and therefore you have to add it yourself by instantiating a LoggerFactory and then injecting it into the services collection.

If you are curious to see what we have done, you can always go to Dept’s github pageand check the whole gravatar solution. The console app itself can be found here. I tried to comment the code as much as possible but if you feel like there are things that could be improved either on code or code commenting please let me know.

Hope this post, its links and examples inspire and help you with you .NET Core console apps!

Happy coding!


Miguel Pinto, Developer

miguel@tamtam.nl

+31(6) 1588 0269

Connect