我創建了一個自定義的肥皂頭,並通過IClientMessageInspector如何,我得到一個定製的肥皂頭的值在WCF
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
var header = new MessageHeader<AuthHeader>();
header.Content = new AuthHeader(Key);
header.Actor = "Anyone";
var header2 = header.GetUntypedHeader("Auth", "xWow");
request.Headers.Add(header2);
return null;
}
[DataContract(Name="Auth")]
public class AuthHeader
{
public AuthHeader(string key)
{
this.Key = key;
}
[DataMember]
public string Key { get; set; }
}
添加到我的消息,我也有一個IDispatchMessageInspector,我可以找到正確的標題在列表中。但是,沒有價值。我知道該值通過線路都是正確的,因爲消息字符串正確
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Auth s:actor="Anyone" xmlns="xWow" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Key xmlns="http://schemas.datacontract.org/2004/07/xWow.Lib">HERE IS MY KEY VALUE!!!!</Key>
</Auth>
<To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:26443/AuthService.svc</To>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IAuthService/GetPayload</Action>
</s:Header>
<s:Body>
<GetPayload xmlns="http://tempuri.org/"/>
</s:Body>
</s:Envelope>
但似乎沒有要任何財產來獲取這個值。 MessageHeaderInfo類有Actor等,但沒有其他有用的,我可以找到。
在客戶端,我不得不在Header和Untyped頭之間進行轉換,在服務器上是否存在相同的操作?
我發現了以下內容,應該可以工作。
request.Headers.FindHeader("Auth", "xWow");
request.Headers.GetHeader<AuthHeader>(index);
如果我手動找到正確的索引並調用第二行,它按預期工作。然而,FindHeader返回-1作爲索引,儘管我已在觀察窗口中確認這些名稱和名稱空間是正確的值。