我使用 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;}
}
请有人指导我以下问题
如果我保留上面的代码,WCF 会序列化上面的类吗? 目前我在 wcf 测试客户端上收到此错误:“”此操作不是 WCF 测试客户端支持”。
我需要在上述生成的代码之上添加 DataContract 和 DataMember 吗?
- DataContract Serializer 与 XML Serializer 哪个选项更好 谢谢你。
最佳答案
- 只要合约(使用此类型的服务合约接口(interface)或操作合约方法)使用
[XmlSerializerFormat]
属性进行标记,它就应该有效 - 否 - 如果使用
[XmlSerializerFormat]
修饰协定,则将忽略 DataContract/DataMember 属性(改为使用 XML 序列化属性,与此类型中的属性类似) 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();
}
}
关于WCF xmlSerializer 和数据协定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7155154/