El Reg sits down to code with .NET for Linux and MySQL, hitting some bumps along the way

Hands on Microsoft loves Linux, but encoding and deploying an ASP.NET Core application for the open source operating system comes with a bit of friction compared to using Windows.

Microsoft announced .NET Core in 2014, an open source and cross-platform fork of the .NET Framework for Windows only. Two years later, it reached version 1.0 and today the current version is version 2.2, along with ASP.NET Core 2.2 and Entity Framework 2.2.

Still in preparation is .NET Core 3.0, now "very close to being a complete feature," according to program manager Richard Lander, and the final release is expected by the end of this year. Next year we should see .NET 5.0, an important version that Microsoft expects to unify the .NET platform.

This means that we now have three years of .NET Core ready for production, indicating a reasonable level of maturity. Multiplatform is a key feature, and there are obvious attractions for deployment in Linux instead of Windows, including options for unlicensed servers.

But, how easy is it to start Visual Studio 2019 and then compile and implement a Web application to linux? It is the kind of thing you might want to do for a rapid prototype, with the benefit of the ubiquitous Linux hosting of numerous providers.

  Framework options for an ASP.NET Core application

Framework options for an ASP.NET Core application (click to enlarge)

Visual Studio offers a variety of frameworks for ASP.NET Core web applications, depending on which components of the expanding Microsoft IDE you have chosen to install. These include the old ASP.NET MVC (but not even older web forms, which are unique to the .NET Framework), as well as Angular, React.js or React.js with Redux (a library that stores and updates the status) .

There are a couple of points to consider. One is that if you use one of the Reaction options, you are creating a dependency on Node.js. Also, while these are fine frames, there is not much help from the official Microsoft documentation on how to use them. Of course, there is a lot of help from the React community, but you will find that not many of them use Visual Studio. In fact, it is better that you use the Visual Studio code, which is used more widely.

The path of least resistance, then, is an ASP.NET MVC application. As ASP.NET MVC is a Microsoft invention, it is much better documented for Visual Studio users.

Another point, however: if you check one of the authentication options, Visual Studio will generate some code useful for user administration, and your application is required to need user administration.

Less obvious is that if you choose "Individual user accounts", which means a dedicated database for use by the application, the application will be configured for SQL Server. It is not a problem, perhaps, if you intend to implement it in Azure, but it is less than ideal if you have a cheap Linux host in mind. Something like MySQL or SQLite is a more convenient option. You will have to modify the code to use a different database administrator.

Microsoft Visual Studio templates push it strongly toward Entity Framework Core (EF Core), the cross-platform version of the company's ORM (Relational Object Mapping) layer. If you are coding a data intensive application where database performance is critical, it may not be the best option. On the other hand, it is convenient for many use cases that use small databases. It is a good option for a prototype.

If you choose Sqlite, there is a useful NuGet package called Microsoft.EntityFrameworkCore.Sqlite. MySQL, on the other hand, is more problematic. The official Oracle provider is described in this table and you will notice that the latest version of the Oracle connector is only compatible with EF Core 1.1. There are other options, such as Pomelo.EntityFrameworkCore.MySql, which has 2.47 million reassuring downloads.

  The Grapefruit library for MySql is more widely compatible than the Oracle offer

The Grapefruit library for MySql is more compatible than the Oracle offer

In this case, we go the MySQL path . In Windows 10, you can install the version of MySQL for Windows or use the Windows Subsystem for Linux and run it there. Either way, create a database, create a user with rights to that database and then you can add a connection string to the ASP.NET application. Add the connection string in appsettings.json . You can then go to Startup.cs and modify services.AddDbContext with options.UseMySql and the name of your new connection string. The option UseMySql appears magically after installing the Pomelo package.

There is a small problem, which may be that EF Core has generated Migrations for its user database. Migrations are used in EF Core to create and then update the structure of the database. You can delete existing migrations, which are specific to SQL Server. Then open the package manager and enter:

Add-migration InitialCreate

Followed by:

Database update

If all goes well, your user database is created on MySql. The application will have a functional registration system and login. If EF Core is working, you have everything you need to create forms about the data application. ASP.NET MVC will provide the CRUD functionality (Create, Recover, Update, Delete) for you, if you add classes in the Models section of the code.

However, what about the implementation in Linux?

options If you are satisfied with a Docker container, right-click on the project, add Docker support and you can select Linux as the target operating system. It's a little black box, but Visual Studio and Docker will provide you with a work container.

However, this will not help you implement it in any old Linux VM. For that, you must press Publish in the Generate menu and select Publish to a folder . Click Advanced and you can select Linux-x64 as the target runtime.

  Publication settings for an ASP.NET Core application

Publication settings for an ASP.NET Core application

You can also choose between Frame dependent or Self-contained . This is an option between installing and maintaining the .NET Core runtime centrally on the server, or implementing the entire runtime with the application: about 112 MB in this case.

There are advantages and disadvantages. The self-contained implementation is simpler and avoids possible incompatibilities with what is installed centrally, but wastes disk space and requires repair. The Framework Dependent implementation will allow you to visit the .NET Core download page in many cases, or configure Microsoft repositories so that you can use apt-get or similar.

There is another complication, which is that .NET Core has its own built-in web server, called Kestrel, which may or may not have to be behind a reverse proxy depending on its configuration; It is recommended to use a reverse proxy. You must also configure a service for the application to run automatically. It is not like a LAMP application (Linux, Apache, MySQL, PHP) where the application will run whenever PHP is run.

1564036171 133 el reg sits down to code with net for linux and mysql hitting some bumps along the way It works! Registering a user with MySql and ASP.NET Core

The good news is that with this configuration, our demo application worked fine on Debian 8, although for some reason we never worked with the preconfigured www data user. creating a new user instead. Performance is good even in a small virtual machine.

However, it should be said that there is a friction in the Linux path with Visual Studio, from the templates that guide it to SQL Server, to problems with the use of MySQL, and follow the correct steps for your chosen Linux destination .

The use of Microsoft Azure and the Linux application service or other means to run Docker containers simplifies things in some way and complicates them in others.

Does it matter? It is not a big problem for an application of significant size, but for prototypes, kicking the tires makes a difference. What he discovers is that the implementation in Windows is both easier and better documented, which may be the way the company wants it, but, on the other hand, it can slow down the technology. ®

Balance consumerization and corporate control.

For More Updates Check out Blog, Windows Softwares Drivers, Antivirus, Ms Office, Graphic Design Don’t Forget to Look Our Facebook Page Get Into Pc like us & follow on Twitter- @getinpc

Please Note: This content is provided and hosted by a 3rd party server. Sometimes these servers may include advertisements. igetintopc.com does not host or upload this material and is not responsible for the content.