WCF xmlSerializer 和数据协定属性

Nil*_*Pun 2 wcf

我使用 XSD2Code 从给定的 XSD 生成 C# 代码。该工具生成的代码截图如下:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class Orders
    {

        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
        public int OrderID {get;set;}
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)]
        public string Description {get;set;}
     }
Run Code Online (Sandbox Code Playgroud)

Coud有人请指导我以下queires,请

  1. 如果我保留上面的代码,WCF 会序列化上面的类吗?目前,我在 wcf 测试客户端上收到此错误:“WCF 测试客户端不支持此操作”。

  2. 我需要在上面生成的代码之上添加 DataContract 和 DataMember 吗?

  3. DataContract Serializer 与 XML Serializer 之间哪个选项更好 谢谢。

car*_*ira 5

  1. 它应该可以工作,只要合同(服务合同接口或使用该类型的操作合同方法)标有[XmlSerializerFormat]属性
  2. 否 - 如果您使用 装饰合同[XmlSerializerFormat],则忽略 DataContract/DataMember 属性(使用 XML 序列化属性,如此类中的属性)
  3. XmlSerializer让您完全控制该类型序列化的XML; 在DataContractSerializer(DCS)不(它更多的限制)。但是DCS有更好的性能,所以哪个更好真的取决于你的场景。

下面的代码显示了使用这种类型的服务,即使使用 WcfTestClient,它也能正常工作。

public class StackOverflow_7155154
{
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class Orders
    {
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
        public int OrderID { get; set; }
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)]
        public string Description { get; set; }
    }
    [ServiceContract]
    [XmlSerializerFormat]
    public interface ITest
    {
        [OperationContract]
        Orders GetOrders();
    }
    public class Service : ITest
    {
        public Orders GetOrders()
        {
            return new Orders { Description = "My order", OrderID = 1 };
        }
    }
    public static void Test()
    {
        //MemoryStream ms = new MemoryStream();
        //XmlSerializer xs = new XmlSerializer(typeof(Orders));
        //Orders o = new Orders { OrderID = 1, Description = "My order" };
        //xs.Serialize(ms, o);
        //Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));

        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
        host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
        host.Open();
        Console.WriteLine("Host opened");

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

        Console.WriteLine(proxy.GetOrders());

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

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
Run Code Online (Sandbox Code Playgroud)