You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Visual Studio 2017使用.NET CLI构建.NET Core项目消除差异?

How to Make Visual Studio 2017 Use .NET CLI for .NET Core Projects

Absolutely! Aligning your local Visual Studio build/test pipeline with the .NET CLI (the same tool your Team City CI uses) is a great way to eliminate those frustrating "works on my machine" discrepancies. Here’s how to set it up for your .NET Core 2.0 project:

1. Override Visual Studio’s Default Build Process

Visual Studio 2017 uses MSBuild under the hood for .NET Core projects, but you can configure it to call the .NET CLI directly instead:

  • Right-click your project in Solution Explorer and select Properties.
  • Navigate to the Build Events tab.
  • In the Pre-build event command line box, clear any existing text and add:
    dotnet build "$(ProjectPath)" --configuration $(ConfigurationName) --no-incremental
    
    The --no-incremental flag ensures a clean build every time, matching what you’d get with a fresh dotnet build call in the CLI.
  • Check the box for Run the post-build event and select When the build updates the project output (or Always if you want to guarantee it runs on every build).

2. Tweak Your Project File for CLI Alignment

You can also modify your .csproj file to force Visual Studio to use the .NET CLI for builds:

  • Right-click your project and select Edit .csproj.
  • Add this custom build target to override the default MSBuild behavior:
    <Target Name="Build" BeforeTargets="Build">
      <Exec Command="dotnet build $(MSBuildProjectFullPath) --configuration $(Configuration)" />
      <Touch Files="$(TargetPath)" />
    </Target>
    
    This tells Visual Studio to run dotnet build directly, then touches the output file to signal the build is complete to the IDE.

3. Configure Test Explorer to Use .NET CLI

To make sure tests run via the .NET CLI in Visual Studio:

  • Go to Test > Test Settings > Default Processor Architecture and select the same architecture your Team City uses (e.g., x64).
  • Create a .runsettings file in your solution with this content:
    <RunSettings>
      <DotNetCliTest>
        <Command>dotnet test $(ProjectPath) --configuration $(Configuration)</Command>
      </DotNetCliTest>
    </RunSettings>
    
  • Go to Test > Configure Run Settings > Select Solution Wide runsettings File and pick your new .runsettings file. Now, when you run tests from Test Explorer, it’ll use dotnet test under the hood.

Bonus Tips to Prevent Future Issues

  • Match SDK Versions: Ensure the .NET Core SDK version installed locally matches what’s on Team City. Lock it in with a global.json file:
    {
      "sdk": {
        "version": "2.0.3"
      }
    }
    
  • Audit Project Properties: Look for conditional compilation symbols, target frameworks, or NuGet references that might behave differently between MSBuild and the CLI. Some packages have distinct assets for IDE vs. CLI builds.
  • Test with CLI First: Before pushing changes, always run dotnet build and dotnet test in your terminal—this is the fastest way to catch CI-aligned issues early.

Once you’ve set this up, your Visual Studio builds and tests will behave exactly like they do in Team City, so you won’t hit those unexpected failures again.

内容的提问来源于stack exchange,提问作者Emma Middlebrook

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:25:07