In today’s post, I’d like to present a dozen of minimalistic samples that you can make use of within ASP.NET Core application. Starting from simple things like options, through middleware, databases and even Nginx or Docker. These samples are part of the upcoming event “Thursday with .NET” that I’ll be part of on Thursday 20.04.2017.
Make sure you execute dotnet restore first and then dotnet run to start the particular application. If the example is using an external resource like a database, ensure that you have it installed and running before starting the app.
Let’s warm up with the options provider. You can easily create a so-called XyzOptions class and bind it to the configuration defined in the appsettings.json file and use it in your application simply by injecting IOptions
You can extend the HTTP request pipeline by adding your own middleware into the overall flow. If you ever used a framework like NodeJS and wanted to have the same possibility of providing your own code in order to e.g. validate or process the incoming request, you can do it in ASP.NET Core as well.
In need of the custom exception handler? Need to log the incoming requests or validate them? These and much more can be achieved by using filters, simply by creating a new attribute and using them on top of your MVC controllers.
Dependency Injection and IoC container are built-in to the framework, however, you can still use your favorite libraries like Autofac if you feel like you need something more powerful in terms of dependency inversion principle.
We are all aware how to write the good unit tests, correct? But what about the integration (end-to-end) tests? For sure, you can expose the running instance of your API and perform the HTTP requests e.g. via HttpClient instance. Yet, there’s a better way and you can do such tests in memory thanks to TestHost library.
6. SQL Server
Did you know that you can run SQL Server on Linux? Anyway, you can connect to the SQL Server instance via .NET Core e.g. by using Entity Framework Core library, however, I do prefer a more lightweight solution, thus the provided example makes use of the Dapper.
Do you like NoSQL databases like I do? You can use the MongoDB Driver and connect to the MongoDB databases from the .NET Core applications.
Powerful caching can be surely done by Redis. And you can connect to the Redis server thanks to the developers from StackExchange who did create such a great library.
Using service bus is one of the most common ways when it comes to creating a distributed system. RabbitMQ is one of those and you can make use of it either by adding the official library or the RawRabbit which I like due to its abstractions and ease of use.
Nancy is an amazing framework for building the HTTP API. I like it very much and I was very happy when I found out that it’s compatible with the ASP.NET Core framework.
Containerized applications and containers, in general, are industry standard nowadays. You can easily make use of the Docker and build your ASP.NET Core applications on top of that.
As mentioned above, all of the examples can be found and downloaded from the GitHub.