visual studio code for .NET

60 天前
 gopher666

2023 年我自己职业生涯中有个较大的变化,疫情结束之后,我来到了二线城市,花了好久才找到一份.NET 的工作。 入职面试的时候,面试官跟我提到了公司内.NET 后端的开发工具要转到 vscode 。

开始尝试

开始使用的时候用 c# for vscode,VSCode-solution-explorer,nuget package manager,C# XML Documentation Comments 等插件配合起来开发 dotnet6. 后来微软推出的 c# for vscode + c# dev kit 不断的升级,基本上取代了之前用的那些插件。 visual studio for MAC 上,微软后期也不在继续更新,这就意味着其他平台上.NET 开发工具的主力就会是 vscode 。 开始使用的时候先尝试了直接使用 dotnet cli 的一些基本命令,来管理项目和解决方案。

dotnet new sln -o solution // 新建解决方案
dotnet new list //列出 templates
dotnet new console -n conApp // 新建一个控制台程序
dotnet new classlib -n Lib -f net6.0  // 新建类库
dotnet sln add .\Lib\Lib.csproj 加入到解决方案

dotnet nuget

dotnet nuget list source 
dotnet nuget add source 添加源
dotnet nuget remove source 
dotnet disable source 
dotnet enable source

当然我们经常用过命令来管理项目确实有写麻烦。 c# kit dev 给 dotnet 开发者提供了 solution explorer 。

Change Cursor Style and Animation in VS Code

"editor.cursorBlinking": "smooth"

Unit test

目前 c# dev kit 还有一些存在的问题待修复。 比如我常用到 xunit 框架用来输出日志的 output ,还无法输出到控制台,无法看到自己打印的信息。 相关 issue. No output recorded after unit testing using xUnit 为了对代码进行测试覆盖,需要安装几个插件 首先代码中需要引入 Coverlet

dotnet add package coverlet.collector

xunit 项目中使用

 dotnet test --collect:"XPlat Code Coverage" 

这个命令可以配置过滤条件排除一些不想被统计的代码参考文档 需要添加一个配置文件 coverlet.runsettings

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="XPlat code coverage">
        <Configuration>
          <Format>json,cobertura,lcov,teamcity,opencover</Format>          
          <Exclude>[coverlet.*.tests?]*,[*]Coverlet.Core*</Exclude> <!-- [Assembly-Filter]Type-Filter -->
          <Include>[coverlet.*]*,[*]Coverlet.Core*</Include> <!-- [Assembly-Filter]Type-Filter -->
          <ExcludeByAttribute>Obsolete,GeneratedCodeAttribute,CompilerGeneratedAttribute</ExcludeByAttribute>
          <ExcludeByFile>**/dir1/class1.cs,**/dir2/*.cs,**/dir3/**/*.cs,</ExcludeByFile> <!-- Globbing filter -->
          <IncludeDirectory>../dir1/,../dir2/,</IncludeDirectory>
          <SingleHit>false</SingleHit>
          <UseSourceLink>true</UseSourceLink>
          <IncludeTestAssembly>true</IncludeTestAssembly>
          <SkipAutoProps>true</SkipAutoProps>
          <DeterministicReport>false</DeterministicReport>
          <ExcludeAssembliesWithoutSources>MissingAll,MissingAny,None</ExcludeAssembliesWithoutSources>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md 为了在 vscode 中可以展示对应文件的代码覆盖率,可以使用这个插件 Coverage Gutters https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters 为了生成对那个的代码测试报告,可以安装 RepotGenerator

Run multiple dotnet project at once

** tasks.json **

