How to write. NET Core Global Tools (two cases included)

Source: Internet
Author: User
Tags dotnet

I. What is. NET Core Global Tools

May 31, 2018 (Beijing Time) Microsoft released the official version of. NET Core 2.1, and. NET Core 2.1 brings us a new feature:. NET Core Global Tools. People who have used Nodejs must not be unfamiliar with NPM, it is inspired by NPM and provides a new deployment and extension mechanism for. NET Core Tools. It is a. NET Core console application that is packaged and acquired as a NuGet package. By default, these tools are framework-dependent applications and contain all of their NuGet dependencies. This means that. NET core tools runs by default on all operating systems and chip architectures that support. NET core, with a set of binary files. By default, the dotnet tool install ? command looks for tools on nuget.org. You can also use your own NuGet source instead.

Two. Use

Please make sure your. NET Core SDK is upgraded to 2.1 (version number 2.1.300), if not please visit the official website for installation: Https://www.microsoft.com/net/learn/get-started/windows

We use dotnet tool commands primarily to operate:

使用情况: dotnet tool [选项] [命令]选项:  -h, --help   显示帮助信息。命令:  install <PACKAGE_ID>     安装在命令行上使用的工具。  uninstall <PACKAGE_ID>   卸载工具。  update <PACKAGE_ID>      将工具更新为最新稳定版本以供使用。  list                     列出当前开发环境中的已安装工具。
1. Command using:

(1). Mounting tooldotnet tool install -g <工具ID>

(2). Unload Tooldotnet tool uninstall -g <工具ID>

(3). Update Tooldotnet tool update -g <工具ID>

(4). list locally installed tooldotnet tool list -g

2. Installation of the first tool

The official provides us with a tool example dotnetsay , which we install by command:

dotnet tool install -g dotnetsay

Then we execute the command to use the tool

dotnetsay

It's for me. We've exported a robot:

You can access Dotnetsay's source code to learn how to write tool.

Three. Preparation of the first. NET core Global Tool1. Create a new. NET Core Console Program

2. Writing code

We write a code that outputs "Hello. NET Core Global Tools".

using System;namespace HelloDotnetCoreTool{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("Hello .NET Core Global Tools");        }    }}
3. Edit the Csproj file

Select the item, right-click menu, edit the csproj file and PropertyGroup add it under the node:

<PackAsTool>true</PackAsTool>

Complete Example:

<Project Sdk="Microsoft.NET.Sdk">  <PropertyGroup>    <OutputType>Exe</OutputType>    <PackAsTool>true</PackAsTool>    <TargetFramework>netcoreapp2.1</TargetFramework>  </PropertyGroup></Project>
4. Edit NuGet Package Information

Select the item, right-click menu, Properties->package, to edit our package information.

5. Packaging

We can use dotnet pack commands to package, or we can use the VS menu to package: Select items-Right-click Menu-Package (project configuration is selected as Release), and in the bin\Release directory, you can find our packaged NuGet package.

6. Installation

Open a command prompt (cmd), go to the directory where the NuGet package is located, and execute the command to install:

dotnet tool install -g HelloDotnetCoreTool --add-source ./

The following prompt appears to indicate that the installation was successful:

We then use the List command to query the lists of installed tools:

dotnet tool list -g

As you can see, the tools we've written have been installed successfully.

7. Use

We use commands at the command prompt (cmd) to hellodotnetcoretool output the "Hello. NET Core Global Tools" that we just wrote

Four. Publish to Nuget.org

We can publish the tools we have written to nuget.org, so anyone can install and use our tools by command.

Use your browser to access: http://www.nuget.org/, and then sign in with your account.

We select the menu "Upload Packages" in the upper right corner

Choose the file we just packaged to upload it:

After uploading, you need to wait about 10 minutes, nuget.org will verify your package, this time you are unable to install your tool through the command, only the verification passed. And then wait 20 minutes or so, nuget.org will index your tools, and this time you will be able to access your tools by searching.

Five. Case studies

I've written two practical. NET Core Global Tool, here for everyone to do a brief introduction

1.DotnetRSA

It is a tool for generating the keys required for RSA cryptographic algorithms, supports the generation of XML, PKCS1, PKCS8 three formats, and supports the conversion of three formats to each other. It is a formal command-line application. You can use the command to install:

dotnet tool install -g dotnetrsa

Detailed Description: https://www.cnblogs.com/stulzq/p/9131074.html
Project Address: Https://github.com/stulzq/dotnetrsa

A simple demo that generates an XML-formatted RSA key with a length of 2048

dotnetrsa gen -f xml -s 2048 -o d:\

Generated secret key:

2.dotnet-cnblog

I wrote an article on how to efficiently write and synchronize blogs (Implementation of. NET core gadgets), where I introduced a. NET core implementation that can be quickly parsed into the markdown file 本地图片 and uploaded to the blog park, Colleagues for content conversion and then saved to the new file, you can 博客快速的发布在博客园 not need to manually upload each image, is a very convenient and quick use of gadgets, now I transform it into a. NET Core Global tool, you can use the command to install:

dotnet tool install -g dotnet-cnblog

Use the following method:

dotnet-cnblog <Markdown文件路径>

Use Demo:

For more information and project address, please visit: https://github.com/stulzq/CnBlogPublishTool

Six. Other tools

A list of tools has been maintained by Natemcmaster, please visit https://github.com/natemcmaster/dotnet-tools for viewing, and the dotnetrsa tools I have written have been added to this list.

Seven. References
    • [Translate]. NET Core 2.1 Release
    • . NET Core 2.1 Global Tools
    • The code used in this article: Https://github.com/stulzq/BlogDemos/tree/master/HelloDotnetCoreTool

How to write. NET Core Global Tools (two cases included)

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.