Posts

Showing posts with the label ASP.NET Core

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

Docker compose an ASP NET Core application with SQL Server

Image
Docker compose an ASP NET Core application with SQL Server Last week we saw how we could install and run an ASP NET Core application in a container , we saw how Visual Studio uses docker-compose to setup our services. Today we will see how we can use compose to setup a cluster composed by an ASP NET Core application container with a SQL Server container and how we can place in a third party process. This post will be composed by three parts: Setup SQL Server container on its own Setup an ASP NET Core application with SQL Server Setup a Flyway as migration tool 1. Setup SQL Server container on its own If you haven’t installed Docker, follow my previous blog post . The SQL Server Docker image can be downloaded from mcr.microsoft.com/mssql/server:2017-latest . So first we can start by running the container: docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyPassword001" -p 1433:1433 --name sqlserver-test -d mcr.microsoft.com/mssql/server:2017-latest -e spec...

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

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

Inspect proxied requests from Nginx to Kestrel with Mitmproxy

Image
Inspect proxied requests from Nginx to Kestrel with Mitmproxy In previous blog posts, we saw how to proxy requests to an ASP NET Core application using Nginx . We saw that request headers also can be proxied with proxy_set_header In order to ease development, we need to be able to debug the values to verify that they are what we expect. Today we will see two methods to inspect the proxied requests. This post will be composed by two parts: Nginx location routing Nginx variable debugging Nginx proxied request debug with Mitmproxy 1 Nginx location routing Considering the following configuration of our server: server { listen 80; location / { proxy_pass http://localhost:5000; } location /api/ { proxy_pass http://localhost:5001/; } } If we want to check whether our location routes are properly configured, we can short circuit the proxy_pass with return and use curl to check whether the location is properly selected. server { listen ...

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

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

SignalR with ASP Net Core SignalR 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 SignalR SSL encryption for Websocket Secure WSS Websocket Authentication with Identity Server 4 SignalR behind Nginx 1. Getting started with SignalR The 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 ...