C#, Docker

Running an ASP.NET Core application targeting .NET Framework in Docker

2 min read

Recently, I’ve came across an interesting challenge that was to dockerize an ASP.NET Core 2.2 application targeting .NET Framework 4.7.2. The reason this application was targeting .NET Framework was because it was using a library that unfortunately had no plans to move to the .NET Core platform. That library was a port from Java. As such I had to take a decision: rewrite the whole application in Java to support this library more natively, or try to find an alternative library that did the same thing. Sometimes it’s better to sleep on such decision. Well it paid off. I had forgotten that possibly I could use mono to run it.

I took this as a challenge and used one of my favorite tools to get cracking: Docker

Dockerizing the application

I started writing my Dockerfile and used the dotnet core SDK 2.2.401 as base image.

Since this image is based on Debian 9, we easily have access to the apt package manager tool. I proceeded to install mono devel (mono development) as per the documentation on the mono project. This is enough to compile the code. The latest mono version that is available is 6.4.0.198 (as of the writing of this post) so that’s the one I used.

I then proceeded to add some regular commands, in my Dockerfile, to build my ASP.NET Core application:

Hold and behold, when it was trying to build the application, I got the following error:

error MSB3644: The reference assemblies for framework “.NETFramework,Version=v4.5” were not found. To resolve this, install the SDK or Targeting Pack …

The key here is to tell the compiler where the Framework assemblies are. For that you need to override FrameworkPathOverride that is used to resolved the assemblies location. I proceeded to add the following to my project file (csproj). Note that the override is only triggered when the application targets a Unix platform and .NET Framework 4 and up

I reran my compilation. I got another error saying it couldn’t find base references to stuff like Object, etc.

The project was missing reference to the netstandard library. I again proceeded to add the reference to the netstandard library in my project file.

Success! The project compiled successfully.

Running the application

To run the application, I targeted the same version of mono that I compiled my application with.

The entry point uses mono. I also passed a few parameters to mono that can be seen in the mono man pages:

Here is the final Dockerfile

Considerations

While compiling your application you may encounter warnings such as that some libraries, such as System.Net.Http, conflicts could not be automatically resolved. Even if .NET Framework 4.7.2 is netstandard compatible, there as still some quirks that make it so that sometimes the bindings of those libraries cannot be automatically resolved.

This doesn’t mean that your application will not work. It just means that one DLL got the best of the other in terms of version (netstandard base lib vs .net framework or vice version!) and it may not be there version you’re expecting.

Make sure to thoroughly test your application to make sure there aren’t any problems with it.