Run net application on linux

.NET Core Apps on Linux

One of the great benefits in working with .NET Core is knowing that your code will be cross platform. In particular it will run on Linux. This opens up a lot of possibilities. But does it really run on Linux if you have never seen it run? But even if you have seen it run, is it really working if you have never run the unit tests on Linux? In my way of looking at the world…no. So in this post I will lay out how to get your cross platform .NET Core apps running and tested on Linux in the most straightforward and efficient way. The approach is to develop code on Windows and test in Linux containers. This is the best combination in my view. So on your windows dev box the set up you need is Hyper-V and Docker. Getting this setup right is not without its challenges which I will not get into here, but I am pleased to report that once you get this working it stays working and I have had this setup working for years now through all manner of Windows and Docker updates. image Also needed are the dotnet CLI and VS Code for this optimum (in my view) setup. image All the code for this tutorial can be found at https://github.com/bobrundle/dotnettolinux I’ll start by creating a simple console app that adds the numbers that appear as arguments. image In VS Code… image Build and run on windows… image Here is where it gets interesting. Create a dockerfile for Linux deployment… image Build a Linux docker image image Let’s try running it… image Oops. ICU stands for Internationalization components for Unicode which is used to handle culture dependent APIs. .NET 5.0 requires ICU by default and it is not available by default on Linux. For a simple app such as ours, the easiest thing to do is disable globalization support. To disable globalization support we need to add another property to our add.csproj project file…

Project Sdk="Microsoft.NET.Sdk"> Exe net5.0 true   

Now lets build and run again… image Now let’s add unit tests. You test-first wingnuts will be very disappointed that I didn’t write these first, but I am simply not a test first guy. I could say more but need to stay focused. image Need to add a project reference to add.csproj…

  net5.0 false     runtime; build; native; contentfiles; analyzers; buildtransitive all  runtime; build; native; contentfiles; analyzers; buildtransitive all      
using System; using Xunit; using add; using System.IO; namespace AddTests < public class ProgramTests < [Theory] [InlineData(new string[] <>, "0",0)] [InlineData(new string[] , "6",0)] [InlineData(new string[] , "",1)] [InlineData(new string[] , "6.6",0)] [InlineData(new string[] , "0",0)] public void MainTest(string[] args0, string r0, int e0) < string outfile = Path.GetTempFileName(); var outstream = File.CreateText(outfile); Console.SetOut(outstream); int e1 = Program.Main(args0); Console.Out.Close(); string r1 = File.ReadAllText(outfile); Assert.Equal(e0, e1); if(e0 == 0) < Assert.Equal(r0 + Environment.NewLine,r1); >> > > 

Build the unit tests and run them… image To run the unit tests in Linux we need to more than move binaries…we have to setup a development environment and build the code before running the tests. To do this we need a Docker file in the parent directory to both code and test folders. image The Dockerfile…

FROM mcr.microsoft.com/dotnet/sdk:5.0 WORKDIR /src COPY /add add COPY /addtests addtests WORKDIR /src/addtests CMD ["dotnet","test"] 

Summary and Discussion

  1. A simple Windows console app was created, built and run on Windows.
  2. The console app was built for Linux on Windows and run in a Linux container.
  3. A xUnit testing library was created to run tests against the console app. It was built and run on Windows.
  4. The source for both the console app and the xUnit tests were built and run in a Linux container.
Читайте также:  Посмотреть pid процесса linux

The following questions about this approach come to mind.

Why are you not a test-first guy? My answer is too long to be considered here.

Your «unit tests» are actually integration tests! This is semantics. What we can agree on is 100% code coverage is the gold standard of automated testing and this has been achieved in this example.

What about macOS? You cannot run macOS containers on Windows. You can only run macOS containers on Macs. There might be a way to test all 3 platforms (Windows, Linux, macOS) on a Mac with containers. I will experiment when I get a chance.

Why build in the Linux container? Why not simply use a test runner to run the binaries? Indeed, this is a good idea. I simply don’t know how to get this to work with xUnit.

Why not construct a CI/CD pipeline to build and test on Windows and Linux in the cloud? Indeed, the next logical step. However, you still cannot reach macOS in the cloud.

I hope what I have done is useful and addresses some questions you might have. I spent about a day researching the various aspects of this problem. I came to this issue when I was designing a command line tool for Windows and came to realize that the tool would be useful on Linux. Then I began to look into building and testing on Linux and discovered the approach was not well documented and not straight-forward and so suggested a post to capture the learnings.

Источник

Install .NET Core Apps on Linux in 5 Minutes

Install .NET Core Apps on Linux in 5 Minutes

