In this post, we will see how we can add a class library project as a dependency to the console app we created in the previous post. DotNet CLI supports the below type of projects.

  1. Console – a basic console application
  2. Web – a web application running under ASP.Net Core 1.0
  3. Lib – a class library (reusable component)
  4. XUnitTest – an unit test project.

Here, we would be using the Lib project type.

Step :1 Setup the appropriate folders

Since the dotnet cli understands the project based on the folders, for us to have a class library project which a console app depends on, we need to create the following folder structures.

| -demoLibraryApp

| -demoLibraryApp/app

| - demoLibraryApp/library

Step : 2 Scaffold the library Project

Navigate to the demoLibraryApp/library folder and run the below command

dotnet new -t Lib

This would have created 2 files for you. Project.json and Library.cs. The project.json would look like the below one

"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable"
"dependencies": {},
"frameworks": {
"netstandard1.6": {
"dependencies": {
"NETStandard.Library": "1.6.0"

I am taking the same example of DirectoryParser, so I have made the changes to Library.cs as below

using System.IO;
using System.Linq;

namespace ClassLibrary
public class DirectoryParser
public int GetSubdirectoriesCount(string directoryName)
DirectoryInfo di = new DirectoryInfo(directoryName);
var files = di.GetDirectories();

foreach (var item in files)
System.Console.WriteLine("Directory Name : "+ item);
return files.Count();

Step 3 : Building the library project

Building the library project is as easy as running the dotnet build command. However, if you do that for the first time, without running a dotnet restore you would encounter the error as follows:

bash-3.2$ dotnet build
Project library does not have a lock file. Please run "dotnet restore" to generate a new lock file.
Project library does not have a lock file. Please run "dotnet restore" to generate a new lock file.

This is because, Project.lock.json helps the dotnet cli understand about all the dependencies a project is dependent on. Though we haven’t added any external Nuget Packages yet to this library project, it is vital to run dotnet restore first before triggering the dotnet build

bash-3.2$ dotnet restore
log : Restoring packages for /Users/z066157/SampleProjects/dotnetSamples/library/project.json...
log : Writing lock file to disk. Path: /Users/z066157/SampleProjects/dotnetSamples/library/project.lock.json
log : /Users/z066157/SampleProjects/dotnetSamples/library/project.json
log : Restore completed in 299ms.
bash-3.2$ dotnet build
Project library (.NETStandard,Version=v1.6) will be compiled because expected outputs are missing
Compiling library for .NETStandard,Version=v1.6

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

Time elapsed 00:00:00.9844542

Step 4 : Scaffolding console app

Navigate to the demoLibraryApp\app folder and run the below command

dotnet new -t console

Repeat the same steps i.e, run dotnet restore and then dotnet build to build the console app.

Step 5: Adding the reference of library project to console app

Open the Project.json file present under demoLibraryApp\app folder and add the below lines of code after the Microsoft.NETCore.App dependency

"library" :{
"target": "project"

Note : the property "target" : "project" tells the dotnet cli that “library” is a project dependency and needs to be restored from the local folder. DotNet CLI wouldn’t look at Nuget for restoring this dependency.

Make sure the dependency name matches with the folder name under which the class library project is present. In this case, the folder name is “library”, hence the dependency name is given as “library”. If you had created the folder with a different name, give that name as dependency.

Step 6: Build and Run the project

Modify the Program.cs under the ConsoleApplication with the below set of lines to make use of the class and method present in the class library we just added.

using System;
using ClassLibrary;

namespace ConsoleApplication
public class Program
public static void Main(string[] args)
Console.WriteLine("Hello World!");
DirectoryParser dp = new DirectoryParser();
var count = dp.GetSubdirectoriesCount("/Users/z066157/SampleProjects");

Running the command dotnet run  from demoLibraryApp\app folder runs the console app and produces the below output

bash-3.2$ dotnet run
Project library (.NETStandard,Version=v1.6) was previously compiled. Skipping compilation.
Project app (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Hello World!
Directory Name : appathon-demo
Directory Name : BowerSamples
Directory Name : CordovaSample
Directory Name : dotnetSamples
Directory Name : GitSamples
Directory Name : IonicSamples
Directory Name : MongoSamples
Directory Name : nodesamples
Directory Name : TacoCliSampels
Directory Name : watchOsSamples
Directory Name : XamarinSamples
Directory Name : YeomanSamples

We could also see that library.dll has been copied into the bin directory.

Screen Shot 2016-07-11 at 10.30.57 PM

The name of the “dll” file is dependent on the folder name which contains the library project. Since I have used “library” folder to create the class library project, I have got the dll as library.dll.