.NETowo


.NET Core, web development with React and other JavaScript Frameworks

.NET Core on (NO)Windows systems – .NET core CLI

tags: dotnet CLI, Visual Studio Code

Welcome on dotnetowo.pl. Today I would like to invite you to a series of guides about creating application with .NET core. How title says the tutorial is for another operating systems than windows. But it not means that I’m opposite for Microsoft. A long time I have used the ecosystem and I’m still using but some time ago I bought MacBook and I want to check how this system works. I’m really glad from .NET core developing and I have choice now what system I want to use in C# software development :).

In this article I will show you a .NET core CLI commands which are unnecessary if you want creating fast and high quality software in .NET core framework.

At the beginning I recommend to download Visual Studio Code. Of course you can use any text editor. However the Microsoft solution is based on good and known Atom editor and it contains a lot of advantages like IntelliSense and debugging support. The application is to download on https://code.visualstudio.com. There is available Visual Studio of Mac on Mac OSx systems but in my opinion it’s not ready to professional usage. There are a few bugs in file saving and IntelliSense in this software.

If you’ve downloaded Visual Studio Code you should install below extensions:

The software environment is ready. Now it is the time to create new project. Our task is create a simple contacts book. The application will be developing in whole .NET core on (NO) Windows systems series. Our solution will contains three projects:

  1. Contacts.Web – MVC Application
  2. Contacts.DataAccess – Class Library Application
  3. Contacts.Tests – xUnit test Application.

At the beginning let’s check that .NET CLI is correct installed on our machine. In Terminal (or Command Line – Windows) type “dotnet”. Expected result:

 
dotnet command result

If a result on your screen is other than mine please check installation tutorial here.

Create new solution

A first step is creating a new solution. Thanks for that we will use convention like in Visual Studio what will provider compatibility on another platforms.

Command: 
dotnet new sln –n Contact 
(n – file name)

 
dotnet new solution

Create projects and add references for they in solution

If you want to create new project you can use dotnet new command. Every project will be in a separate directory which we can define with output parameter.

Adding a Contact.Web project

Command: 
dotnet new mvc –n Contact.Web –o Web 
(n – file name, o – path) 

Adding a Contact.DataAccess project

Command: 
dotnet new classlib –n Contact.DataAccess –o DataAccess 

Adding a Contact.Tests project

Command: 
dotnet new xunit –n Contact.Tests –o Tests 

 

Adding projects to solution

Command: 
dotnet sln Contact.sln add Web/Contact.Web.csproj
dotnet sln Contact.sln add Tests/Contact.DataAccess.csproj
dotnet sln Contact.sln add DataAccess/Contact.DataAccess.csproj

Nuget packages installation

There will be used Entity Framework to connect with database in the next series in DataAccess project so there are required references to Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.Design. We can add it in two ways:

With dotnet CLI usage

In the DataAccess directory where is Contact.DataAccess.csproj file execute below command.
Command: dotnet add package Microsoft.EntityFrameworkCore

Command: 
dotnet add package Microsoft.EntityFrameworkCore 

 

csproj modification

There was saved information about project in JSON file in last .NET Core versions. But now in .NET Core 1.1 Microsoft move this to csproj file. Thanks for that it looks similar like in classic .NET. During executing above command .NET CLI added package reference to csproj file. So the second solution is – manually editing a csproj file.

<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" /> 

 

To restore nuget packages you can use dotnet restore command in sln file directory.

 

Adding references between projects

Usually solution contains few projects. There is the same rule in our case. In this step we will add references (sln file directory):

Contact.Web has reference Contact.DataAccess

Command: 
dotnet add Web/Contact.Web.csproj reference DataAccess/Contact.DataAccess.csproj 

Contact.Tests references to Contact.DataAccess oraz Contact.Web

Command:
dotnet add Tests/Contact.Tests.csproj reference DataAccess/Contact.DataAccess.csproj Web/Contact.Web.csproj

 

There is possibility to modify references directly in csproj file.

Build and run

In a project directory which we want build and run our application we will execute below command (before you execute it, be sure that all dependences are downloaded – dotnet restore).

dotnet build

dotnet run
(in Contact.Web directory)

Application is available on http://localhost:5000.

Application debugging

In this step I will show you how you can debug an application using Visual Studio Code. I will not talk about details. I will show only basics but this will help you in next steps.

Before you start be sure that you have installed C# for Visual Studio Code extension which I recommended at the beginning of this art.

After start our application witch dotnet run command we will attach to the application process. We will add breakpoint to Home controller Index method and will check if debugging works correctly.

Configuration adding

In a debug bookmark we choose Add Configuration

Select .NET Core

There will be created launch.json file.

Attach to the application process

Select Contact.Web.dll process. In my case 16131

Check point

Add breakpoint in Controllers/HomeController.cs file and open http://localhost:5000 in your website. If everything has been configured correctly application should be stopped in place where you’ve added breakpoint.

I think that more details about debugging using Visual Studio Code I will describe in another post.

Summary

I hope information in this article will be helpful for person who wants to start adventure with .NET Core CLI and who wants to create applications on any operating system.
You should check other .NET core CLI features which are presented on https://docs.microsoft.com/en-us/dotnet/articles/core/tools/.

It’s my first article on this blog so I will be really grateful for your feedback :).

Repo: https://github.com/artzie92/dotnetowo_contact/tree/dotnetcore_basic


Got Something To Say:

Your email address will not be published. Required fields are marked *