HttpClientFactory in ASP NET Core 2.1

HttpClientFactory in ASP NET Core 2.1ASP.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 clientTyped clientNamed client1. Default clientTo 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://localhost:5100"); va…

A complete SignalR with ASP Net Core example with WSS, Authentication, Nginx

SignalR with ASP Net CoreSignalR is a framework from ASP NET Core allowing us to establish a two way communication between client and server. This two way communication allows the client to send messages to the server but more importantly allows the server to push messages to the client.
SignalR makes use of Websocket when available else it falls back to SSE or pulling. Today we will focus on how to setup SignalR to work with WSS, Websocket secure and how we can authenticate the user requesting to connect to our SignalR hub via Webscoket.Getting started with SignalRSSL encryption for Websocket Secure WSSWebsocket Authentication with Identity Server 4SignalR behind Nginx1. Getting started with SignalRThe Hubs are the main components of SignalR. It is an abstraction of a two way communication available for both client and server. Public functions from the hub can be called from the server code and can be called from the client. The frontend NPM package @aspnet/signalr library makes the …

Verify dotnet SDK and runtime version installed

Verify dotnet SDK and runtime version installedTo 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: 59524873d6 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: 6985b9f684 .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.All] Microsoft.AspNetCore.All 2.1.1 [C:\…

Remove redirection of localhost to HTTPS

Remove redirection of localhost to HTTPSSymptomsChrome redirects http://localhost to https://localhostOn guest mode, http://localhost is accessibleCurl can retrieve http://localhostCauseOne of my test project was configuring SSL on nginx.
The configuration was redirecting localhost to https for testing purposes and Chrome had since then cached the redirection.
Subsequent calls were no longer hitting nginx as they were only hitting Chrome cache.FixOpen the Web Developer Console on Chrome, CTRL+SHIFT+IRight click on the reload arrowSelect Empty Cache And Hard ReloadThis will remove all cached items including the redirection. http://localhost will no longer be redirected by Chrome.

Self Signed Certificate for Identity Server 4 and SSL in Ubuntu 16.04 server

Self Signed Certificate for Identity Server 4 and SSL in Ubuntu 16.04 serverTo sign our JWT tokens, Identity Server 4 requires a signing credential. Today we will see how we can create our own key and provide it to Identity Server to be used as signing credential.Configure ASP NET CoreCreate key with opensslSelfsigned certificate for local SSL usageIf you are new to Identity Server, you can have a look at my previous blog post on How to configure a Implicit authentication with Identity Server.1. Configure ASP NET CoreWe start first by creating an extension on top of the IIdentityServerBuilder which when the key is available, will load our own key using the .AddSigningCredential() else will create a temporary key for development purposes, .AddDeveloperSigningCredential().public static class IdentityServerBuilderExtensions { public static IIdentityServerBuilder LoadSigningCredentialFrom(this IIdentityServerBuilder builder, string path) { if (!string.IsNullOrEmptyString(…

Multi environment logging with Serilog for AspNet Core

Multi environment logging with Serilog for AspNet CoreFew 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 SerilogMulti environmentAWS CloudWatch1. Setup SerilogIf 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) .UseSerilog((builder, cfg)…

Setup a CI/CD pipeline with Gitlab for ASPNET Core on Ubuntu

Setup a CI/CD pipeline with Gitlab for ASPNET Core on UbuntuFew weeks ago I explained how we could setup a CI/CD pipeline whereby the runner would be on Windows and the last stage was to package the application.
Today we will see how we can setup a runner on Ubuntu CI server and use it to build and deploy an ASP MET Core application onto a Ubuntu 16.04 server. This post will be composed by three parts:Setup the runner on the CI serverSetup the application on the serverSetup the job in our projectIf you are unfamiliar with Gitlab pipeline and its terminology, you can read my previous post where I explain the main concepts behind GitLab pipeline with runner, jobs and stages.
If you are unfamiliar with ssh and systemd, you can read my previous blog post on useful ssh commands and my previous blog post on how to manage Kestrel process with systemd.1. Setup the runner on the CI serverSetup the runner on your CI server by getting the package with apt-get.curl -L…