【发布时间】:2020-09-09 15:16:27
【问题描述】:
我目前正在尝试将旧的 csproj 文件迁移到 dotnet 核心样式文件。
我有一个简单的 csproj 文件,比如
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">
<PropertyGroup>
<PackageTags>build-$(BUILD_BUILDID)</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyName>Something</AssemblyName>
<AssemblyTitle>Something</AssemblyTitle>
<AssemblyProduct>Something</AssemblyProduct>
<Description>Something</Description>
<ProjectGuid>SomeGuid</ProjectGuid>
<EnableDefaultCompileItems>true</EnableDefaultCompileItems>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SomeReference" Version="6.0.2" />
<PackageReference Include="SomeReference" Version="3.4.0" />
<PackageReference Include="SomeReference" Version="4.7.0" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Configuration" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.ServiceProcess" />
</ItemGroup>
</Project>
Visual Studio 对此很满意,但排除了我的所有代码。我尝试将 EnableDefaultCompileItems 显式设置为 true(如上),但即使在项目卸载/加载之后也没有任何反应。
如果我尝试通过“显示所有文件”来“包含在项目中”,那么我会在构建时添加一堆“编译包含”元素:
<ItemGroup>
<Compile Include="Class1.cs" />
</ItemGroup>
这会产生以下错误
Severity Code Description Project File Line Suppression State Suppression State
Error Duplicate 'Compile' items were included. The .NET SDK includes 'Compile' items from your project directory by default. You can either remove these items from your project file, or set the 'EnableDefaultCompileItems' property to 'false' if you want to explicitly include them in your project file. For more information, see https://aka.ms/sdkimplicititems. The duplicate items were: 'Class1.cs' Some.Project.Name
这似乎表明 VS 已经知道该文件存在,但没有在解决方案资源管理器中显示它或将其包含在编译中。有没有人遇到过这个问题?
VS 版本信息:
Microsoft Visual Studio Enterprise 2019
Version 16.3.4
VisualStudio.16.Release/16.3.4+29409.12
Microsoft .NET Framework
Version 4.8.03752
【问题讨论】:
-
是Net Core项目的新特性,Net Core项目的csproj文件不同于旧的sdk格式。非常简洁,不需要添加任何项目include,
Microsoft.NET.Sdk默认会自动关联所有源文件,无需手动编写,这是Net Core相对于NET Framework的优势。 -
这个问题是从net framework迁移到net core的正常错误。正如微软文档所说,不要添加任何项目包括,你应该删除任何
compile,none,...项目。将EnableDefaultCompileItems设置为false将隐藏所有编译文件,不建议这样做。使用<PropertyGroup><EnableDefaultItems>true</EnableDefaultItems></PropertyGroup>显示所有文件。
标签: visual-studio msbuild