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

.NET引用项目内容文件未复制到linux-x64输出目录求助

问题:如何让P2项目的WebPerfCounter.json复制到Linux平台发布文件夹?

解决方案结构与依赖链

Solution layout:

[Solution]
     |
     +----[Project P1, .NET 6 Console App]
     |         |
     |         +---- ...
     |         |
     |         +----[Settings folder]
     |                   |
     |                   +----appsettings.json
     |
     |          
     +----[Project P2, .NET Standard]
     |         |
     |         +----[Settings folder]
     |                   |
     |                   +----WebPerfCounter.json
     |
     +----[Project P3, .NET Standard]

Dependency chain:

[Project P3] <---Ref By---- [Project P2] <---Ref By--- [Project P1]
                                |
                +---------------+
                |
           <ItemGroup>
             <ContentWithTargetPath Include="Settings\WebPerfCounter.json">
               <CopyToOutputPath>Always</CopyToOutputPath>
               <TargetPath>Settings\WebPerfCounter.json</TargetPath>
             </ContentWithTargetPath>
           </ItemGroup>

问题现象

P1.csproj中配置了<RuntimeIdentifiers>linux-x64;win-x64</RuntimeIdentifiers>,构建解决方案后生成了win-x64和linux-x64两个平台文件夹。其中win-x64文件夹按预期包含WebPerfCounter.json,但linux-x64文件夹中没有该文件。

尝试在P2.csproj中添加<Target><Copy /></Target>配置(如下),但未成功,且Visual Studio输出面板中发布P1时未显示配置的<Message>内容:

<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">
  <PropertyGroup>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <RootNamespace>P2</RootNamespace>
    <AssemblyName>P2</AssemblyName>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Title></Title>
  </PropertyGroup>
  <ItemGroup>
    <ContentWithTargetPath Include="Settings\WebPerfCounter.json">
      <CopyToOutputPath>Always</CopyToOutputPath>
      <TargetPath>Settings\WebPerfCounter.json</TargetPath>
    </ContentWithTargetPath>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="prometheus-net" Version="3.6.0" />
    <PackageReference Include="System.Diagnostics.PerformanceCounter" Version="6.0.0" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\P3\P3.csproj" />
  </ItemGroup>
  <Target Name="PublishPerfCounterConfigFiles" AfterTargets="Publish">
    <ItemGroup>
      <JsonPerfCounterConfigFiles Include="Settings\*.json" />
    </ItemGroup>
    <Message Text="Copying performance counter configuration now." Importance="high" Runtime="*" />
    <Copy DestinationFolder="$(PublishDir)Settings" SourceFiles="@(JsonPerfCounterConfigFiles)"
      Runtime="*" Architecture="*" />
  </Target>
</Project>

原因分析

P2是.NET Standard类库,当发布主项目P1时,类库的Publish目标不会被触发执行,因此你添加的AfterTargets="Publish"的Target根本没有运行,这就是为什么<Message>内容未显示,文件也没复制到linux-x64文件夹。

解决方法

方法1:修改P2的ContentWithTargetPath配置

在P2的<ContentWithTargetPath>节点中添加Runtime="*"和Architecture="*"属性,确保文件会被复制到所有运行时和架构的输出目录:

<ItemGroup>
  <ContentWithTargetPath Include="Settings\WebPerfCounter.json" Runtime="*" Architecture="*">
    <CopyToOutputPath>Always</CopyToOutputPath>
    <TargetPath>Settings\WebPerfCounter.json</TargetPath>
  </ContentWithTargetPath>
</ItemGroup>

方法2:在P1中显式引用P2的配置文件

直接在P1的csproj中添加对P2配置文件的引用,强制文件复制到P1的所有发布目录:

<ItemGroup>
  <Content Include="..\P2\Settings\WebPerfCounter.json" Link="Settings\WebPerfCounter.json">
    <CopyToOutputPath>Always</CopyToOutputPath>
    <CopyToPublishDirectory>Always</CopyToPublishDirectory>
  </Content>
</ItemGroup>

方法3:调整P2的Target触发时机

将P2中的Target触发时机从Publish改为Build,这样每次构建P2时都会执行复制操作,确保文件被传递到P1的输出目录:

<Target Name="CopyPerfCounterConfigFiles" AfterTargets="Build">
  <ItemGroup>
    <JsonPerfCounterConfigFiles Include="Settings\*.json" />
  </ItemGroup>
  <Message Text="Copying performance counter configuration now." Importance="high" />
  <Copy DestinationFolder="$(OutputPath)Settings" SourceFiles="@(JsonPerfCounterConfigFiles)" />
</Target>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 19:09:53