如何在BizTalk 2016的WCF-Custom发送端口添加Content-Type头
BizTalk 2016 WCF-Custom发送端口Content-Type头不生效问题解决
我们使用BizTalk 2016配置WCF-Custom发送端口发送SOAP消息,需要设置的Content-Type为:
Content-Type: application/soap+xml; action="actioname"; charset=UTF-8
尝试用以下代码添加头,但出站请求中未出现该Content-Type:
string headerString = "Content-Type: application/soap+xml; action=\"actioname\"; charset=UTF-8"; pInMsg.Context.Write("HttpHeaders", "http://schemas.microsoft.com/BizTalk/2006/01/Adapters/WCF-properties", headerString); pInMsg.Context.Promote("IsDynamicSend", "http://schemas.microsoft.com/BizTalk/2003/system-properties", true);
问题原因
WCF-Custom适配器会根据端口绑定类型自动生成并管理Content-Type头,直接通过HttpHeaders写入的内容会被适配器覆盖,尤其是使用wsHttpBinding、basicHttpBinding等标准SOAP绑定时,适配器会强制使用对应SOAP版本的默认Content-Type。
解决方案
方案1:使用自定义WCF行为扩展
通过创建IClientMessageInspector实现类,在出站消息发送前修改Content-Type头,这是最可靠的方式:
public class CustomContentTypeBehavior : IClientMessageInspector { public void AfterReceiveReply(ref Message reply, object correlationState) { // 无需处理响应,留空即可 } public object BeforeSendRequest(ref Message request, IClientChannel channel) { HttpRequestMessageProperty httpRequestProp; // 获取或创建HTTP请求属性 if (!request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out var propObj)) { httpRequestProp = new HttpRequestMessageProperty(); request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestProp); } else { httpRequestProp = (HttpRequestMessageProperty)propObj; } // 设置目标Content-Type httpRequestProp.Headers["Content-Type"] = "application/soap+xml; action=\"actioname\"; charset=UTF-8"; return null; } }
将该行为扩展编译为程序集后,在BizTalk管理控制台的WCF-Custom端口配置中,进入行为选项卡,添加自定义行为并关联这个扩展。
方案2:调整上下文属性设置(动态发送场景)
如果使用动态发送端口,需修改上下文属性的写入方式:
- 不要通过
HttpHeaders写入完整的Content-Type行,直接使用Content-Type属性写入值(去掉前缀) - 确保端口绑定为
customBinding,避免适配器自动覆盖头信息
修改后的代码:
// 直接设置Content-Type属性的值,无需前缀 string contentTypeValue = "application/soap+xml; action=\"actioname\"; charset=UTF-8"; pInMsg.Context.Write("Content-Type", "http://schemas.microsoft.com/BizTalk/2006/01/Adapters/WCF-properties", contentTypeValue); pInMsg.Context.Promote("IsDynamicSend", "http://schemas.microsoft.com/BizTalk/2003/system-properties", true);
同时,需在动态端口的TransportTypeData属性中配置customBinding,确保编码器允许自定义Content-Type。
注意事项
- 静态WCF-Custom端口可直接在消息头配置页添加Content-Type头,优先级高于上下文属性设置
- 若使用标准SOAP绑定,需切换为
customBinding并配置textMessageEncoding,设置messageVersion为Soap12WSAddressing10以匹配application/soap+xml类型
内容的提问来源于stack exchange,提问作者mit
相关产品推荐
相关产品推荐

