Page 21 - MSDN Magazine, September 2017
P. 21
Introduction to .NET Core
.NET Core is a new cross-platform and fully open source .NET implementation that was forked from .NET Framework and Silverlight. It’s optimized for mobile and server workloads by enabling self-contained XCOPY deployments.
To get a better feel for .NET Core, let’s take a closer look at what developing for .NET Core looks like. And let’s do this while explor- ing the new command line-based tooling. You can also use Visual Studio 2017 for your .NET Core development, but because you’re reading this article, chances are you’re quite familiar with Visual Studio already, so I’ll focus on the new experience.
When .NET was created, it was heavily optimized for rapid appli- cation development on Windows. In practice, this means that .NET development and Visual Studio were inseparable friends. And sure thing: Using Visual Studio for development is a blast. It’s super pro- ductive and the debugger is hands down the best I’ve ever used.
However, there are cases where using Visual Studio isn’t the most convenient option. Let’s say you want to just play with .NET to learn C#: You shouldn’t have to download and install a multi-gigabyte IDE. Or, say you’re accessing a Linux machine over SSH where using an IDE is simply not an option. Or, say you’re simply some- one who prefers using a command-line interface (CLI).
That’s why a first-class CLI was created, called .NET Core CLI. The .NET Core CLI’s main driver is called “dotnet.” You can use it for virtually all aspects of your development, including creating, building, testing and packaging projects. Let’s see what this looks like.
Start by creating and running a Hello World console app (I use PowerShell on Windows, but this will work equally well with Bash on macOS or Linux):
$ dotnet new console -o hello $ cd hello
$ dotnet run
Hello World!
The “dotnet new” command is the CLI equivalent of File | New Project in Visual Studio. You can create a variety of different proj- ect types. Type “dotnet new” to see the different templates that come pre-installed.
Now, let’s extract some of the logic into a class library. To do this, first create a class library project that’s parallel to your hello project:
$ cd ..
$ dotnet new library -o logic $ cd logic
The logic you want to encapsulate is the construction of a Hello World message, so change the contents of Class1.cs to the following code:
namespace logic {
public static class HelloWorld {
public static string GetMessage(string name) => $"Hello {name}!"; }
}
At this point, you should also rename Class1.cs to HelloWorld.cs:
$ mv Class1.cs HelloWorld.cs
Note that you don’t have to update the project file for this change. The new project files used in .NET Core simply include all source files from the project’s directory. Thus, adding, removing and renaming files doesn’t require modifying the project anymore. This makes file operations smoother from the command line. msdnmagazine.com
Figure 2 Updating the Program.cs File to Use HelloWorld Class
using System; using logic;
namespace hello {
class Program {
static void Main(string[] args) {
Console.Write("What's your name: ");
var name = Console.ReadLine();
var message = HelloWorld.GetMessage(name); Console.WriteLine(message);
} }
}
Figure 3 Changing the UnitTest1.cs Contents to Add a Test
using System; using Xunit; using logic;
namespace tests {
public class UnitTest1 {
[Fact]
public void Test1() {
var expectedMessage = "Hello Immo!";
var actualMessage = HelloWorld.GetMessage("Immo"); Assert.Equal(expectedMessage, actualMessage);
} }
}
To use the HelloWorld class, you need to update the hello app to reference the logic library. You can do this by editing the project file or by using the dotnet add reference command:
$ cd ../hello
$ dotnet add reference ../logic/logic.csproj
Now, update the Program.cs file to use the HelloWorld class, as shown in Figure 2.
To build and run your app, just type dotnet run:
$ dotnet run
What's your name: Immo Hello Immo!
You can also create tests from the command line. The CLI sup- ports MSTest, as well as the popular xUnit framework. Let’s use xUnit in this example:
$ cd ..
$ dotnet new xunit -o tests
$ cd tests
$ dotnet add reference ../logic/logic.csproj
Change the UnitTest1.cs contents, as shown in Figure 3, to add a test.
Now you can run the tests by invoking dotnet test:
$ dotnet test
Total tests: 1. Passed: 1. Failed: 0. Skipped: 0. Test Run Successful.
To make things a bit more interesting, let’s create a simple ASP.NET Core Web site:
$ cd ..
$ dotnet new web -o web
$ cd web
$ dotnet add reference ../logic/logic.csproj
Edit the Startup.cs file and change the invocation of app.Run to use the HelloWorld class as follows:
September 2017 17