WCF 通用结果 class 始终为空
WCF Generic result class always is empty
我的服务器响应有这个通用 class:
[DataContract]
public class GenericResult<T>
{
public List<T> ListResult { get; set; }
public T Result { get; set; }
public string Message { get; set; }
}
并将此 GetAllBrandsTest 方法 return 数据发送给客户端:
public async Task<GenericResult<Brand>> GetAllBrandsTest()
{
var result = await repo.GetAllAsync<Brand>();
return new GenericResult<Brand>()
{
ListResult = result.ToList(),
Message = "Success"
};
}
使用此方法对应的 GetAllBrands 一切正常:
public async Task<IList<Brand>> GetAllBrands()
{
return await repo.GetAllAsync<Brand>();
}
但是当我调用 GetAllBrandsTest 时,结果是空的。
[DataContract]
[KnownType(typeof(Brand))]
public class GenericResult<T>
{
[DataMember]
public List<Brand> ListResult { get; set; }
[DataMember]
public Brand Result { get; set; }
[DataMember]
public string Message { get; set; }
}
在服务器端和客户端之间传输的任何数据类型都应该明确指定我们如何序列化和反序列化它。请使用 DataContract
属性指定数据结构如何序列化为 XML ,以便序列化和反序列化可以在服务端和客户端之间正常工作。另外,对于未知的数据类型,请使用KnownType
特性提前指定序列化方式。
[DataContract]
[KnownType(typeof(CircleType))]
[KnownType(typeof(TriangleType))]
public class CompanyLogo2
[DataMember]
private Shape ShapeOfLogo;
[DataMember]
private int ColorOfLogo;
}
[DataContract]
public class Shape { }
[DataContract(Name = "Circle")]
public class CircleType : Shape { }
[DataContract(Name = "Triangle")]
public class TriangleType : Shape { }
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-contract-known-types
我们要么用 DataContract
属性和 DataMember
属性修饰 class,要么删除 DataContract
属性和 DataMember
属性。因为当复杂数据类型没有指定任何XML序列化器时,默认使用DataContract
序列化器。
我的服务器响应有这个通用 class:
[DataContract]
public class GenericResult<T>
{
public List<T> ListResult { get; set; }
public T Result { get; set; }
public string Message { get; set; }
}
并将此 GetAllBrandsTest 方法 return 数据发送给客户端:
public async Task<GenericResult<Brand>> GetAllBrandsTest()
{
var result = await repo.GetAllAsync<Brand>();
return new GenericResult<Brand>()
{
ListResult = result.ToList(),
Message = "Success"
};
}
使用此方法对应的 GetAllBrands 一切正常:
public async Task<IList<Brand>> GetAllBrands()
{
return await repo.GetAllAsync<Brand>();
}
但是当我调用 GetAllBrandsTest 时,结果是空的。
[DataContract]
[KnownType(typeof(Brand))]
public class GenericResult<T>
{
[DataMember]
public List<Brand> ListResult { get; set; }
[DataMember]
public Brand Result { get; set; }
[DataMember]
public string Message { get; set; }
}
在服务器端和客户端之间传输的任何数据类型都应该明确指定我们如何序列化和反序列化它。请使用 DataContract
属性指定数据结构如何序列化为 XML ,以便序列化和反序列化可以在服务端和客户端之间正常工作。另外,对于未知的数据类型,请使用KnownType
特性提前指定序列化方式。
[DataContract]
[KnownType(typeof(CircleType))]
[KnownType(typeof(TriangleType))]
public class CompanyLogo2
[DataMember]
private Shape ShapeOfLogo;
[DataMember]
private int ColorOfLogo;
}
[DataContract]
public class Shape { }
[DataContract(Name = "Circle")]
public class CircleType : Shape { }
[DataContract(Name = "Triangle")]
public class TriangleType : Shape { }
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-contract-known-types
我们要么用 DataContract
属性和 DataMember
属性修饰 class,要么删除 DataContract
属性和 DataMember
属性。因为当复杂数据类型没有指定任何XML序列化器时,默认使用DataContract
序列化器。