Posts

Showing posts with the label C#

Serilog with AWS Cloudwatch on Ubuntu

Image
Serilog with AWS Cloudwatch on Ubuntu Few weeks ago we saw How to configure Serilog to work with different environment . At the end of the post, we saw briefly how to get the structured logs synced to Cloudwatch. Today we will explore the configuration in more details. Unified Cloudwatch agent Literate and json logs with Serilog Debug the Cloudwatch agent 1. Unified Cloudwatch agent The Unified Cloudwatch agent can be installed by following the official documentation https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/UseCloudWatchUnifiedAgent.html . There is a previous version of the Cloudwatch agent, this new version, introduced in December 2017, unifies the collection of metrics and logs for Cloudwatch under the same configuration. To install the agent, execute the following commands: mkdir ~/tmp cd tmp wget https://s3.amazonaws.com/amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip unzip AmazonCloudWatchAgent.zip sudo install.sh This will install ...

HttpClientFactory in ASP NET Core 2.1

HttpClientFactory in ASP NET Core 2.1 ASP.NET Core 2.1 ships with a factory for HttpClient called HttpclientFactory . This factory allows us to no longer care about the lifecycle of the HttpClient by leaving it to the framework. Today we will see few ways of instantiating clients: Default client Typed client Named client 1. Default client To use the factory, we start first by registering it to the service collection with .AddHttpClient() which is an extension coming from Microsoft.Extensions.Http . public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddHttpClient(); } This gives us access to the IHttpClientFactory which we can inject and using it, we can create a HttpClient . [HttpPost] public async Task<ActionResult<string>> PostDefaultClient([FromServices]IHttpClientFactory factory, [FromBody] ValueDto value) { var client = factory.CreateClient(); client.BaseAddress = new System.Uri("http://loc...

ASP NET Core with Nginx

ASP NET Core with Nginx Few weeks ago I showed how to host ASP NET Core on Windows Server behind IIS. Compared to Windows Server, Ubuntu with nginx offers a quicker way to get started and a better control over the kestrel process. Today we will see how to host an ASP NET Core application on Ubuntu. This post will be composed of three parts: Install nginx Configure nginx Host ASP NET Core 1. Install nginx Start by installing nginx. sudo apt-get update sudo apt-get install nginx After installing nginx, the daemon should have been installed and started. We should be able to navigate to http://localhost and see the nginx default page. This page is the default root folder of nginx which can be found under /var/www/html/ . We should also be able to interact with it just like any other daemon managed by systemd : sudo systemctl start nginx sudo systemctl stop nginx sudo systemctl restart nginx sudo systemctl status nginx And similarly it can be debugged via journald : sudo jo...

Semantic versioning for dotnet application

Image
Semantic versioning for dotnet application Versioning application allows us to know which features are currently available in the environment where we deployed but when our application is composed by multiple webservers, it becomes tedious to maintain the versioning. On top of that with the dotnet core movement, management of versioning has changed. Today I will show a way to automate the versioning using Gitversion and how it can be used for dotnet core and dotnet framework. This post will be composed by 3 parts: Version assemblies Semantic versioning Gitversion 1. Versioning assemblies In dotnet, assemblies are versioned via the AssemblyInfo.cs file. This file contains the metadata used by the compiler to populate the information about the assembly like the title, the author, the copyrights and the version. It is handle via attributes. Here is an example of an assembly info file: using System.Reflection; [assembly: AssemblyTitle("HelloWorld")] [assembly: Assembl...

Microsoft Project Orleans ClientBuilder and SiloBuilder

Microsoft Project Orleans ClientBuilder and SiloBuilder Prior 2.0.0 stable, we used to configure client and silo using ClientConfiguration and ClusterConfiguration . I was hard to understand how to configure those as many options were available. Moving forward to 2.0.0 stable, ClientConfiguration and ClusterConfiguration no longer exist! It has now been replaced by a ClientBuilder and a SiloBuilder (notice there is no cluster builder). The shift toward builders makes life easier to us to configure client and silo. Today I want to take the time to explain how the migration between beta 3 and stable can be done in three parts: Configure ClientBuilder Configure SiloBuilder 1. Configure the ClientBuilder A client needs to connect to a cluster. The only configuration needed for the client is therefore: the id of the cluster the id of the service where to find the cluster During beta this used to be configured in ClientConfiguration , it is now done using the ClientBuilder :...

Hashicorp Vault behind IIS

Image
Hashicorp Vault behind IIS Last week I talked about Hashicorp Vault and how it could be used to store secrets . Today I will continue on the same line and show how we can host Vault behind IIS and use what we learnt in the previous post to retrieve secrets from ASP.NET Core. Setup Vault Read secrets from Vault from ASP.NET Core 1. Setup Vault Vault is a webserver which comes with a complete API. In this example, we will show how to setup Vault and proxy calls from IIS to Vault. 1.1 Boot Vault To begin with, we can follow the same steps described in my previous post - Hashicorp Vault and how it could be used to store secrets . As a quick overview, here are the steps to be executed inside Windows Server: download Vault create the config.hcl file run the command vault.exe server -config=config.hcl In config.hcl, we configured Vault to listen on http://localhost:8200 so the next thing to do is to proxy calls from IIS to Vault process. 1.2 Configure IIS to direct calls to...

Start processes from C# in DotNet Core

Start processes from C# in DotNet Core Being able to run batch during the lifecycle of an application is always useful. It gives a way to programmatically interact with any programs which implements a CLI. Today we will see how we can start processes from C# on .NET Core applications and how it can be useful in a real scenario 1. Unzip from CLI 2. Use CLI from C# 1. Unzip from CLI In this tutorial we will use a process to execute a 7zip command to extract files into a particular input. If we have 7zip installed and added to PATH, using a terminal, we should able to execute the following from the folder containing the archive: 7z x .\\archive.zip -o.\\archive x stands for extract and -o stands for output directory. The result of this command should be the list of files in extract unzipped into the archive folder. There are times where it comes handy to zip and unzip as part of an application lifecycle, for example to allow a user to download multiple files from our server, we...

Microsoft Orleans logs warnings and errors

Microsoft Orleans logs warnings and errors Microsoft Orleans is a framework which helps building distributed system by implementing the actor model together with the concept of virtual actors, taking care of availability and concurrency. If you are unfamiliar with Microsoft Orleans, you can look at my previous blog post explaining the benefits of Microsoft Orleans . Even though Orleans promises to abstract the distributed system problems, there are instances where errors arise without us being able to understand what is going on. Lucky us, the logs are well documented… but only for those who can decrypt them. Today I will go through some of the errors and warnings which can be seen from silo and client so that you too can undestand what is going on. Enjoy! The code used to produce those errors can be found on my GitHub https://github.com/Kimserey/orleans-cluster-consul . 1. Client logs Logs on client appears with address {ip}:0 . 1.1. Can’t find implementation of interface An un...

Validation in ASP NET Core and Angular

Validation in ASP NET Core and Angular Validation is an important part of any application. There are two parts where validation is required, the API level and the frontend. The API validation is meant to prevent any malformed input to corrupt our data while the frontend validation is meant to guide the user to fill in a form by providing interactive feedback on her input. ASP NET Core for the backend and Angular for the frontend both ship with validation mechanisms fulfilling are requirements. Today we will see how we can implement validation in ASP NET Core using data annotation and inline validation with Angular reactive form. This post will be composed by 2 parts: 1. Implement validation for ASP NET Core 2. Implement inline validation for Angular form 1. Implement validation for ASP NET Core In ASP NET Core, validation can be implemented using data annotation. On each call, parameters are tested against the annotation and the ModelState property is filled up. When any of the p...

Swagger for ASP NET Core API development

Image
Swagger for ASP NET Core API development Building a web API is not an easy task. In order to build one easy to use, we need to consider the routes, the HTTP methods, the return results from the endpoints, the parameter used for the body of the requests, etc… Swagger is a tool which compiles all our API endpoints into a friendly GUI and allows us to directly test them. It brings a lot of benefits as we can easily pinpoint mistakes in the endpoints route or parameters and of course in the implementation since we can straight away call the endpoints. Today we will see how we can integrate Swagger in 3 parts: 1. Add Swagger to ASP NET Core project 2. Handle authentication 3. Handle endpoints specificities with filters 1. Add Swagger to ASP NET Core project We start first by creating an empty ASP NET Core project with the following startup: public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Co...

Silo configuration and Cluster management in Microsoft Orleans

Silo configuration and Cluster management in Microsoft Orleans Few weeks ago we saw how to create a Silo and how to implement grains. We also saw how we could create an ASP NET Core web app to talk to the Silo. We discovered the benefits of the single threaded model in grains. Today we will see one of the other major feature of Orleans, cluster management. This post will be composed by 3 parts: 1. Build a silo 2. Form a cluster with multiple silos 3. Cluster management with membership All the source code can be found on my GitHub. https://github.com/Kimserey/orleans-sample 1. Build a silo Let’s start first by implementing a Silo. We will be using the example we used in the past post. public class Program { public static void Main(string[] args) { var silo = new SiloHost("main"); silo.InitializeOrleansSilo(); var success = silo.StartOrleansSilo(); if (!success) { throw new Exception("Failed to start silo...