In my last blog article, I talked about hosting .NET web apps on a Linux server. It takes a little bit of setup and deployment can be a bit more involved than it would be on a Windows server, but it’s a small learning curve that could save you a significant sum of money in hosting costs.
If you’re going to host your apps on Linux, you may also want to develop them in a Linux environment. That’s easy enough to do with a Linux based .NET IDE like Rider or MonoDevelop. But what if you have source code that has been written and maintained in Visual Studio that you would like to port to a Linux environment? In this article I’m going to walk through my experience of porting source code from Visual Studio in Windows to MonoDevelop in Linux.
I had an MVC web app with individual authentication that targeted .NET Framework 4.5. The very first and most obvious step was to just copy the entire solution over to the Linux machine then open it in MonoDevelop and try to compile it. The first error was related to the compiler settings in the web.config file. There are two entries in the “compilers” section of the web.config file that need to be changed.
Replace these lines:
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
With these:
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<providerOption name="CompilerVersion" value="v4.0" />
<providerOption name="WarnAsError" value="false" />
</compiler>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type ="Microsoft.VisualBasic.VBCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<providerOption name="CompilerVersion" value="v4.0" />
<providerOption name="OptionInfer" value="true" />
<providerOption name="WarnAsError" value="false" />
</compiler>
The second error was related to Mono being unable to write debug information. The more recent projects I have ported did not have this problem, but it is an error that I saw with some early projects.
Removing this line from the .csproj file fixed that issue for me:
<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />
Unfortunately, not everything in the .NET framework is supported under Mono. When I ported most of my apps to Linux for example, OWIN authentication was not yet supported. Luckily for me I wasn’t using OWIN so it was not an issue, but your mileage may vary depending on the libraries referenced in your project. References to 3rd party assemblies will almost certainly need to be edited to reflect the new location on the Linux machine. And since Mono is not fully compatible with .NET, those assemblies may not work at all. Be patient, and be prepared to do some research.
If you need to add .NET assembly references to your project, you will find them in:
/app/lib/mono/<version>-api/
The other major hurdle I faced was the database. All of my applications used MSSQL to store data which needed to be converted to MySql. There are some workarounds to get Entity Framework to work pretty nicely with MySql, but there are several bugs that need to be addressed if you want to use a CodeFirst approach. Today there is a much simpler solution that doesn’t require any kind of conversion: you can just install MSSQL server 2017 on Linux and move your database straight over from your Windows machine! This will greatly simplify porting apps in the future.
The current trend of open source frameworks and cross-platform support means we are no longer restricted to a specific operating system for hosting nor for development. These are exciting times for developers, experiment and learn something new!
Please share your experiences with porting traditional .NET applications in the comments section below…
Happy Coding,
Dave Koons
Elegant Software Solutions