. NET Core series: 3. Use multiple projects. netcore

Source: Internet
Author: User
Tags dotnet

. NET Core series: 3. Use multiple projects. netcore

Through the previous two articles, we have learned how to create a new project, how to generate and run our applications, and what the content in the project. json file (roughly) means. However, most projects usually require multiple projects or referenced class libraries. We want to create a class library project and an application project. How to reference our class library in an application.

Create an Application Project File> New Application

If you are familiar with this, enter the following command:

Mkdir DotnetNewApp

Cd DotnetNewApp

Dotnet new

Dotnet restore

Dotnet build

Dotnet run

[Root @ Mono ~] # Mkdir DotnetNewApp
[Root @ Mono ~] # Cd DotnetNewApp/
[Root @ Mono DotnetNewApp] # dotnet new
Created new C # project in/root/DotnetNewApp.
[Root @ Mono DotnetNewApp] # dotnet restore
Log: Restoring packages for/root/DotnetNewApp/project. json...
Log: Writing lock file to disk. Path:/root/DotnetNewApp/project. lock. json
Log:/root/DotnetNewApp/project. json
Log: Restore completed in 8774 ms.
[Root @ Mono DotnetNewApp] # dotnet build
Project DotnetNewApp (. NETCoreApp, Version = v1.0) will be compiled because expecte d outputs are missing
Compiling DotnetNewApp for. NETCoreApp, Version = v1.0

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

Time elapsed 00:00:03. 7500023


[Root @ Mono DotnetNewApp] # dotnet run
Project DotnetNewApp (. NETCoreApp, Version = v1.0) was previusly compiled. Skippin g compilation.
Hello World!

 

We have created an application project. Next we will create a class library project.

Create a Class Library Project File> New Library

By default, dotnet new creates a console project. It can also create other project types:

[Root @ Mono DotnetNewApp] # dotnet new-t-h
Unrecognized type:-h
Avaiable types for C #:
-Console
-Web
-Lib
-Xunittest

Look at this output, the word spelling is wrong, Avaiable should be Available, has been raised the bug https://github.com/dotnet/cli/pull/3822, in addition to the console, you can also create Web, Lib and xunittest, the project type is the same as that of Visual Studio 2015. The web is the asp.net core template, xunittest is the test project, Lib is the class library project type we need to create, so we will add the-t parameter to specify as Lib, scott Hanselman's blog post indexing ing dotnet new. NET Core is also detailed:

[Root @ Mono DotnetNewApp] # cd ..
[Root @ Mono ~] # Mkdir DotnetNewLib
[Root @ Mono ~] # Cd DotnetNewLib/
[Root @ Mono DotnetNewLib] # dotnet new-t Lib
Created new C # project in/root/DotnetNewLib.
[Root @ Mono DotnetNewLib] # pwd
/Root/DotnetNewLib
We have created a C # Class Library Project/root/DotnetNewLib

What is the difference between the console and the class library project? The previous article ". NET Core series: 2. What medicines are sold in project. json" has been briefly mentioned. Let's take a look at the project. json of the Class Library project:

{
"Version": "1.0.0 -*",
"BuildOptions ":{
"DebugType": "portable"
},
"Dependencies ":{},
"Frameworks ":{
"Netstandard1.6 ":{
"Dependencies ":{
"NETStandard. Library": "1.6.0"
}
}
}
}

 

EmitEntryPoint is missing in buildOptions, which is a class library, so no entry is required. The dependency framework is not netcoreapp1.0, but replaced by netstandard1.6. The previous article briefly describes how netcoreapp1.0 and. NET 4.6.3 is implemented by NETStardard 1.6. Mono is also adjusting NETStardard, https://github.com/mono/mono/tree/netstandard.

Essentially, NETStandard. library is a target that supports the basic class Library at the lowest level, so that forward compatibility can be better. When a new version is available on the existing platform (such.. net core 1.1 or even 2.0) without the need to release new changes. Specific reference https://github.com/dotnet/corefx/blob/master/Documentation/architecture/net-platform-standard.md, the latest table:

Project Reference Project References

The attribute "target": "project" is different from the dependency attribute of Microsoft. NETCore. App."Type": "platform" is similar.

Next we will add the dependency of the class library DotnetNewLib to our console application, and add it in project. json of DotnetNewApp. The file content is as follows:

{
"Version": "1.0.0 -*",
"BuildOptions ":{
"DebugType": "portable ",
"EmitEntryPoint": true
},
"Dependencies ":{},
"Frameworks ":{
"Netcoreapp1.0 ":{
"Dependencies ":{
"Microsoft. NETCore. App ":{
"Type": "platform ",
"Version": "1.0.0"
},
"DotnetNewLib ":{
"Target": "project"
}
},
"Imports": "dnxcore50"
}
}
}
There is a premise that your DotnetNewApp and DotnetNewLib folders have the same parent folder. Here there are two things to note: every time you build dotnet to build DotnetNewApp, he will recursively try dotnet build DotnetNewLib. The second thing to note is to build rather than execute restore. You need to ensure that both projects run dotnet restore. Of course, you can execute dotnet restore in their parent folder. Project. json in all subfolders will be restored.

Next, we will call the DotnetNewLib multiplication method in the application DotnetNewApp on the console:

Using System;
Using DotnetNewLib;

 

Namespace ConsoleApplication
{
Public class Program
{
Public static void Main (string [] args)
{
Calculator calc = new Calculator ();
Var result = calc. Multi (9, 8 );
Console. WriteLine ($ "Hello World is {result }");
}
}
}

 

Compile and run

Switch to the DotnetNewApp folder. Now we can compile and run this application, and also compile our class library project DotnetNewLib.

[Root @ Mono DotnetNewApp] # dotnet build
Project DotnetNewLib (. NETStandard, Version = v1.6) will be compiled because expected outputs are missing
Compiling DotnetNewLib for. NETStandard, Version = v1.6

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

Time elapsed 00:00:02. 8849475
Project DotnetNewApp (. NETCoreApp, Version = v1.0) will be compiled because dependencies changed
Compiling DotnetNewApp for. NETCoreApp, Version = v1.0

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

Time elapsed 00:00:03. 0273700


[Root @ Mono DotnetNewApp] # dotnet run
Project DotnetNewLib (. NETStandard, Version = v1.6) was previusly compiled. Skipping compilation.
Project DotnetNewApp (. NETCoreApp, Version = v1.0) was previusly compiled. Skipping compilation.
Hello World is 72

Here we have successfully built a class library project and a console project. The console references the class library project to complete the multiplication operation. Although this is a simple multi-project application, the simplest project explains how to develop and pay attention to dotnet core multi-project applications.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.