[英]WCF Service - Getting list from request header
I'm trying to recover a list key-value from request header in a WCF service. 我正在尝试从WCF服务中的请求标头中恢复列表键值。 I'm able to recover a single property, but I can't find how to recover a list.
我可以恢复单个属性,但是找不到如何恢复列表。
This is my call: 这是我的电话:
<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>
And this is how I recover "header1": 这就是我恢复“ header1”的方法:
MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
string header1 = headers.GetHeader<string>("header1", "http://tempuri.org/");
I thought that I could use something like: 我认为我可以使用类似的方法:
IDictionary<string, string> properties = headers.GetHeader<Dictionary<string, string>>("Properties", "http://tempuri.org/");
But properties is always null. 但是属性始终为null。
As MSDN says the GetHeader<T>(string, string)
method only is capable of returning header that is serialized by DataContractSerializer
, which means your property
objects should exist as a .NET type. 正如MSDN所说,
GetHeader<T>(string, string)
方法仅能够返回由DataContractSerializer
序列化的标头,这意味着您的property
对象应以.NET类型存在。
What you can do instead is to use GetHeader<T>(string, string, XmlObjectSerializer)
which uses serializer to deserialize the content of the header value and return it. 相反,您可以使用
GetHeader<T>(string, string, XmlObjectSerializer)
,该方法使用序列化程序反序列化头值的内容并返回它。
For that you need to implement your own serializer that will read the content and return dictionary. 为此,您需要实现自己的序列化程序,该序列化程序将读取内容并返回字典。 I think something like that would work (depends on whether
key
and value
are strictly in this order or can be swapped). 我认为类似的方法会起作用(取决于
key
和value
是否严格按此顺序排列或可以交换)。 Also, I didn't check what happens with namespaces. 另外,我没有检查名称空间会发生什么。
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.