.NET Core 3.1升级至6后WCF服务绑定报错,添加绑定重定向无效求解决
解决.NET 6升级后WCF程序集加载错误的建议
1. 替换为.NET兼容的WCF NuGet包
.NET Core/.NET 5+不再依赖传统的System.ServiceModel(4.0.0.0版本),需安装官方维护的WCF客户端包:
- 卸载项目中旧的
System.ServiceModel.*包(若为直接引用) - 根据WCF功能需求安装对应NuGet包,可通过NuGet包管理器操作,或直接在项目文件中添加:
<ItemGroup> <PackageReference Include="System.ServiceModel.Primitives" Version="6.0.*" /> <PackageReference Include="System.ServiceModel.Http" Version="6.0.*" /> <!-- 按需添加其他WCF相关包,如System.ServiceModel.NetTcp、System.ServiceModel.Security等 --> </ItemGroup>
2. 手动添加程序集绑定重定向
自动生成的绑定重定向可能未生效,可在项目的app.config或web.config中手动添加规则:
<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.ServiceModel" publicKeyToken="b77a5c561934e089" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> <!-- 针对其他System.ServiceModel.*组件添加类似重定向 --> <dependentAssembly> <assemblyIdentity name="System.ServiceModel.Web" publicKeyToken="b77a5c561934e089" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
3. 清理并重建项目
- 删除项目的
bin和obj文件夹,清除残留旧程序集 - 执行
dotnet clean和dotnet build命令,确保项目基于新依赖编译
4. 检查WCF客户端代码兼容性
升级后部分WCF API存在变更,需检查客户端代码:
- 确认
ChannelFactory、ClientBase等类的初始化方式符合.NET 6规范 - 替换已过时API,例如HTTP绑定场景下调整为适配.NET 6的客户端配置
5. 验证项目目标框架
确保项目文件中已正确设置目标框架为.NET 6:
<PropertyGroup> <TargetFramework>net6.0</TargetFramework> </PropertyGroup>
内容的提问来源于stack exchange,提问作者user007
相关产品推荐
相关产品推荐

