CoreWCF中TransportBindingElement实例创建及.NET4.7.2代码迁移方案
迁移.NET 4.7.2 WCF代码到CoreWCF(netstandard2.0)的解决方案
核心替代类型说明
CoreWCF中提供了对应原System.ServiceModel.Channels.HttpsTransportBindingElement的类型CoreWCF.Channels.HttpsTransportBindingElement,需先确保项目已安装CoreWCF.Http NuGet包——这是CoreWCF实现HTTP/HTTPS传输绑定的核心依赖包。
迁移后的代码实现
替换原代码的命名空间引用,并调整CoreWCF中已变更的属性配置:
using CoreWCF.Channels; using System.Security.Authentication.ExtendedProtection; using System.Net; TransportBindingElement bindingElement = new HttpsTransportBindingElement { RequireClientCertificate = true, AllowCookies = false, AuthenticationScheme = AuthenticationSchemes.Anonymous, BypassProxyOnLocal = false, DecompressionEnabled = true, // CoreWCF简化了ExtendedProtectionPolicy的构造参数,若需原场景逻辑可按需调整 ExtendedProtectionPolicy = new ExtendedProtectionPolicy(PolicyEnforcement.Always), HostNameComparisonMode = HostNameComparisonMode.StrongWildcard, KeepAliveEnabled = true, ManualAddressing = false, MaxBufferPoolSize = MaxBufferPoolSize, MaxBufferSize = MaxBufferSize, MaxReceivedMessageSize = MaxReceivedMessageSize, ProxyAuthenticationScheme = AuthenticationSchemes.Anonymous, TransferMode = TransferMode.Buffered, UseDefaultWebProxy = true };
关键差异点处理
- 命名空间替换:将所有
System.ServiceModel.Channels下的类型替换为CoreWCF.Channels - ExtendedProtectionPolicy构造:CoreWCF移除了原构造函数中的
ProtectionScenario参数,若需保留特定保护场景,可通过类的其他属性补充配置,或根据实际需求简化为仅指定策略强制执行模式 - MaxPendingAccepts属性:该属性已从
TransportBindingElement迁移至绑定的ListenerSettings中。若需设置此值,需通过对应绑定的配置对象操作,示例:var httpBinding = new HttpBinding(); httpBinding.ListenerSettings.MaxPendingAccepts = 0; - 依赖包安装:务必确保项目引用
CoreWCF.HttpNuGet包,若涉及WCF其他功能(如TCP传输),需额外安装对应CoreWCF包
内容的提问来源于stack exchange,提问作者Saeed Heidarbozorg
相关产品推荐
相关产品推荐

