REST与多态DataContracts - 反序列化失败

时间:2011-10-05 20:42:53

标签: c# .net wcf rest datacontract

这一直让我疯狂,因为我没有做任何改变,但我发誓这是按照我昨天的预期方式工作。

我使用以下合同定义了WCF 4 REST服务:

[ServiceContract]
public interface IPhoneFeaturesManagementHost
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/accounts/{accountNumber}/phoneNumbers/{phoneNumber}/features/{featureType}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    void UpdateFeatureStatus(string accountNumber, string phoneNumber, string featureType, FeatureUpdateRequest updateRequest);
}

我还定义了以下类型:

[DataContract]
[KnownType(typeof(One900FeatureUpdateRequest))]
public abstract class FeatureUpdateRequest
{
    [DataMember]
    public FeatureStatus Status { get; set; }

    [DataMember]
    public DateTime EffectiveDate { get; set; }

    public string AccountNumber { get; set; }
    public string PhoneNumber { get; set; }
    public string UserId { get; set; }
    public DateTime Timestamp { get; set; }

    public override string ToString()
    {
        return String.Format("Status: {0}, Effective Date: {1}", Status, EffectiveDate);
    }
}

[DataContract]
public class One900FeatureUpdateRequest : FeatureUpdateRequest
{
    [DataMember]
    public bool PerformSwitchUpdate { get; set; }
}

昨天我发誓我能够提交此表格的POST数据:

<One900FeatureUpdateRequest>
  <EffectiveDate>1999-05-31T11:20:00</EffectiveDate>
  <Status>Enabled</Status>
  <PerformSwitchUpdate>true</PerformSwitchUpdate>
</One900FeatureUpdateRequest>

今天,同一批XML导致HTTP 400错误,并显示以下消息:

Unable to deserialize XML body with root name 'One900FeatureUpdateRequest' and root namespace '' (for operation 'UpdateFeatureStatus' and contract ('IPhoneFeaturesManagementHost',  'http://tempuri.org/')) using DataContractSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.

今天似乎唯一可用的XML如下所示,我真的不喜欢添加名称空间加属性来描述我的子DataContract的子类型。

<FeatureUpdateRequest i:type="One900FeatureUpdateRequest" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Project.Services.Host">
  <EffectiveDate>1999-05-31T11:20:00</EffectiveDate>
  <Status>Enabled</Status>
  <PerformSwitchUpdate>true</PerformSwitchUpdate>
</FeatureUpdateRequest>

有没有人对我可能触及的内容或我可能需要更改的内容有任何想法,以便回到之前的简单XML格式进行反序列化?

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

我找不到使用DataContractSerializer的方法,所以我转而使用旧的XmlSerializer,这样我就可以直接控制XML格式了。这似乎有效。