Posts

Configuring Any .NET 9.0 Program to Run in Docker: A Step-by-Step Guide

Docker provides a consistent runtime environment for running .NET applications, enabling seamless development and deployment across various platforms. This guide explains how to configure any .NET program to run in Docker, explores project types compatible with Docker, and provides examples for Web API and Console Applications . Understanding the Docker Configuration in launchSettings.json The launchSettings.json file in .NET projects includes profiles for debugging and running applications. When configured for Docker, a typical profile might look like this: json "Container (Dockerfile)" : { "commandName" : "Docker" , "launchUrl" : "{Scheme}://{ServiceHost}:{ServicePort}" , "environmentVariables" : { "ASPNETCORE_HTTPS_PORTS" : "8081" , "ASPNETCORE_HTTP_PORTS" : "8080" } , "publishAllPorts" : true , "useSSL" : true } Key Propertie...

Understanding a Multi-Stage Dockerfile for .NET 9 Application

Dockerfiles are essential for containerizing applications, allowing developers to package code, dependencies, and runtime environments into lightweight, portable containers. In this article, we’ll explore a multi-stage Dockerfile designed for a .NET Web API project named SampleWebAPI . This Dockerfile employs best practices to create a lightweight and secure container image while maintaining an efficient build process. Dockerfile Overview This Dockerfile is structured into multiple stages, each focusing on a specific phase of the containerization process: Debug Stage : Prepares the runtime environment for debugging. Build Stage : Compiles the application code. Publish Stage : Prepares the application for deployment. Runtime Stage : Creates the final container image for production. Debug Stage dockerfile FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base USER $APP_UID WORKDIR /app EXPOSE 8080 EXPOSE 8081 Base Image : Uses the official ASP.NET Core runtime image ( aspnet:9.0 ) to provide...

ASP.NET Core 9.0 Web API application with authentication, user management, and authorization features.

Here’s a breakdown of the functionality in this class ( Program.cs ), which configures an ASP.NET Core Web API application with authentication, user management, and authorization features. The code is centered around Google OAuth and role-based user management. 1. Services Configuration Minimal asp.net core API Scans and discovers all Minimal API endpoints (routes added via methods like MapGet, MapPost, etc.). csharp builder.Services.AddEndpointsApiExplorer(); Swagger Setup What it does : Adds Swagger support for API documentation. Includes a Bearer token security scheme to secure the API endpoints that require authentication. csharp builder.Services.AddSwaggerGen(c => { c.SwaggerDoc( "v1" , new OpenApiInfo { Title = "Minimal Web API" ,           Version = "v1" }); c.AddSecurityDefinition( "Bearer" , ...); c.AddSecurityRequirement( new OpenApiSecurityRequirement() { ... }); }); Authentication Configuration Default Scheme : Se...

Minimal APIs in ASP.NET Core 9.0

Minimal APIs in ASP.NET Core offer a streamlined approach to building lightweight HTTP services. They are designed for simplicity, performance, and quick development cycles, making them an excellent choice for certain types of projects. Here's a breakdown of their advantages over traditional controllers: Advantages of Minimal APIs 1. Simplicity Less Boilerplate Code : Minimal APIs allow you to define endpoints directly without requiring a Controller class, attributes, or action methods. csharp app.MapGet( "/hello" , () => "Hello, world!" ); Compare this to a traditional controller where you'd need: csharp [ ApiController ] [ Route( "[controller]" ) ] public class HelloController : ControllerBase { [ HttpGet ] public IActionResult Get () => Ok( "Hello, world!" ); } Easier to Read and Write : The code is concise and straightforward, ideal for simple use cases or microservices. 2. Performance Fewer Middleware Layers : M...

Calling a FAKE API for TESTING

Have you ever just wanted to make an http request to an endpoint and get some json data? Well you can do that using this API endpoint. https://jsonplaceholder.typicode.com/todos/1 The API returns  // https://jsonplaceholder.typicode.com/todos/1 {   "userId": 1,   "id": 1,   "title": "delectus aut autem",   "completed": false } Simply make your http call to this api.Here are a few examples. Javascript Example fetch('https://jsonplaceholder.typicode.com/todos/1') .then(response => response.json()) .then(json => console.log(json)) C# Example using var client = new HttpClient(); result = await client.GetAsync("https://jsonplaceholder.typicode.com/todos/1"); Console.WriteLine(result.StatusCode); Python Example import requests r = request.get(url='https://jsonplaceholder.typicode.com/todos/1') print(r.json())

How to Deploy and Test an Arm Template using Azure CLI

To deploy an arm template using the Azure CLI, do the following: New-AzResourceGroupDeployment -Name ValidateDeployment -ResourceGroupName *nameOfResourceGroup*  -TemplateFile *PathToArmTemplate* -TemplateParameterFile *PathToArmTemplateParams* You can also test the deployment before you actually perform the operation. Use the following command to test the arm template deployment: Test-AzResourceGroupDeployment -ResourceGroupName *nameOfResourceGroup*  -TemplateFile *PathToArmTemplate* -TemplateParameterFile *PathToArmTemplateParams* If the command runs successfully, you can check the portal for your resources. Cheers

How to create CDN endpoint using Azure CLI

First connect to your Azure instance az login Then run the following command az cdn endpoint create --resource-group *resourceGroupName* --profile-name *cdnProfileName* --name *endpointName* --origin *originDomain* Once the process completes, you will see a new endpoint in your cdn profile. Cheers