如何将.NET 5.0客户端与服务端公共依赖移至独立文件夹?
解决方案:将.NET 5.0项目公共依赖迁移至独立文件夹
最终目录结构规划
调整后release目录结构如下,确保client与server独立,公共依赖集中存储:
release/ ├─ client/ │ └─ client.exe、client.dll及client专属文件 ├─ server/ │ └─ server.dll及server专属文件 └─ common/ └─ 所有公共托管依赖DLL
步骤1:修改项目文件,控制依赖输出
调整client.csproj
在原有配置基础上,添加公共依赖的定向复制规则,同时禁止公共依赖输出到client目录:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> <!-- 原有PropertyGroup配置保持不变 --> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <OutputPath>../../release/client/</OutputPath> <AssemblyName>client</AssemblyName> <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> </PropertyGroup> <!-- 新增公共依赖控制配置 --> <ItemGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <!-- 将公共依赖复制到common目录,排除client自身文件和调试符号 --> <PublishFile Include="*.dll" Exclude="client.exe;client.dll;*.pdb"> <DestinationRelativePath>../../common/%(Filename)%(Extension)</DestinationRelativePath> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </PublishFile> <!-- 禁止公共依赖复制到client输出目录 --> <Content Remove="*.dll" Exclude="client.exe;client.dll"/> </ItemGroup> </Project>
调整server.csproj
同理添加对应配置,确保公共依赖仅输出到common目录:
<Project Sdk="Microsoft.NET.Sdk.Web"> <!-- 原有PropertyGroup配置保持不变 --> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <OutputPath>../../release/server/</OutputPath> <AssemblyName>server</AssemblyName> <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> </PropertyGroup> <!-- 新增公共依赖控制配置 --> <ItemGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <!-- 将公共依赖复制到common目录,排除server自身文件和调试符号 --> <PublishFile Include="*.dll" Exclude="server.dll;*.pdb"> <DestinationRelativePath>../../common/%(Filename)%(Extension)</DestinationRelativePath> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </PublishFile> <!-- 禁止公共依赖复制到server输出目录 --> <Content Remove="*.dll" Exclude="server.dll"/> </ItemGroup> </Project>
步骤2:配置程序集解析路径
为client和server添加程序集解析逻辑,使其能从common目录加载依赖:
Client(WinForms程序)
在Program.cs入口处添加解析事件:
using System; using System.IO; using System.Reflection; using System.Windows.Forms; namespace client { static class Program { [STAThread] static void Main() { // 注册程序集解析事件,指向common目录 AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { var assemblyName = new AssemblyName(args.Name).Name; var commonDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\common\", $"{assemblyName}.dll"); if (File.Exists(commonDllPath)) { return Assembly.LoadFrom(commonDllPath); } return null; }; Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
Server(Web程序)
在Program.cs主机构建前添加解析事件:
using System; using System.IO; using System.Reflection; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; namespace server { public class Program { public static void Main(string[] args) { // 注册程序集解析事件,指向common目录 AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { var assemblyName = new AssemblyName(args.Name).Name; var commonDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\common\", $"{assemblyName}.dll"); if (File.Exists(commonDllPath)) { return Assembly.LoadFrom(commonDllPath); } return null; }; CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); } }
步骤3:验证与优化
- 分别在client和server项目目录执行发布命令:
dotnet publish -c Release -p:Platform=x64 - 检查release目录:client和server目录仅保留自身程序文件,common目录包含所有公共依赖
- 若存在版本差异的依赖,不要放入common目录,保留在各自项目目录中
- 可根据实际依赖情况调整
PublishFile的Include/Exclude规则,确保专属文件留在对应目录
内容的提问来源于stack exchange,提问作者geek175
相关产品推荐
相关产品推荐