[
    {
            "command": "dotnet",
            "args": [
                "build",
                "${workspaceFolder}\\xxx.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile",
            "type": "process",
            "label": "dotnet: build APIGateway4"
    }
]

** launch.json **

[{
            "name": ".NET8 Launch (APIGateway4)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "dotnet: build APIGateway4",
            "program": "${workspaceFolder}\\APIGateway4.exe",
            "args": [],
            "cwd": "${workspaceFolder}\\src\\APIGateway\\APIGateway4",
            "stopAtEntry": false,
            "serverReadyAction": {
                "action": "openExternally",
                "pattern": "\\bNow listening on:\\s+( https?://\\S+)"
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            }
 }],
 "compounds": [
        {
            "name": ".NET8 Launch (ALL API Gateway)",
            "configurations": [
                ".NET8 Launch (APIGateway)",
                ".NET8 Launch (APIGateway2)"
            ],
            "stopAll": false
        }
    ]

Add bracket pair colorization to your settings.json

 "editor.guides.bracketPairs": "active",
 "editor.bracketPairColorization.enabled": true,

集成 cmder

按 Ctrl + , 打开 VS Code setting ,搜索 terminal profiles windows,或者 ctrl + shift +p 打开 vs code Command Pallet 搜索 编辑 settings.json

   "terminal.integrated.defaultProfile.windows": "Cmder",

    "terminal.integrated.profiles.windows": {
        "Cmder": {
            "name": "Cmder",
            "path": [
                "${env:windir}\\Sysnative\\cmd.exe",
                "${env:windir}\\System32\\cmd.exe"
            ],
            "args": ["/k", "${env:cmder_root}\\vendor\\bin\\vscode_init.cmd"],
            "icon": "terminal-cmd",
            "color": "terminal.ansiGreen"
        },
    },

需要提前配置一下环境变量 CMDER_ROOT 指向 Cmder 的安装目录

Hot keys

Quick fix 失效了

quick fix 按 ctrl + . 失效了,可以尝试修改成其他的快捷键 shift + ctrl + p 输入 Open Keyboard Shortcuts search quick fix 比如修改成 ctrl + shift + .

体会

总体感觉 vscode ,在微软更新了 c# + c# dev kit 之后,写 dotnet 的代码体验越来越好,给我的感觉是轻量和可定制化,希望 vscode for dotnet 越来越好。 ** 那么你有没有在使用 vscode 来开发 dotnet 呢? 可以与我一起讨论这个话题 **

1607 次点击
所在节点    .NET
21 条回复
cnbatch
60 天前
有点好奇,写代码时跟 Visual Studio 相比( Windows 版),体验怎么样?
几年前我用过 vscode 写 C# ,并不算好用,代码补全能力很差,quick fix 基本不存在,不知道现在“进化”得怎么样
xieren58
60 天前
没有... 还是用 vs studio 免费版
gopher666
60 天前
@cnbatch 至少现在我用 vscode 开发 dotnet6+感觉很不错。
yyyyyyh
60 天前
正经开发还是离 vs 差很远。 只是 vscode 更方便,配合 vscode web ,适合各种环境。 不过到了调试,断点体验,远程调试等等,还是远不如 vs 的
bc2
60 天前
在 Linux 下还是不可用, 配置了 dotnet sdk path 也不行.
idragonet
60 天前
一般 NET 开发还是用重量级的 Visual Studio 吧。
Bronya
60 天前
vsc 官方插件不支持 dotnet-script ,只能卸载了用 omnisharp
GalileoP
60 天前
项目大一点我就用 vstudio ,mac 下也用
Eiden
60 天前
好奇面试官为什么要求转 vsc, 授权原因吗
gopher666
60 天前
@Eiden 免费啊,VS 得花钱。
lichao
60 天前
用户体验应该是不能跟 Visual Studio 相比的
klo424
60 天前
我做 .NET 6 和 .NET 8 开发,两个版本在开发上基本没有差别。

目前,vscode 除了轻量,其他完全没法跟 Visual Studio 比。
gowk
60 天前
干 .NET 然而却叫 Gopher?
thinkershare
60 天前
VSCode 深度使用和 Visual Studio 差的实在太远了,甚至根本没法比,除非你开发的都是纯 API 项目。
Removable
60 天前
作为帕鲁还自费使用 Rider ,就为了用起来舒服
beginor
59 天前
还是 Rider 好用
noahlias
59 天前
我看 unity 游戏开发者也用 vscode 开发 c#
qwer1234zh
59 天前
@gopher666 visual studio 有社区版的,
zuiyue123
59 天前
@gopher666 Visual Studio 啥时候要花钱了,微软一直提供免费社区版,包含正式收费版功能的 80%差不多
gopher666
59 天前
@zuiyue123 "在非企业组织中,最多五名用户可使用 Visual Studio Community 。 在企业组织(即拥有超过 250 台电脑或年收入高于 1 百万美元的组织)中,只允许在上述参与开放源代码项目、学术研究和教室学习环境使用情景下使用 Visual Studio Community"

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1022574

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX