Posts

Showing posts with the label dotnet

Setup a Jenkins Pipeline for local development environment in Docker container

Image
Setup a Jenkins Pipeline for local development environment in Docker container CI/CD pipelines allow us to automatically build, test and deploy code changes. With Jenkins pipeline , the pipeline itself is generated from a file called the Jenkinsfile which, usually, is source controlled together with the source code repository. When we need to push new changes, what we would usually do, is test locally and then commit to the repository. From there, the Jenkins pipeline will trigger and build, test on the integration server and deploy to a testable environment (DEV/QA). But what do we do when the changes that we are making are on the Jenkinsfile itself? How do we test locally the validity of the Jenkinsfile or more simply, how do we try on a sandbox a Jenkins pipeline to learn how to write a Jenkinsfile? Today we will see how we can setup a sandbox with a full CI/CD deployment which can be quickly brought up and teared down for testing. Jenkins server via docker Jenkins pipeline Sim...

Deploy ASP NET Core application on Docker Linux container

Image
Deploy ASP NET Core application on Docker Linux container from Windows Few weeks ago we saw how we could run ASP NET Core application on Ubuntu . This proving that a .NET Core Application can run on a Linux system, today we will be taking it a step further and see how we can deploy our application in a Docker Linux container. This post will be composed by three parts: Install Docker on Windows Docker basic commands Create ASP NET Core application 1. Install Docker on Windows The first step is to go to the official site, sign up and download Docker CE . Once downloaded, install Docker. After being installed you should be able to right click on the icon > Settings on the Docker notification icon and see that Docker is running by checking the status at the bottom left, it should say Docker is running . Open PowerShell and type docker run hello-world . $ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world...

Versioning for open source library

Versioning for open source library Few weeks ago I explained how Gitversion can be used to apply semantic versioning to projects based on commit and tag history. Today I will dive deeper in the purpose of versioning and introduce a flow which can be followed to version open source project libraries by looking into four important parts in the lifecycle of an open source library. Version with Semantic versioning Branching strategy and Commits Continuous Integration and Releases 1. Version with Semantic versioning Semantic versioning is ideal for versioning libraries. It is formed by three numbers {major}.{minor}.{patch} . Each number is used to indicate to the user the level of safety for upgrading the library. Upgrade of the major is risky and has chances to contain breaking changes, therefore looking at release notes or looking for migration would be recommended. Upgrade of the minor has lesser risk and can be used to show availability of new features. Upgrade of the pat...

Implement timeout and retry policies for HttpClient in ASP NET Core with Polly

Implement timeout and retry policies for HttpClient in ASP NET Core with Polly Few weeks ago I explained [how to use the new HttpClientFactory . This freed ourselves from managing the confusing lifecycle of a HttpClient and at the same time allowed us to setup commmon options like base address for all HttpClient injections in our classes. Today we will see how we can setup timeout and retry policies for the HttpClient in in ASP NET Core using Polly . Setup a simple service using HttpClient Install Polly extensions for ASP NET Core Setup Polly policies 1. Setup a simple service using HttpClient In the previous blog post we saw how we could setup a HttpClient and register it to the service collection. Here is a simple service client which will be injected throughout the application: public interface IMyApiClient { Task<HttpResponseMessage> Get(); } And here is the implementation: public class MyApiClient : IMyApiClient { private readonly HttpClient _client; ...

SDK-Style project and project.assets.json

SDK-Style project and project.assets.json Last week I encountered an issue with MSBuild while trying to run it from command line. The issue did not appear when using VisualStudio right click + build but only appeared when using msbuild.exe CLI directly with a clean project. Assets file 'C:\[...]\obj\project.assets.json' not found. Run a NuGet package restore to generate this file. When I first saw the error, few questions came to my mind which I will share today in 3 points: Overview of project.assets.json Slim SDK-Style project Mixing SDK-Style project and old projects Special shoutout to @enricosada who provided me with all the answers regarding the SDK-Style project. 1. Overview of project.assets.json project.assets.json lists all the dependencies of the project. It is created in the /obj folder when using dotnet restore or dotnet build as it implicitly calls restore before build, or msbuid.exe /t:restore with msbuild CLI. To simulate dotnet build (re...

SignalR Core with Angular

Image
SignalR Core with Angular Last week we saw how to Configure SignalR and get a server notifying a client built as Razor page via Websockets . We completed the post by having a fully functional backend setup with SignalR and authentication done via Resource Owner Password. Today we will see how we can connect to SignalR hub from an Angular application and demonstrate how we can authenticate in five parts: SignalR server Setup an Angular application Connect to SignalR hub Send messages Authentication 1. SignalR server We won’t be describing the server here, instead we will take from where we left in my previous blog post with the code fully available on my Github https://github.com/Kimserey/signalr-core-sample/tree/master/Example . Get the repository and run signalr-core-sample/Example . It will run a server on http://localhost:5000 with a SignalR hub on /chathub and Identity server configured with a client my-app setup with Resource owner password flow. 2. Setup an Angul...

Manage configurations with ASP NET Core on Ubuntu

Manage configurations with ASP NET Core on Ubuntu Managing configurations can be challenging. We cannot simply check-in in our repository secrets and connection strings and at the same time we want an easy way to maintain them. Today we will see how we can manage secrets is am easy way on Ubuntu with systemd. Make secrets available on server with systemd Manage secrets locally with UserSecrets on ASP NET Core Manage UserSecrets for dotnet Console Application Goal We need to keep secrets out of the source code. Therefore we want to have our application get secrets locally for local testing and we want the application to get them in our hosted environment. In order to achieve that we will use systemd override configuration to hold configuration of secrets on our server and in our local machine we will use UserSecrets which holds configurations in the user app folder. Take note that UserSecrets file is not encrypted. The only protection we get is the OS user protection. If you...

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...

Verify dotnet SDK and runtime version installed

Verify dotnet SDK and runtime version installed To check your dotnet version installed, use dotnet --info . This command will display the SDKs and runtimes installed on your system together with the path where they can be found. For example on my Windows 10 development machine, dotnet --info will yield the following: > dotnet --info .NET Core SDK (reflecting any global.json): Version: 2.1 . 301 Commit: 59524873 d6 Runtime Environment: OS Name: Windows OS Version: 10.0 . 17134 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\ 2.1 . 301 \ Host (useful for support): Version: 2.1 . 1 Commit: 6985 b9f684 .NET Core SDKs installed: 2.1 . 4 [C:\Program Files\dotnet\sdk] 2.1 . 201 [C:\Program Files\dotnet\sdk] 2.1 . 300 [C:\Program Files\dotnet\sdk] 2.1 . 301 [C:\Program Files\dotnet\sdk] .NET Core runtimes installed: Microsoft.AspNetCore.All 2.1 . 0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.A...

Multi environment logging with Serilog for AspNet Core

Multi environment logging with Serilog for AspNet Core Few months ago we saw how to get started with Serilog. We discovered what was Serilog and the different concepts of settings, enrichers and sinks. Today we will see how we can take Serilog further and configure a logging mechanism for an application running in multi environment. Setup Serilog Multi environment AWS CloudWatch 1. Setup Serilog If you have never seen Serilog before you can start with my previous post on How to get started with Serilog . In order to configure our first logging mechanism, we start by creating an AspNet Core application and install Serilog , Serilog.AspNetCore and the sinks we will be using Serilog.Sinks.Console , Serilog.Sinks.File . RollingFile sink also exists but it has been superseded with File sink. Next we can configure the logger to write to Console and write to a file: public static IWebHost BuildWebHost(string[] args) { return WebHost.CreateDefaultBuilder(args) ...