.NET Core Project Overview- Pt 1

.NET Core is Microsoft’s latest development framework. It is open source, cross platform, and features many improvements from prior versions of the .NET Framework. I’ve been very excited about .NET Core since it was released in the summer of last year.

There are many new features and improvements but, in this series I want to focus on just one piece, the project structure. With Visual Studio 2015 (Core Tools 1.0.0-preview2), there were quite a few changes to the traditional Visual Studio project. Visual Studio 2017 RC 1 (Core Tools 1.0.0-preview4) introduces even more changes to this project structure.

In this series, I want to take a dive into the project structure for both versions of the tooling. Later, we will discuss the compatibility and migration between the two versions.

VS 2015 .NET Core Project Overview

With the .NET Core Tools there are two ways we can create a project.

Lets start by creating a project with the command line tools.

Command Line Tools

Creating a new project is done with the new command.

> dotnet new

Created new C# project in C:\Jason\Demo\JrTech.Core101.Cmd.

This command will create two new files.

  • Program.cs
  • Project.json

Inside Program.cs file we see the traditional “Hello World!” message.

using System;

namespace ConsoleApplication
{
  public class Program
  {
    public static void Main(string[] args)
    {
      Console.WriteLine("Hello World!");
    }
  }
}

The project.json file is where the project metadata is defined. This replaces the project file from previous .NET projects.

{
   "version":"1.0.0-*",
   "buildOptions":{
      "debugType":"portable",
      "emitEntryPoint":true
   },
   "dependencies":{

   },
   "frameworks":{
      "netcoreapp1.0":{
         "dependencies":{
            "Microsoft.NETCore.App":{
               "type":"platform",
               "version":"1.0.1"
            }
         },
         "imports":"dnxcore50"
      }
   }
}

Now lets use the restore command to download all of our dependencies.

> dotnet restore

log : Restoring packages for C:\Jason\Demo\JrTech.Core101.Cmd\project.json...
log : Writing lock file to disk. Path: C:\Jason\Demo\JrTech.Core101.Cmd\project.lock.json
log : C:\Jason\Demo\JrTech.Core101.Cmd\project.json
log : Restore completed in 887ms.

We can see that a project.lock.json file has been created. During the package restore process, the dependencies from the project.json are analyzed so they can be downloaded with nuget. Not only do we want to gather the projects package references but, also the dependencies of those packages as well. For projects with a large amount of dependencies this can be a significant amount of work. As such, this information is cached in the project.lock.json file so, we can quickly restore in the future. The thing to keep in mind is this file is system generated and shouldn’t be modified directly.

With our dependencies restored, we are ready to build and run our project. This can be done by simply executing the run command.

> dotnet run

Project JrTech.Core101.Cmd (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling JrTech.Core101.Cmd for .NETCoreApp,Version=v1.0

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

Time elapsed 00:00:01.8575574

Hello World!

When it is all said and done, we end up with a project structure as shown below.

├── bin
├── obj
├── Program.cs
├── project.json
├── project.lock.json

Now this is the cool part! From here we can go on our operating system of choice (Windows, Linux, or OSX) and open the project in our favorite editor (Visual Studio Code, Sublime Text, Atom, etc). Building the project is as easy as executing a command.

Next, lets take a look at creating the same project in Visual Studio 2015.

Visual Studio

With Visual Studio open, we select File | New | Project and choose the Console Application (.NET Core) project template.

New Project

Once this is completed, we observe the following contents in the Solution Explorer.

Solution Explorer

On disk, we see the following files.

├── global.json
├── JrTech.Core101.Vs.sln
├── src
| └── JrTech.Core101.Vs
| ├── bin
| ├── obj
| ├── Properties
| | └── AssemblyInfo.cs
| ├── JrTech.Core101.Vs.xproj
| ├── Program.cs
| ├── project.json
| └── project.lock.json

A few of the files are replicas of what we saw in the previous example however, there are a couple new files that can be found. Some of these, such as the solution file and the assembly info file, we recognize from previous versions of .NET…

Some of them we will not.

The global.json file is new in .net core. This file provides solution level metadata on the sdk, projects, and nuget packages that are referenced by the solution. Below you will see the contents of this file.

{  
   "projects":[  
      "src",
      "test"
   ],
   "sdk":{  
      "version":"1.0.0-preview2-003131"
   }
}

The other new file is the xproj file. The xproj file is used to bridge the gap between the .NET Core project and Visual Studio. In contains, any MSBuild related information and other Visual Studio specific information.

<?xml version="1.0" encoding="UTF-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0">
   <PropertyGroup>
      <ActiveDebugProfile>JrTech.Core101.Vs</ActiveDebugProfile>
   </PropertyGroup>
</Project>

Summary

In this post, we took a look at creating a .NET Core project with Visual Studio 2015 (Core Tools 1.0.0-preview2). Next in this three part series, we will create the same project with Visual Studio 2017 RC (Core Tools 1.0.0-preview4). For further reading, please take a look at the following links listed below.

Advertisements

2 thoughts on “.NET Core Project Overview- Pt 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s