Posted on Leave a comment

Containerize a .NET app with Docker and vs code

When you build your application with cloud native technologies you will build microservices on containers instead of monolithic applications. We will now examine how easy is to build a .NET application in a container and run this application on your local machine.

First we will need to create the visual studio solution. I will go through that with visual studio IDE and then I will use vs code. For my microservice I am using a ASP .NET Core web api with default code.

The target framework for the solution will be the latest .NET framework which is version 7. All other settings will be set to defaults.

When you run the app locally with IIsExpress you will be able to access the swagger interface through the port which you defined in the launchSettings.json. 

https://localhost:7057/swagger/index.html

This file can be located under Properties and there you can configure on which port the application will run. In the profiles section under https settings, you can find the default application URL and port. This will be needed in later steps.

Microsoft provides the below documentation in order to create a containerized application that runs on .NET

Build and run an ASP.NET Core app in a container
In this guide you will learn how to: Create a Dockerfile file describing a simple .NET Core service container. Build…code.visualstudio.com

In order to create a microservice based on our vs solution we will need a dockerfile. This can be created automatically with vs code.

In vs code command dialog search for docker add and select docker compose files to workspace.

Then select asp net core.

and after that your operating system. The next step will be to select the exposed port, or otherwise under which port your application will run. There we should provide the port that we found under our launchSettings.json or the one that we configured manually. In my case I will select the default one for the solution which was 7057.

When a popup window appears on the screen you should select add Dockerfile and automatically the build files will be generated.

Dockerfile

Based on my setup I altered two things in the generated Dockerfile. The first thing will be to change configuration to Debug instead of Release. For production environments you will consider using the release build directive. The second thing will be to add an environmental variable ASPNETCORE_ENVIRONMENT inside the container with the value Development.

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 7057

ENV ASPNETCORE_URLS=http://*:7057
ENV ASPNETCORE_ENVIRONMENT=Development

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["AspNetWebApi.csproj", "./"]
RUN dotnet restore "AspNetWebApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "AspNetWebApi.csproj" -c Debug -o /app/build

FROM build AS publish
RUN dotnet publish "AspNetWebApi.csproj" -c Debug -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AspNetWebApi.dll"]

docker build command

after the build is completed and the image is created you can run a new container locally.

Keep in mind that in order to test your container you should create a port forward from the container to your host. I used the same port for the host so I added the -p 7057:7057

The logs of the container indicate a successful run of the application.

Our application now runs as a microservice container inside the host machine (my laptop). 

We can verify the access to our application using the URL with the swagger.

Youtube video:

Posted on Leave a comment

Error MSB3073 The command “npm run build” exited with code 1 – Visual Studio

Recently I was trying to build a complex project which included also node js code and was deployable to IIS. The error I faced on visual studio during the build was the below.

"npm run build" exited with code 1 - Visual Studio 
"npm install" exited with code 1 - Visual Studio

both of which were misleading.

In order to locate the error I navigated on the Node project and tried to build using

npm run build

Command line never lies. I was able to determine that webpack was not installed.

In order to resolve I executed:

npm install -g webpack
npm install -g webpack-cli
Posted on Leave a comment

Extend available properties of User.Identity – Asp.Net [web api]

Asp.net web api by default contains some pre configured fields that can handle the registration of a user in a web app. Some of them are Email, Password, Confirm Password and others. You can extend those properties and include your own for your purposes with the following procedure.

First of all you need to execute some commands in the package manager. You can find nuget package manager in Tools -> Nuget Package Manager -> PM Console 

The first thing to do is to enable Migrations

Enable-Migrations

The you can go to Models\AccountBindingModels.cs and add your property to the class RegisterBindingModel. Also add the same property in the Models\IdentityModels.cs inside ApplicationUser class. For example lets assume you want to add a username in the registration proccess. For this purpose you can use the following line

 public string AppUserName { get; set; }

Also include in the file.

using System;

The you must execute:

Add-Migration "AppUserName"
Update-Database

Those commands will run all the migration files and update the database schema to add a AppUserName column in your database.

Then update the registration action in Controllers\AccountController.cs to store AppUserName as well.

var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, AppUserName = model.AppUserName };

It was that easy. you can finally find AppUserName in your database.

Posted on Leave a comment

Application Insights VS2015 failed to add applicationinsights.config

If you ever deal with the problem

nuget package install  failed to add applicationinsights.config

in Visual studio 2015 and Windows 10 you only have to create a file and name it  ApplicationInsights.config with the code below. That’s because vs and nuget cannot create this file in the root folder of your project.

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
  <InstrumentationKey>xxxx</InstrumentationKey>
</ApplicationInsights>

You must enter your specific InstrumentationKey for the application that you have registered through Azure portal for Insights.

The trick is that you must select Build Action as Content and Copy always to output folder

vs2015insights