Search This Blog

Run native .NET application in Docker (.NET Framework 4.6.2)

Scope
The main scope of this post is to see how we can run a legacy application written in .NET Framework in Docker.

Context
First of all, let’s define what is a legacy application in our context. By a legacy application we understand an application that runs .NET Framework 3.5 or higher in a production environment where we don’t have any more the people or documentation that would help us to understand what is happening behind the scene.
In this scenarios, you might want to migrate the current solution from a standard environment to Docker. There are many advantages for such a migration, like:

Continuous Deployment

Testing

Isolation

Security at container level

Versioning Control

Environment Standardization

Until now, we didn’t had the possibility to run a .NET application in Docker. With .NET Core, there was support for .NET Core in Docker, but migration from a full .NET framework to .NET Core can be costly and even impossible. Not only because of lack of features, but also because once you modify an existing application phases like V&V (Verification and Validation) can be time consuming.

Docker support on Windows Server 2016
Now, with Windows Server 2016 and native support for containers, things started to change. On Windows Server 2016, we can have a console application that runs native in Docker.
The most important thing at this level is that we don’t need to modify our existing code to run in Docker. Based on what kind of resources we use we might need to map different folders, endpoints or install on the Docker image some applications and services. This are the same steps that we do on a classical system – but in this case we need to configure Docker and not a machine.

Initial Use Case
Let’s take as example a consoler application that is written in .NET 4.6.1. We need to be able to take this application and run in in Docker, without changing a line of code in our console application.

In the above example we have Legacy.Code assembly that runs a custom code in StockCheck class. Legacy.Runner is our console application that calls StockCheck. Nothing special or exotic.

Docker Setup
To be able to do this migration we will need Windows Server 2016. For PoC and playground, I recommend to create an instance of Windows Server 2016 on Azure. You will be able to find an image with Containers.
Once you have a machine with Windows Server 2016, you will need to install and setup Docker. There is a great article that describes all this steps that needs to be done on MSDN - https://msdn.microsoft.com/en-us/virtualization/windowscontainers/quick_start/quick_start_windows_server.
You should continue only when you manage to run with success the Microsoft/sample-dotnet image, presented in the above link.

Docker Image for .NET Framework
In Docker Hub, we can find numerous images created by Microsoft, even for .NET Framework (4.6.2, 3.5, ASP.NET and so on). The one that we need for this demo is.NET Framework 4.6.2 - https://hub.docker.com/r/microsoft/dotnet-framework/.
To pull the right image from Docker we need to run the following command:docker pull microsoft/dotnet-framework
Now, we have the image on our system and we can play with it. If we want we can run we can do this from Power Shell using the following command:docker run microsoft/dotnet-framework
As we can see in below picture, we run the image. Nothing happen because in this moment we don’t have nothings deployed.

Create a custom image with our code
On GitHub, I shared the base project, that you can use to run this demo - https://github.com/vunvulear/Stuff/tree/master/Azure/RunLegacyDotNetFrameworkUsingDocker. Once you downloaded it, you will need to build the project. To be able to build it with success you will need also Docker Tools for Visual Studio.
At solution folder, you can find Dockerfile.txt. This contains the commands and actions that needs to be executed by Docker to build the image. The first line specifies what image shall be used to create a new image (FROM). The second like specifies to Docker to copy the output of the build to root folder of the image (COPY). The last line specifies the entry point of our image.
Do be able to work you will need to run the below image from the same location where the .sln project is. Otherwise, you need to modify the COPY action to use a valid path from where our application is copied.docker build -f Dockerfile.txt -t legacy .
Because we added .txt prefix to Dockerfile, we need to specify the input instruction file. ‘-t’ adds a custom tag to our new image. This tag will be used by us later on to run this image. The above commands creates a new image, adding our code to it.

Run our custom image
Now, we have our image created and we are ready to run our new image for the first time.docker run legacy
Congratulation, you have a legacy application that runs in Docker, using .NET Framework 4.6.2

Conclusion
For developers it is the perfect moment when we need to look at containers and Dockers. Even if we work with systems that are production and use .NET Framework <=4.6.2, we can still use Docker with success. Based on our needs we might be able to migrate them to containers pretty easily.

Get link

Facebook

Twitter

Pinterest

Google+

Email

Get link

Facebook

Twitter

Pinterest

Google+

Email

Comments

Post a Comment

Popular posts from this blog

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) {
$httpProvider.interceptors.push('httpInterceptorService…

Today blog post will be started with the following error when running DB tests on the CI machine:threw exception:
System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName)
This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration: