Skip to content
分类目录:

DotNet CLI

Post date:
Author:
Number of comments: no comments

https://learn.microsoft.com/en-us/dotnet/core/tools/
https://source.dot.net/

Dotnet命令行:

New command:

# dotnet 命令行
dotnew [command] --help
# 显示项目模板:

dotnet new [options]
-o,--output Location to place the generated output.
-n,--name   name for the output being created. If none, will use -o value
-i,--install  install a source or a template package
-l,--list  list templates.

dotnet new webapi -o  .\Demos\FluentValidations 

dotnet new -l

dotnet new <type> [-n <ProjectName>] -o <DirName>

dotnet new sln,solution
dotnet new classlib

# 创建console
dotnet new console
# wpf
dotnet new wpf
dotnet new wpflib
dotnet new wpfcustomcontrollib
dotnet new wpfusercontrollib

# web
dotnet new web (aspnetcore empty)
dotnet new webapi
dotnet new web
dotnet wen mvc
dotnet new angular

dotnet add
# 为Project添引用

dotnet referece <Project_Path>:

dotnet build: 
dotnet clean:

dotnet list <package/referecne>:显示当前Project的包或引用


dotnet tool:
dotnet tool install <PackageId>

Run command:

dotnet [options] run:
[options]:
  -c,--configuration: Debug,Release
  -f,--framework:  target framework to run,必须同时在project文件中指定
  --project: 要运行的project的路径,如无,则用当前路径下的工程
  -p,--property
  --launch-profile:
  --no-build
  --no-restore
  -a,--arch : the target arcitecture
  --os the target operating system

# Run in source code.
dotnet watch run

# 恢复依赖
dotnet restore
# Run in binary 
dotnet aspnetcore.web.dll --urls="https://*:8079" --ip="127.0.0.1" --port=8029

dotnet run --urls="http://localhost:6000" ASPNETCORE_ENVIRONMENT=Local

Global.json

dotnet new globaljson

Nugetconfig

dotnet new nugetconfig

Sln command:


//如果 没有指定name,则会使用outputdir的名字。
dotnet new sln [-n <SlnName>] -o <Output dir name>

// 在ReaderSln文件夹中创建一个ReaderSln.sln文件
dotnet new sln -n ReaderSln -o ReaderSln


dotnet sln:修改soluntion文件
dotnet sln add <Project_Path> 将project添加到sln file中 -s Sln Folder
dotnet sln remove <Project_Path> 将project从sln 中移除
dotnet sln list: 显示所有的projects

dotnet sln  add .\Demos\FluentValidations\FluentValidations.csproj -s Demos 


# 在命令行输入时,支持模糊匹配,输入完成后,需要Tab键,自动查找和补全。
dotnet sln add *Access\*.csp
# 传递project的路径也可以自动将该目录下csproj添加到sln中。
dotnet sln add <Project folder>

Format command:


# Formats code to match editorconfig settings.
dotnet format <sln file>|<project file>

Test command

dotnet new xunit

dotnet test

Nuget 目录配置:

nuget的包默认在C盘,比较占用磁盘空间。

Nuget的配置文件路径:

nuget locals all -list

Mac

  • ~/.local/share/NuGet/Cache
  • ~/.nuget/packages

Windows

  • %LocalAppData%\NuGet\Cache
  • %UserProfile%\.nuget\packages

Linux

~/.local/share/NuGet/Cache
~/.nuget/packages

修改Nuget packages的配置:

https://docs.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders
  1. 修改环境变量 NUGET_PACKAGES , NUGET_HTTP_CACHE_PATH , NUGET_PLUGINS_CACHE_PATH
  2. 修改配置文件%AppData%\Roaming\NuGet
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="cnblog" value="https://nuget.cnblogs.com/v3/index.json" />
  </packageSources> 
  <config>
     <add key="globalPackagesFolder" value="D:\Users\linde\.nuget\packages" />
  </config>
</configuration>

Clear packages:

# Clear all caches (use either command)
dotnet nuget locals all --clear
nuget locals all -clear

package source

https://learn.microsoft.com/zh-cn/dotnet/core/tools/dotnet-nuget-add-source
dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org
//本地路径作为source
dotnet nuget add source c:\packages

dotnet nuget add source https://someServer/myTeam -n myTeam -u myUsername -p myPassword --store-password-in-clear-text

dotnet nuget remove source <name>

dotnet nuget list soruce

:: disable source.
dotnet nuget disable source <sourceName>



Package add
//CLI中指定了source后,会覆盖之前的配置,如果不指定source,
//会使用dotnet nuget list source中的配置
dotnet add package <packagename> --prerelease [-s <package_source>]

Package library

https://github.com/NuGet/Home/wiki/Adding-nuget-pack-as-a-msbuild-target#including-content-in-package
https://learn.microsoft.com/zh-cn/dotnet/core/tools/dotnet-pack

将library打包成nuget包使用

Package在csproj中的配置:

//Metadata
<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>disable</Nullable>
    <PackageId>...</PackageId>
    <Version>1.0.0</Version>
    <Authors>.....</Authors>
    <Company>....</Company>
    <Description>...</Description>
    <IncludeBuildOutput>true</IncludeBuildOutput>
    <PackageOutputPath>....</PackageOutputPath>
    <!--<NuspecFile>package.nuspec</NuspecFile>-->
  </PropertyGroup>


Resource, 如 config.json也要打包到nuget中:

<None Include="Resources\test.json" 
     Pack="true" 
     PackageCopyToOutput="true" 
     PackageFlatten="false" >
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
 </None>

Metadata也可以在VS2022中配置:

右击工程,属性,Package页面

Pack方式:

  1. VS2022中,右击项目,点击Pack,即可打包
  2. 命令行方式,dotnet pack
  3. 设置PackWhenBuild方式,即编译时,同时pack:在csproj中添加
<PropertyGroup>
  <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

如果要借助nuspec文件配置打包,则需要在csproj文件中添加下面的Property

<NuspecFile>package.nuspec</NuspecFile>

创建.Net CLI Tool

https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools-how-to-create
<PackAsTool>true</PackAsTool>
<ToolCommandName>botsay</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>

dotnet pack

Nuget Authenticate

https://github.com/microsoft/artifacts-credprovider#azure-artifacts-credential-provider
https://github.com/microsoft/artifacts-credprovider#session-token-cache-locations

在Nuget.config中配置PAT

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="intern-3rdparty" value="https://pkgs.dev.azure.com/nuget/v3/index.json" />   
    <add key="nuget" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
  <packageSourceCredentials>
	<intern-3rdparty>
		<add key="Username" value="<Email>" />
    <add key="ClearTextPassword" value="<PAT>" />	
  </packageSourceCredentials>
</configuration>

CSharepRepl

https://github.com/waf/CSharpRepl

是一个在命令行运行C#语言的工具。

安装和更新

# 安装工具
dotnet tool install -g csharprepl
# 更新工具
dotnet tool update -g csharprepl

使用

csharprepl

# 添加引用
#r "AssemblyName"

#r "path/to/assembly.dll"

#r "path/to/project.csproj"

#r "nuget: PackageName"

csharprepl --framework  Microsoft.AspNetCore.App

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

豫ICP备2021008859号-1