In the back of the nonsense, the operation steps are posted first. If you just want to see how to add a reference to a local package, you can skip to step sixth .
First, open the VS Code, use the shortcut key "Ctrl +" to open the terminal, or find "Integration terminal" and click on the "View" menu in the menu bar. Enter the command in the Open Terminal window to navigate to the directory where you saved the program, and create a new folder "Localnupkgrefexample", which is defined by the folder name. As shown in.
Command Description:
CD parameter: DOS command, navigate to the folder specified in the parameter
mkdir parameter: dos command, create folder in current directory, name is specified by parameter. If a path is specified in the parameter, a folder, such as MkDir, is created at the specified location. /test, the Test folder is created at the top level of the directory.
To make it easier to manage files, we can open the folder you just created (the "Open Folder" item under the "File" menu) in VS Code, and then manage the files inside it through the VS Code Explorer, which no longer says, and that you can try it out for yourself just like you do with a new phone.
Note: This example is done with. Net Core 2.0 And if it is not installed on your PC, please download and install it first.
The second step is to add an empty project using the dotnet new command named "Localnupkglib", which is not appropriate for the referenced local package (now called "package") because it is just a project for the hypervisor when it was written, not yet packaged. As shown in. After running the command, a folder "Localnupkglib" is created under the current directory "Localnupkgrefexample" and a project file named Localnupkglib is created, along with a class file called Class1.
Command Description:
dotnet New template-N name: You need to use the dotnet command prompt when using. NET core commands, the new command is used to indicate that a new file is added, the template is used to indicate the file type, and-n indicates that the file name is specified by the following parameter "name". The template is classlib, which means that the class library is created. If you do not use-N to specify a file name, the name of the current folder is the file name by default.
You can use dotnet new or dotnet new-h or dotnet new--help to see which file types The new command can create.
In the third step, modify the name of the Class1 in the Explorer to Myclasslib (delete the class file and add a new one), modify the class name to Myclasslib, and add a property message inside it for use in other projects.
The fourth step is to package the project just now. Use the command Pack to package the project to the specified location.
Command Description:
Pack Project Name-O Target Location: The project name is the name of the project Locanupkglib.csproj that you created earlier Localnupkglib,-o enter where you want to place the package, and if you do not specify a location, the default package is packaged in the build file of the project you want to package, which is inside the Debug folder. In order to facilitate other project references, the location is placed under the folder Localnupkgrefexample with the item's sibling.
If the project name is written with an extension when you package it, you need to write the project directory when you run the command, as shown in the directory where the current command is Localnupkgrefexample, and the project file Locaknupkglib in the subfolder localnupkglib. So run the command dotnet pack localnupkglib.csproj-o directly. \mypack error occurred, prompted to find the project file, and write on the subfolder can be normal packaging.
After packaging, you can see the folder mypacks that you just generated in Explorer, which contains a package file localnupkglib.1.0.0.nupkg.
Personal conjecture when you do not write the project extension, the system thinks that the project is in the provided folder Localnupkglib, will find the project file in this folder, the name is not the same (this is not verified).
If you need to know the detailed packaging commands, you can use dotnet pack-h to view them.
Fifth, add a console project that references the package you just built.
Add a console project using the command dotnet new Console-n Localnupkgapp. This is similar to the command to add a class library, but instead changes the classlib to console. Others no longer make instructions.
Expand the Localnupkgapp folder in Explorer and you can see that there is a project file localnupkgapp.csproj in it. Open the Program.cs class file, where we invoke the method in the class library localnupkglib that you added in the previous project. As shown in. There is no smart hint because the packaged package file is not referenced here.
You can use the command dotnet new or dotnet new-h to see what types of files can be generated, and the commands need to use the contents of the short Name column to represent the type of file to be generated.
Build this console project and see what happens.
An error occurred after using the command dotnet build Localnupkgapp, as shown in: Missing reference. Is it the same as the problem that occurs in Visual Studio? The next step is to solve the reference problem.
Sixth step, add a local reference to the console project.
Run the command dotnet Add Localnupkgapp package localnupkglib. The error occurred after the operation, indicating that the package could not be found. Note that the error prompt is found on a Web site, not the location we specified locally mypacks.
In order for the system to know where to find our package files, we need to modify the project file Localnupkgapp.csproj for a console project.
As shown, open the Localnupkgapp.csproj file, add the element <restoresources> in the PropertyGroup, ... \mypacks</restoresources>, ".. \mypacks "is the package location that we specified because the Mypacks folder is located on the top level of the console project folder, so you need to use".. " To determine the relative path.
If you need to set the location of multiple packages, you can add multiple locations in the restoresources element, with semicolons between multiple locations ";" Separated. For example, <RestoreSources>C:\Users\sun\.nuget\packages;. \mypacks; Https://api.nuget.org/v3</restoresources>
Run the Package Reference command again. You can see that there are no errors and the display has been added to the package. At this point in the view Localnupkgapp.csproj file you can see that an element has been added <PackageReference>. Of course this element can also be manually added to the project file instead of the command line.
If we want to refer directly to the project instead of the class library (package), then use the command dotnet add Localnupkgapp reference localnupkglib\localnupkglib.csproj. Omitting the project path and extension will cause an error, indicating that the item could not be found. The system adds a project reference to the <ItemGroup> element of the project file after it is run:
<projectreference Include=". \localnupkglib\localnupkglib.csproj " />
Seventh step, rebuild the project. No error has been made.
Eighth step, run the project. You can see that the message we added has been shown.
To run the project using the Run command, note to add-p to specify the project name.
To this, adding the local class library to the project has ended.
Some of the commands in the article are not explained in detail, because each command has multiple parameters, it takes a lot of time to explain it, and I hope that I have time to summarize the methods and techniques of these commands.
Some parameters in the command can write the project name directly, and some need to fill in the relative path of the project, these differences need to be collated, and understand why some can write the project name directly, while others need to write the path.
Because in the new contact with. NET Core 2.0, the inside of the various commands are not familiar with the addition of local class library, encountered the problem of adding a lot of articles on the Internet is not accurate, and many are for. NET Core 1.0, very hard to find an article a little bit of a look, coupled with their own test finally figured out. Here forgot the address of the article, just re-search a bit, did not find ... Temporarily can not add the article address, the author of the article does not live.
Because I was just in touch with. NET Core 2.0, and there is no blogging experience, the content is scattered, I hope to help everyone, but also hope that. NET core 2.0 the better, do not finish.
Referencing a local package in a. Net Core 2.0 Project