2011-08-23 81 views
2

我使用XSD2Code從給定的XSD生成C#代碼。該工具生成的代碼剪斷如下:WCF xmlSerializer和數據合同屬性

[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;} 
    } 

Coud有人請指導我用以下queires,請

  1. 如果我離開了上面的代碼,因爲它是不WCF序列化上面的類? 目前我得到的WCF測試客戶端上這樣的錯誤:「」此操作不 在WCF測試客戶端支持」

  2. 我是否需要添加DataContract和DataMember上述生成的代碼的頂部

  3. 哪個選項是DataContract串行器之間尤爲明顯VS XML序列 謝謝你。

回答

3
  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(); 
    } 
} 
+0

謝謝@carlosfigueira您的有用答案。 –