Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net WCF Rest服务未知属性类型列表_.net_Wcf_List_Object_Xml Deserialization - Fatal编程技术网

.net WCF Rest服务未知属性类型列表

.net WCF Rest服务未知属性类型列表,.net,wcf,list,object,xml-deserialization,.net,Wcf,List,Object,Xml Deserialization,我使用的REST服务总是返回ServiceResult类型的对象。WCF的酷之处在于xml反序列化是自动完成的。以下是两个运营合同示例: [ServiceContract] [XmlSerializerFormat] public interface IMyService { [OperationContract] [WebGet( BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = Web

我使用的REST服务总是返回ServiceResult类型的对象。WCF的酷之处在于xml反序列化是自动完成的。以下是两个运营合同示例:

[ServiceContract]
[XmlSerializerFormat]
public interface IMyService
{
    [OperationContract]
    [WebGet(
        BodyStyle = WebMessageBodyStyle.Bare,
        ResponseFormat = WebMessageFormat.Xml,
        UriTemplate = "authenticate?api={apiKey}&userName={userName}&password={password}")]
    ServiceResult Authenticate(string apiKey, string userName, string password);

    [OperationContract]
    [WebGet(
        BodyStyle = WebMessageBodyStyle.Bare,
        ResponseFormat = WebMessageFormat.Xml,
        UriTemplate = "getSchedules?api={apiKey}&user={userAccessCode}&states={states}&types={types}&filter={filter}")]
    ServiceResult GetSchedules(string apiKey, string userAccessCode, string states, string types, string filter);

}
ServiceResult对象包含两个元素:State一个简单的字符串和Result,Result是一个对象(所以它可以是任何东西)。如果我使用object作为其类型,在反序列化之后,我将得到一个XmlNodes列表。因此,我创建了一个抽象类结果,作为所有预期类型的超类:

[Serializable()]
[XmlRoot("ServiceResult", Namespace = "http://schemas.datacontract.org/2004/07/DTOModel.OpenApi")]
public class ServiceResult
{
    [XmlElement("Result")]
    public Result Result { get; set; }
    [XmlElement("State")]
    public string State { get; set; }
}

这非常有效,我可以在反序列化后将超类转换为正确的子类。但我的问题是:结果也可以是一个数组,我无法创建一个从结果继承的类(使用CollectionDataContract),它是一个数组(或列表)

有没有解决办法,或者我是不是看错了


谢谢

您能用数组元素创建result的子类吗?这意味着数组中有一个换行元素,但在你的例子中应该可以使用。我已经尝试过了,但是我该如何注释它呢?好的,这很有效!我不知道多个元素会自动转换为一个数组。谢谢。
[XmlInclude(typeof(UserDto)), XmlInclude(typeof(ScheduleDto))]
[XmlType(Namespace = "http://schemas.datacontract.org/2004/07/DTOModel.Management")]
public abstract class Result
{
}