如何让Visual Studio 2017使用.NET CLI构建.NET Core项目消除差异?
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:
Thedotnet build "$(ProjectPath)" --configuration $(ConfigurationName) --no-incremental--no-incrementalflag ensures a clean build every time, matching what you’d get with a freshdotnet buildcall 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:
This tells Visual Studio to run<Target Name="Build" BeforeTargets="Build"> <Exec Command="dotnet build $(MSBuildProjectFullPath) --configuration $(Configuration)" /> <Touch Files="$(TargetPath)" /> </Target>dotnet builddirectly, 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
.runsettingsfile 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
.runsettingsfile. Now, when you run tests from Test Explorer, it’ll usedotnet testunder 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.jsonfile:{ "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 buildanddotnet testin 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

