Multiple projects using Net Core

Source: Internet
Author: User
Tags dotnet

Through the previous two articles, we already know how to create a new project, how to build and run our application, and also know what (roughly) what the content in the Project.json file means. But most projects often also require multiple projects or referenced class libraries. We're going to create the class library project and the Application project. In the application, how to reference our class library.

Create an app project File--New application

This is familiar, enter the following command:

mkdir Dotnetnewapp

CD Dotnetnewapp

dotnet New

dotnet Restore

dotnet Build

Dotnet Run

[Email protected] ~]# mkdir Dotnetnewapp
[Email protected] ~]# CD dotnetnewapp/
[Email protected] dotnetnewapp]# dotnet New
Created New C # project In/root/dotnetnewapp.
[Email protected] 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 8774ms.
[[email protected] dotnetnewapp]# dotnet Build
Project Dotnetnewapp (. netcoreapp,version=v1.0) 'll be compiled because EXPECTE D outputs is missing
Compiling Dotnetnewapp for. netcoreapp,version=v1.0

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

TIME Elapsed 00:00:03.7500023


[[email protected] dotnetnewapp]# dotnet Run
Project Dotnetnewapp (. netcoreapp,version=v1.0) was previously compiled. Skippin G compilation.
Hello world!

We created the application project and then we created a class library project

Creating a Class Library project File-New Library

Dotnet New creates a console project by default, and he can also create other project types:

[Email protected] dotnetnewapp]# dotnet new-t-H
Unrecognized type:-H
avaiable types for C #:
-Console
-Web
-Lib
-Xunittest

Look at this output, the words are misspelled, 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 and the use of visual Studio 20,151 are addressed. The Web is an ASP. Xunittest is a test project, and Lib is the Class library project type we need to create, so we'll add the-t parameter to the Lib,scott Hanselman's blog exploring dotnet New with. NET Core is also very detailed:

[Email protected] dotnetnewapp]# CD.
[Email protected] ~]# mkdir dotnetnewlib
[Email protected] ~]# CD dotnetnewlib/
[Email protected] dotnetnewlib]# dotnet new-t Lib
Created New C # project In/root/dotnetnewlib.
[Email protected] dotnetnewlib]# pwd
/root/dotnetnewlib
We created a C # class library project that was completed/root/dotnetnewlib

Let's compare the difference between a console and a class library project. The previous article, ". NET Core Series: 2, Project.json this gourd in what medicine" we have briefly mentioned. Let's 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"
}
}
}
}

Class Library compilation options Buildoptions less Emitentrypoint, this is a class library, so there is no need for a portal. Depends on the framework is not netcoreapp1.0, but with a netstandard1.6 substitution, the above article we briefly explained that, in essence, netcoreapp1.0 and. NET 4.6.3 are Netstardard 1.6 implementations, Mono is also adjusting the netstardard,https://github.com/mono/mono/tree/netstandard.

Essentially, Netstandard.library is a goal-minimum support base Class library, which allows for better forward compatibility when new versions of existing platforms appear (such as. NET core 1.1 or even 2.0) without having to republish new changes. For specific reference https://github.com/dotnet/corefx/blob/master/Documentation/architecture/net-platform-standard.md, the current latest form:

How to understand this form

    • If a class library is specified. NET platform Standard 1.3, it can only run on platforms such as the. NET Framework 4.6 or newer frameworks, Universal Windows Platform (UWP), DNX Core 5.0, and Mono/xamarin.
    • If a class library is specified. NET platform Standard 1.3, then it can reference ( original: Consume) all from the previous. NET Platform Standard version (1.2, 1.1, 1.0).

If we want our class library project, we can use it more broadly, such as the old version. Net project can also be used, we can reduce the modification to netstandard1.0, which means that all. NET 4.5 or later versions of the runtime are compatible, plus Windows Phone Silverlight (8.0 + 8.1). It is noteworthy that the following versions of. Net 4.5 are incompatible with this new Netstandard versioning scheme. In our console application, we are targeting Microsoft.NETCore.App dependencies. This refers to a type of platform and declares a dependency on the runtime of our application.

Our console program will rely on our class library project to do some computational logic, we will use it to do a 2 number multiplication, modify Class1 to Calculator,method1 modified to multi:

Namespace Dotnetnewlib
{
public class Calculator
{
public int Multi (int x,int y)
{
return x * y;
}
}
}

The following entry into our focus today is the project reference.

Project Reference Projects References

Referencing the project and referencing the NuGet package is mostly "target": the "Project" attribute, which is the dependency property of the previous Microsoft.NETCore.App"type": "platform"类似。

下面我们给我们的控制台应用添加类库DotnetNewLib的依赖,在DotnetNewApp的project.json 添加,文件内容如下:

{
"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 are two things to note, whenever you dotnet build Dotnetnewapp, he will recursively try dotnet build Dotnetnewlib. The second thing to note is build instead of performing restore, you need to make sure that all two projects are running dotnet Restore, and of course you can do dotnet restore in their parent folder. All the subfolders inside the Project.json will be restored.

Next, we invoke the multiplication of dotnetnewlib in the console application Dotnetnewapp:

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}");
}
}
}

Compiling and running

We switched to the Dotnetnewapp folder, we can now compile and run the application, and we will compile our class library project Dotnetnewlib.

[[email protected] dotnetnewapp]# dotnet Build
Project Dotnetnewlib (. netstandard,version=v1.6) 'll be compiled because expected outputs is 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) 'll 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


[[email protected] dotnetnewapp]# dotnet Run
Project Dotnetnewlib (. netstandard,version=v1.6) was previously compiled. Skipping compilation.
Project Dotnetnewapp (. netcoreapp,version=v1.0) was previously compiled. Skipping compilation.
Hello World is 72

Here we have successfully built a class library project and a console project, and the console refers to the class library project to complete the multiplication, although it is a very simple multi-project application, we explain how dotnet Core's multi-project application development and considerations through this simplest project.

Multiple projects using Net Core

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.