[英]WCF Service - Getting list from request header
我正在嘗試從WCF服務中的請求標頭中恢復列表鍵值。 我可以恢復單個屬性,但是找不到如何恢復列表。
這是我的電話:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header>
<tem:header1>Header 1</tem:header1>
<tem:Properties>
<tem:property>
<key>KEY.ONE</key>
<value>value1</value>
</tem:property>
<tem:property>
<key>KEY.TWO</key>
<value>value2</value>
</tem:property>
</tem:Properties>
</soapenv:Header>
<soapenv:Body>
<tem:function1>
<tem:param1>100</tem:param1>
</tem:function1>
</soapenv:Body>
</soapenv:Envelope>
這就是我恢復“ header1”的方法:
MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
string header1 = headers.GetHeader<string>("header1", "http://tempuri.org/");
我認為我可以使用類似的方法:
IDictionary<string, string> properties = headers.GetHeader<Dictionary<string, string>>("Properties", "http://tempuri.org/");
但是屬性始終為null。
正如MSDN所說, GetHeader<T>(string, string)
方法僅能夠返回由DataContractSerializer
序列化的標頭,這意味着您的property
對象應以.NET類型存在。
相反,您可以使用GetHeader<T>(string, string, XmlObjectSerializer)
,該方法使用序列化程序反序列化頭值的內容並返回它。
為此,您需要實現自己的序列化程序,該序列化程序將讀取內容並返回字典。 我認為類似的方法會起作用(取決於key
和value
是否嚴格按此順序排列或可以交換)。 另外,我沒有檢查名稱空間會發生什么。
public class MySerializer : XmlObjectSerializer
{
public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
{
reader.ReadFullStartElement();
Dictionary<string, string> r = new Dictionary<string, string>();
while (reader.IsStartElement("property"))
{
reader.ReadFullStartElement("property");
reader.ReadFullStartElement("key");
string key = reader.ReadContentAsString();
reader.ReadEndElement();
reader.ReadFullStartElement("value");
string value = reader.ReadContentAsString();
reader.ReadEndElement();
r.Add(key, value);
reader.ReadEndElement();
reader.MoveToContent();
}
return r;
}
public override void WriteStartObject(XmlDictionaryWriter writer, object graph)
{
throw new NotImplementedException();
}
public override void WriteObjectContent(XmlDictionaryWriter writer, object graph)
{
throw new NotImplementedException();
}
public override void WriteEndObject(XmlDictionaryWriter writer)
{
throw new NotImplementedException();
}
public override bool IsStartObject(XmlDictionaryReader reader)
{
throw new NotImplementedException();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.