发布.NET 8 Windows服务:如何从自包含构建中排除指定文件
解决C# Windows服务自包含构建中外部文件部署问题
问题描述
我通过指定Profile Settings生成C# Windows服务的自包含构建(配置截图:
),当前需要将两个内容部署到构建目录根目录(而非打包进构建文件内部):
MyNotificationServiceCore.dll.config文件:服务部署必需的配置文件certificate文件夹:之前旧框架版本通过POST-BUILD EVENT复制证书文件
尝试修改FolderProfile.xml配置后未解决问题,查阅相关文档也无有效方案。注意到在生成net8.0-windows\publish\win-x64最终自包含包前,会先创建临时发布文件。
我修改的FolderProfile.xml配置如下:
<Project> <PropertyGroup> <Configuration>Release</Configuration> <Platform>Any CPU</Platform> <PublishDir>bin\Release\net8.0-windows\publish\win-x64\</PublishDir> <PublishProtocol>FileSystem</PublishProtocol> <_TargetId>Folder</_TargetId> <TargetFramework>net8.0-windows</TargetFramework> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <SelfContained>true</SelfContained> <PublishSingleFile>true</PublishSingleFile> <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract> <PublishReadyToRun>true</PublishReadyToRun> <SatelliteResourceLanguages>en</SatelliteResourceLanguages> </PropertyGroup> <ItemGroup> <Content Remove="MyNotificationServiceCore.exe.config" /> </ItemGroup> </Project>
解决方案
1. 单独处理配置文件
在FolderProfile.xml中添加配置,确保MyNotificationServiceCore.dll.config被排除在单文件打包外,并复制到发布目录:
<ItemGroup> <!-- 排除配置文件从单文件打包,同时复制到发布目录 --> <ExcludeFromSingleFile Include="MyNotificationServiceCore.dll.config"> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </ExcludeFromSingleFile> <!-- 确保源app.config文件的生成产物被正确复制 --> <None Update="app.config"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </None> </ItemGroup>
2. 处理certificate文件夹
添加文件夹复制配置,确保整个certificate文件夹及其内容被复制到发布目录根,且不被打包进单文件:
<ItemGroup> <None Include="certificate\**"> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> <ExcludeFromSingleFile>true</ExcludeFromSingleFile> </None> </ItemGroup>
完整的FolderProfile.xml配置
<Project> <PropertyGroup> <Configuration>Release</Configuration> <Platform>Any CPU</Platform> <PublishDir>bin\Release\net8.0-windows\publish\win-x64\</PublishDir> <PublishProtocol>FileSystem</PublishProtocol> <_TargetId>Folder</_TargetId> <TargetFramework>net8.0-windows</TargetFramework> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <SelfContained>true</SelfContained> <PublishSingleFile>true</PublishSingleFile> <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract> <PublishReadyToRun>true</PublishReadyToRun> <SatelliteResourceLanguages>en</SatelliteResourceLanguages> </PropertyGroup> <ItemGroup> <!-- 处理配置文件 --> <ExcludeFromSingleFile Include="MyNotificationServiceCore.dll.config"> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </ExcludeFromSingleFile> <None Update="app.config"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </None> <!-- 处理certificate文件夹 --> <None Include="certificate\**"> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> <ExcludeFromSingleFile>true</ExcludeFromSingleFile> </None> </ItemGroup> </Project>
关键说明
ExcludeFromSingleFile:指定文件/文件夹不被打包进单文件,保留在发布目录根CopyToPublishDirectory>PreserveNewest:确保文件/文件夹被复制到发布目录,仅更新最新版本- 针对
app.config的None Update配置,保证其编译生成的对应.dll.config文件被正确传递到发布目录
内容的提问来源于stack exchange,提问作者bob.mazzo
相关产品推荐
相关产品推荐

