服务代码:
[ServiceContract]
public interface IServices
{
[OperationContract]
[XmlSerializerFormat]
GetProductsResponse Getproducts(GetProductsRequest productsrequest);
[OperationContract]
SaveProductsResponse SaveProducts1(SaveProductsRequest1 productsrequest);
}
public class GetProductsRequest
{
[XMLElement]
public string id;
[XMLElement,typeof(Person)]
public Person[] Persons;
}
[DataContract]
public class SaveProductsRequest1
{
[DataMember]
public List<Person> Persons;
}
[DataContract]
public class Person
{
[DataMember]
public int Id;
[DataMember]
public string Name;
[DataMember]
public Sex Sex;
}
[DataContract]
public enum Sex
{
[EnumMember]
Male,
[EnumMember]
Female
}
客户 :
ServicesClient client = new ServicesClient();
SaveProductsRequest1 req = new SaveProductsRequest1();
req.Persons = new List<Person> { new Person { Id = 10, Name = "Al", Sex = Sex.Female} }.ToArray();
client.SaveProducts1(req);
我能够在服务端获得值“Al”(字符串)。但是值 10 和 Male(值类型)在服务端被重置为默认值(0 和 Male)。
这只发生在当我将 XMLElement 用于任何数组属性和一个附加字符串属性时,我在服务合同(在本例中为 GetProducts)和我的 GetProducts 类中为我的任何一种方法设置了 XmlSerializerFormat。
WCF 运行时有问题吗?