WCF& IXmlSerializable:根元素在响应中被替换

时间:2011-10-20 13:27:34

标签: wcf serialization

我们的“协议”有自定义XML序列化:

[XmlRoot("axf", Namespace = Axf10Namespace)]
public class AxfDocument : IXmlSerializable
{
  public const string Axf10Namespace = "http://schemas.***.ru/axf/axf-1.0.0";
  // ...
}

使用标准.NET XmlSerializer时一切正常:

<?xml version="1.0" encoding="utf-16"?>
<axf version="1.0.0" createdAt="2011-10-20T13:11:40" xmlns="http://schemas.***.ru/axf/axf-1.0.0">
  <itineraries>
    <!-- -->
   </itineraries>
</axf>

现在我们尝试在一个简单的WCF服务中使用这个类:

[OperationContract]
AxfDocument GetItineraries(ItinerariesQuery query);

发送到客户端的实际XML文档是:

<GetItinerariesResult version="1.0.0" createdAt="2011-10-20T13:17:50" xmlns="http://tempuri.org/">
  <itineraries xmlns="http://schemas.***.ru/axf/axf-1.0.0">
    <!-- rest is fine, serialization code does work -->

如何弯曲WCF以按原样发送根元素,而不是将其替换为自己的?

1 个答案:

答案 0 :(得分:1)

默认情况下,操作响应包含在操作名称中。但是,您可以在操作定义中使用MessageContract来使用“未包装”响应,如下所示。如果你在Fiddler中查看请求的响应主体,你会发现它与序列化中的响应主体完全相同。

public class StackOverflow_7836645
{
    [XmlRoot("axf", Namespace = Axf10Namespace)]
    public class AxfDocument : IXmlSerializable
    {
        public const string Axf10Namespace = "http://schemas.something.ru/axf/axf-1.0.0";

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("intineraries", Axf10Namespace);
            writer.WriteElementString("item", Axf10Namespace, "one value");
            writer.WriteElementString("item", Axf10Namespace, "another value");
            writer.WriteEndElement();
        }
    }

    [MessageContract(IsWrapped = false)]
    public class OperationResponse
    {
        [MessageBodyMember(Name = "axf", Namespace = AxfDocument.Axf10Namespace)]
        public AxfDocument axf;
    }

    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        OperationResponse GetAxf();
    }

    public class Service : ITest
    {
        public OperationResponse GetAxf()
        {
            return new OperationResponse { axf = new AxfDocument() };
        }
    }

    public static void Test()
    {
        Console.WriteLine("Serialization");
        MemoryStream ms = new MemoryStream();
        XmlSerializer xs = new XmlSerializer(typeof(AxfDocument));
        xs.Serialize(ms, new AxfDocument());
        Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));

        Console.WriteLine();
        Console.WriteLine("Service");

        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();
        Console.WriteLine(proxy.GetAxf());

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}