简体   繁体   中英

WCF Service - Getting list from request header

I'm trying to recover a list key-value from request header in a WCF service. 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":

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.

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.

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.

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). 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();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM