我是 WCF 新手。我正在托管由第三方应用程序 (TPA) 调用的 WCF 服务。TPA 要求 wcf 服务响应具有自定义标头,并且响应肥皂信封应如下所示:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:stan="http://client/schema/framework/header/standardheaderresponse"
xmlns:sys="http://client/schema/framework/common/systemidentifier" >
<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<stan:StandardHeaderResponse>
<stan:From>
<sys:Identifier>BUS</sys:Identifier>
<sys:Name>BuildingEvent</sys:Name>
<sys:Version>1</sys:Version>
</stan:From>
<stan:Timestamp>2013-03-15T03:05:41.000</stan:Timestamp>
<stan:CorrelationID>uuid:c12c9e48-8164-4074-9c8c-2b979804dbd2</stan:CorrelationID>
</stan:StandardHeaderResponse>
<wsa:Action>
http://client/service.buildingloc.event.BuildingEvent.SendEvent
</wsa:Action>
<wsa:MessageID>uuid:ab4c4001-f089-721c-aae6-f51ec37d3501</wsa:MessageID>
<wsa:To>http://www.w3.org/2005/08/addressing/anonymous</wsa:To>
<wsa:RelatesTo>uuid:de9a7007-f056-431c-aae6-f51ec37d3488</wsa:RelatesTo>
</soap:Header>
<soap:Body>
…
</soap:Body>
</soap:Envelope>
我的服务使用自定义 wcf 绑定,如下所示:
var binding = new CustomBinding();
binding.Elements.Add( new TextMessageEncodingBindingElement() { MessageVersion = MessageVersion.Soap12WSAddressing10 } );
binding.Elements.Add( new HttpTransportBindingElement() );
要在我的响应中添加自定义肥皂标题,我正在使用 IDispatchMessageInspector,它看起来如下所示:
public void BeforeSendReply(ref Message reply, object correlationState)
{
reply.Headers.Add(new StandardHeaderResponse());
}
和 MessageHeader 如下:
[MessageContract]
public class StandardHeaderResponse : MessageHeader
{
#region Overrides of MessageHeaderInfo
public override string Name
{
get { return GetType().Name; }
}
public override string Namespace
{
get { return "client/schema/framework/header/standardheaderresponse"; }
}
#endregion
#region Overrides of MessageHeader
protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
{
writer.WriteElementString("Key", "Value");
}
#endregion
}
但是当我运行服务并从我的测试客户端发出请求时,我在 Fiddler 中得到以下输出
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:RelatesTo>urn:uuid:953e0b5b-44c6-4994-aae2-7710c31cb8f0</a:RelatesTo>
<StandardHeaderResponse>
<Key>Value</Key>
</StandardHeaderResponse>
</s:Header>
<s:Body>
...
</s:Body>
</s:Envelope>
这与 TPA 的预期完全不同。我需要做什么才能使提琴手输出看起来更像 TPA 所期望的。为什么不是所有的 WS-Address 元素都像<wsa:Action>
, <wsa:MessageID>
并被<wsa:RelatesTo>
呈现为标题的一部分。任何帮助将非常感激。