Learning C# and ASP.NET

One of the technologies that we teach at Nashville Software School is the C# and ASP.NET stack. It’s one of the trickier stacks to teach because the students are fresh out of their JavaScript education, so I need to attune their brains to building a solution where the code is compiled, strongly typed, and much more verbose.

There are also many moving parts that they needs to be aware of when building an application that many other stacks abstract away.

  • LINQ
  • Entity Framework
  • CORS
  • Code-first Modeling
  • View Models
  • Annotations
  • Model Binding
  • Razor Templating
  • View Components

On top of that, they have to learn the concepts of OOP, relational data concepts, ERDs, agile workflow, being a good teammate, etc. It’s a significant amount of knowledge for them to gain a slim understanding of in 60 days.

One thing that has made teaching the course easier is Microsoft’s wholehearted adoption of the open source, cross platform mindset once Satya Nadella took the reins of the company. I walked away from the Microsoft world back in 2011 because it was a mess. It was tough, because I absolutely love the C# language, but the ecosystem was too bloated, locked into the Windows OS, and notoriously hard to manage.

Now that .NET Core is out, and much more mature with 2.0, I’m as happy as a fox in a hen house. I can start the students coding in C# on day 1 because they can install .NET Core SDK, Visual Studio Code, and SQLite and immediately start learning. Windows does get installed as a VM on all their machines over the first couple weeks of the course, since they do need to gain some comfort with the OS, SQL Server, and Visual Studio Community Edition, but they are no longer required - which is both exciting, and a bit strange.

While I’m talking strange, I recently went through the process of deploying a small, sample ASP.NET MVC application to my Ubuntu VPS on Digital Ocean so that students could deploy their applications likewise and not have to worry about Azure deployment while they are looking for jobs. The fact that I can even type that sentence now feels odd.

I want to share that knowledge with the community.

Running ASP.NET on Linux

Azure is Microsoft’s cloud platform, and an option for deploying their code. However, since we are teaching .NET Core, and its whole philosophy is to be cross platform, I decided to find out how to deploy my example ASP.NET web application to my own Linux VPS on Digital Ocean with Nginx as the reverse proxy to the running dotnet assembly. The process was surprising straightforward and made me wonder at this brave, new world that we live in where running a .NET project on Linux is easy, and intended.

Here’s what you need to do. I’m going to make the assumption that you already have a VPS, or EC2 instance (or equivalent) running that you can deploy to.

  1. Get .NET Core installed on the machine.
  2. Clone your repository to a directory of your choosing on your machine.
  3. Run dotnet restore to install required packages.
  4. Run dotnet ef database update to create your database. Since this is Linux, unless you are using RHEL, you’ll have to stick to SQLite or MySQL - basically anything but SQL Server as your engine.
  5. Run dotnet run to verify that the build works and go to your domain on port 5000 to test that it works as intended. Make sure you open up port 5000 on your firewall temporarily to test it.
  6. If your application builds, and works, then it’s time to start configuring your system to run it as a service. First, create a new A record on your domain so that your application will run as a sub-domain if you like.
  7. The folks at Microsoft put together a wonderful guide for setting up Nginx configuration, service configuration, and publishing your application. Read the ASP.NET Core on Linux with Nginx. Here’s a quick highlight of those steps.
    1. Run dotnet publish to package up your application into a production assembly.
    2. Create a new sites-available configuration file. I made /etc/nginx/sites-available/commerce because I’m running a sub-domain - commerce.bangazon.com.
    3. Create a symlink of your new config file to sites-enabled with ln -s /etc/nginx/sites-available/commerce /etc/nginx/sites-enabled/commerce.
    4. Reload nginx configuration with nginx -s reload.
    5. Create the systemctl config file for your dotnet process.
    6. Enable the service (e.g. systemctl enable commerce.service).
    7. Start the service (e.g. systemctl start commerce.service).
    8. Check the status of the service (e.g. systemctl status commerce.service).
More Recent Posts: