【问题标题】:WCF xmlSerializer and Data Contract AttributesWCF xmlSerializer 和数据协定属性
【发布时间】:2011-08-23 00:37:58
【问题描述】:

我使用 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;}
     }

请有人指导我以下问题

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

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

  3. DataContract 序列化程序与 XML 序列化程序之间哪个选项更好 谢谢你。

【问题讨论】:

    标签: wcf


    【解决方案1】:
    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();
        }
    }
    

    【讨论】:

    • 感谢@carlosfigueira 的有用回答。
    猜你喜欢
    • 1970-01-01
    • 2011-04-06
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    相关资源
    最近更新 更多