我正在開發一個使用 SOAP WCF WS-Addressing 訊息與遺留系統進行通信的客戶端。
此外,要求其定制SOAP信封標頭To,并Action包含自定義資訊頭。
我能夠通過使用下面的代碼所示的資訊來設定To和ActionSOAP-Envelope 標頭OperationContextScope:
public async Task<getAttorneyResponseStructure> GetAttorneyAsync(GetAttorneyRequestStructure getAttorneyRequestStructure)
{
try
{
using (new OperationContextScope(Client.InnerChannel))
{
getAttorneyRequestStructure.AttorneyHeader = Header;
OperationContext.Current.OutgoingMessageHeaders.To = new Uri("http://rydwvgsn01.spga.gov.sa/GSBExpress/Legal/MOJAttorneyInquiry/2.0/AttorneyInquiryService.svc");
OperationContext.Current.OutgoingMessageHeaders.Action = "http://tempuri.org/IAttorneyInquiryService/GetAttorney";
return await Client.GetAttorneyAsync(getAttorneyRequestStructure);
}
}
catch (Exception e)
{
throw;
}
}
當我運行代碼并嘗試發送訊息時,我最終遇到了例外 Multiple headers with name 'Action' and namespace 'http://schemas.microsoft.com/ws/2005/05/addressing/none' found.
通過查看圖片中附加的例外堆疊,似乎有一個物件包含與我要添加的標題相同的資訊。

所以,我的問題是有沒有解決更改Action標題的命名空間或修改Action包含設定命名空間的現有內容的方法?
uj5u.com熱心網友回復:
解決了!事實證明,我使用的問題是使用BasicHttpBinding與服務器不同的不同soap 版本的默認值。此外,Action不需要標頭中的屬性,因為我CustomBinding在連接建構式中使用如下更改了 SOAP 版本:
CustomBinding binding = new CustomBinding();
var mtomMessageEncodingBindingElement = new MtomMessageEncodingBindingElement
{
MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap11, AddressingVersion.WSAddressing10),
};
binding.Elements.Add(mtomMessageEncodingBindingElement);
var httpTransportBindingElement = new HttpTransportBindingElement();
binding.Elements.Add(httpTransportBindingElement);
Client = new AttorneyInquiryServiceClient(binding, new EndpointAddress("http://rydwvgsn01.spga.gov.sa/GSBExpress/Legal/MOJAttorneyInquiry/2.0/AttorneyInquiryService.svc"));
對上面代碼的一些額外解釋:
MtomMessageEncodingBindingElement: 用于能夠設定 SOAP 版本的版本并啟用 MOTM 型別的回應。HttpTransportBindingElement: 在系結中需要。在方法呼叫中,
Action被移除。
public async Task<getAttorneyResponseStructure> GetAttorneyAsync(GetAttorneyRequestStructure getAttorneyRequestStructure)
{
try
{
using (new OperationContextScope(Client.InnerChannel))
{
getAttorneyRequestStructure.AttorneyHeader = Header;
OperationContext.Current.OutgoingMessageHeaders.To = new Uri("http://rydwvgsn01.spga.gov.sa/GSBExpress/Legal/MOJAttorneyInquiry/2.0/AttorneyInquiryService.svc");
return await Client.GetAttorneyAsync(getAttorneyRequestStructure);
}
}
catch (Exception e)
{
throw;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/362464.html
上一篇:由于通用鏈接存盤拒絕