As a big fan of open source, I’m loving the fact that .NET Core is cross-platform. It opens up endless possibilities, from hobby projects, experiments, and proofs of concept, to massive high-load production applications that run on cost-effective infrastructure with high security and scalability. I usually get the simplest and cheapest $5/month Ubuntu-based virtual private server (VPS) from any cloud platform provider instead of the more complex and expensive container instances or cloud computing services.

Читайте также:  Минт линукс установка уефи

I’m going to guide you through the steps on how to set up a .NET Core runtime environment, and how to deploy a .NET Core web application with Okta authentication, once you’ve got an Ubuntu VPS, all using nothing more than the Terminal.

The Benefits of Using Okta

Okta is a cloud service that allows developers to create, edit, and securely store user accounts and user account data, and connect them with one or multiple applications. Using Okta, you don’t have to worry about implementing sign up, login and logout flows manually. In our sample app, we will set up Okta to handle our user management for OAuth sign-in. There are a few tricks to set up .NET Core to work on Linux (especially when it comes to containerization on a host like AWS or Azure), but don’t worry — you will get a good overview in this tutorial.

Prerequisites

Install .NET Core SDK/Runtime on Linux

.NET Core SDK or Runtime: Which One Is Best

The .NET Core runtime allows you to run applications on Linux that were made with .NET Core but didn’t include the runtime. With the SDK you can run but also develop and build .NET Core applications. Normally you would need only the runtime in a production environment and the SDK in a development environment.

Today we are going to build and run our sample application on the same machine. So let’s install the .NET Core SDK.

Adding the Package Repository

We need to add Microsoft’s package signing key to make the package repository trusted by the system.

Open Terminal and run the following commands:

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb 
sudo dpkg -i packages-microsoft-prod.deb 

Installing the SDK

sudo apt-get update; \ sudo apt-get install -y apt-transport-https && \ sudo apt-get update && \ sudo apt-get install -y dotnet-sdk-3.1 

To make sure the installation was successful, run:

The output should be the installed .NET Core version.

Building and Running a .NET Core application on Linux

We are going to use the .NET Core CLI to build and run the sample application.

Copying the source code

I have prepared a sample application for the sake of this example. Let’s use git to copy it to our machine from GitHub.

git clone https://github.com/oktadeveloper/okta-netcore3-deploy-linux-example okta 

Building the .NET Core Application

Enter the folder where we just copied the source code:

Читайте также:  Linux проверить открыт ли файл

The first build might take a while. Then the output should be something like:

Build succeeded. 0 Warning(s) 0 Error(s) 

Running the .NET Core Application

To run the application in Development mode, type:

Running the sample application will fail because we need to set up Okta login first.

Unhandled exception. System.ArgumentNullException: Replace with the client ID of your Application. You can copy it from the Okta Developer Console in the details for the Application you created. Follow these instructions to find it: https://bit.ly/finding-okta-app-credentials (Parameter 'ClientId') 

Quickly Set Up Okta Login

Navigate to Applications , then select Add Application .

Select Web as a platform:

On the next screen add the following: Login redirect URIs: https://localhost:5001/authorization-code/callback

Logout redirect URIs: https://localhost:5001/signout/callback

When finished, click Done.

Take note of your client credentials ( Client ID and Client secret).

Open appsettings.json in your favorite code editor and add your credentials.

You can find your Org URL in the top right corner of the Dashboard:

Now the sample app is ready to run:

info: Microsoft.Hosting.Lifetime[0] Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime[0] Now listening on: http://localhost:5000 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development info: Microsoft.Hosting.Lifetime[0] Content root path: /home/ubuntu/okta 

You can now open a browser window at http://localhost:5000 to see the application running. Also you can try logging with Okta in the top right corner.

Troubleshooting

In case you run into a Correlation Error after logging in with Okta, you need to manually set the SameSite cookie attribute to None , and enable SSL (HTTPS) on your server. Check out more about how SameSite affects your apps in this article.

Takeaways

Developing .NET Core applications on Linux is not the stuff of science fiction any more. Since Microsoft started moving away from closed-source and platform-dependent solutions, a Linux-based development environment has its advantages. I believe tools like VSCode and Rider—also available on every platform—are mature enough to make them reasonable competitors of the classic Visual Studio IDE for Windows. I’ve successfully used Linux as my primary development environment for .NET Core for a few years now. Give it a try yourself and let us know what your experience has been in the comments below!

Learn More About .NET and Okta

If you are interested in learning more about security and .NET check out these other great articles:

Don’t forget to follow us on Twitter and subscribe to our YouTube channel for more great tutorials.

Источник

Оцените статью
Adblock
detector