电脑不想装几十个g的 vs2017,那就用 vs code 吧

目标:

  • 创建一个类库项目 skany.core,并用 nuget 引用第三方组件 hash 实现加密算法
  • 创建一个单元测试项目 skany.tests,引用类库 skany.core,并测试其中的方法
  • 创建一个控制台应用程序项目 skany.output,引用类库 skany.core,并输出方法执行结果
  • 创建一个解决方案 skany.sln,包括以上三项目

环境

  • .net core sdk 2.2.202

开始

首先在 vs code 安装几个扩展插件

  • c#
  • c# extensions
  • .net core test explorer

这三个插件就可以做最基础的开发了

构建项目

创建解决方案文件夹 c:\here\skany
在 vs code 中打开这个文件夹
在 vs code 中打开终端(也可以在外部使用cmd或powershell)
接下来将使用 .net core cli 命令创建项目

# 创建类库项目
dotnet new classlib -n skany.core
# 创建控制台应用程序
dotnet new console -n skany.output
# 创建xunit单元测试项目
dotnet new xunit -n skany.tests
# 为 output 添加 core 引用(因为当前在解决方案目录,而不是项目目录,所以add后要加上项目名,以下同理)
dotnet add skany.output reference skany.core
# 为 tests 添加 core 引用
dotnet add skany.tests reference skany.core
# 为 core 项目添加 nuget 引用
dotnet add skany.core package hash --version 4.0.0
# 创建解决方案 sln
dotnet new sln -n skany
# 添加项目到解决方案
dotnet sln skany.sln add skany.core
dotnet sln skany.sln add skany.output
dotnet sln skany.sln add skany.tests
# 编译一下 output 和 tests 项目
dotnet build skany.output
dotnet build skany.tests

当不熟悉命令时,都可以通过 -h 或 –help 参数获取帮助,比如我不知道单元测试项目的参数,就输入 dotnet new -h 查看 new 的选项
注:其实用第三方插件(比如 vscode-solution-explorer)轻松实现以上命令的可视化操作,这里只是演示 .net core cli 命令用法

创建完后的项目结构

skany
 | skany.core
  | class1.cs
  | skany.core.csproj
 | skany.output
  | program.cs
  | skany.output.csproj
 | skany.tests
  | unittest1.cs
  | skany.tests.csproj
 | skany.sln

将 core 项目中的 class1.cs 改为 crypthelper.cs,代码如下

using hashlibrary;

namespace skany.core
{
  public class crypthelper
  {
    public static string hashpassword(string password, out string salt)
    {
      var hash = hashedpassword.new(password, hashlength: 50, saltlength: 10);
      salt = hash.salt;
      return hash.hash;
    }
    
    public static bool verifypassword(string password, string hashpassword, string salt)
    {
      var hash = new hashedpassword(hashpassword, salt);
      bool matches = hash.check(password);
      return matches;
    }
  }
}

在 tests 项目中别写测试案例,将 unittest1.cs 改为 cryptunittest.cs,代码如下

using system;
using xunit;

namespace skany.tests
{
  using core;

  public class cryptunittest
  {
    [theory]
    [inlinedata("zhang")]
    [inlinedata("baidu")]
    public void hashpasswordtest(string password)
    {
      string salt;
      var hashpassword = crypthelper.hashpassword(password, out salt);
      assert.notnull(hashpassword);
      assert.notnull(salt);
      assert.true(hashpassword.length == crypthelper.hashlength);
      assert.true(salt.length == crypthelper.saltlength);
      verifypasswordtest(password, hashpassword, salt);      
    }

    [theory]
    [inlinedata("zhang", "áēõĀ1fv¾Ēëüĝ}f§¼kè$æ7kĎĞĐmĬzĝČ9ëå«¢ýĪùøê£İ)¥jµqiįa", "ïûŀb³äĭonê")]
    [inlinedata("baidu", " g©®Ģ¹óäõ¥ģh»7ċuo¸%aoĮ©ĩ§8ĆkĄöĉĖß$µåë¬üö=ĝĴ¶cꨧh/", "ĀĖ§į^h7í_h")]
    public void verifypasswordtest(string password, string hashpassword, string salt)
    {
      assert.true(hashpassword.length == crypthelper.hashlength);
      assert.true(salt.length == crypthelper.saltlength);
      assert.true(crypthelper.verifypassword(password, hashpassword, salt));
    }
  }
}

单元测试

通过命令执行单元测试

dotnet test skany.tests

如果只想测试其中一个方法 hashpasswordtest

dotnet test skany.tests --filter hashpasswordtest

当然有可视化的测试插件,谁还用命令啊

控制台应用程序

在 output 项目调用 core.crypthelper 输出结果,代码如下

static void main(string[] args)
{
   string salt = null;
   var password = "baidu";
   var hashpassword = crypthelper.hashpassword(password, out salt);
   console.writeline("password => {0}", password);
   console.writeline("hashpassword => {0}", hashpassword);
   console.writeline("salt => {0}", salt);

   //===================================

   var matchs = crypthelper.verifypassword(password, hashpassword, salt);
   console.writeline("matchs: {0}", matchs);
   console.readline();
}

运行 output 项目

dotnet run --project skany.output

断点调试

在 output/program.cs 中第13行设置一个端点(鼠标在行首点一下即可,再点一下移除断点)

debug面板配置选择 .net core launch (console),点击绿色的开始调试按钮,启动调试

与 vs 中一样,可以通过 f5/f10/f11 控制调试流程,也可以添加 watch 变量,鼠标也可以感知变量值

如果开始测试后,警告提示启动程序找不到,可以按提示(也可以点击界面齿轮按钮)修改配置文件 .vscode/launch.json
核对 program 节点路径是否正确

发布

vs 中用工具发布,发布参数配置在 *profile.pubxml,但 vs code 中只能用命令

# 发布release配置,包括 .net core 运行时,分别发布到 linux 和 windows
dotnet publish -c release --self-contained -r linux-x64
dotnet publish -c release --self-contained -r win-x64

# 发布release配置,包括 .net core 运行时,指定目标框架 netcoreapp2.2
dotnet publish -c release -f netcoreapp2.2 --self-contained -r linux-x64
dotnet publish -c release -f netcoreapp2.2 --self-contained -r win-x64

# 发布release配置,不包括 .net core 运行时
dotnet publish -c release --self-contained false -r linux-x64
dotnet publish -c release --self-contained false -r win-x64

# 发布release配置,不包括 .net core 运行时,指定输出目录
dotnet publish -c release --self-contained false -r linux-x64 -o c:\here\spany\publish\linux-x64
dotnet publish -c release --self-contained false -r win-x64 -o c:\here\spany\publish\win-x64

虽然 vs 无比强大,vs code 小清醒,但是 vs code 灵活扩展性强,用来开发前端或 .net core 项目,也是得心应手

到此这篇关于使用vscode开发和调试.net core程序的方法的文章就介绍到这了,更多相关vscode开发和调试.net core内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!