具有多态数据契约的 REST - 反序列化失败
这让我一整天都发疯,因为我没有做出任何改变,但我发誓这正在按照我昨天的预期进行。
我有一个使用以下合同定义的 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 格式进行反序列化?
任何帮助将不胜感激。谢谢!
This has been driving me nuts all day, as I've made no changes yet I swear this was working the way I had intended yesterday.
I have a WCF 4 REST service defined with the following contract:
[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);
}
I also have the following types defined:
[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; }
}
Yesterday I swear I was able to submit POST data of this form:
<One900FeatureUpdateRequest>
<EffectiveDate>1999-05-31T11:20:00</EffectiveDate>
<Status>Enabled</Status>
<PerformSwitchUpdate>true</PerformSwitchUpdate>
</One900FeatureUpdateRequest>
Today that same batch of XML is causing HTTP 400 errors with the following message:
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.
The only XML that seems to work today is the below, and I really really dislike the necessity of adding a namespace plus attribute to describe the subtype of my child 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>
Does anyone have any ideas as to what I might have touched or what I might need to change in order to get back to the prior simple XML format for deserialization?
Any help would be much appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找不到使用 DataContractSerializer 执行此操作的方法,因此我改用旧的 XmlSerializer,以便可以直接控制 XML 格式。这似乎有效。
I couldn't find a way to do this with the DataContractSerializer, so I instead switched to the old XmlSerializer so I can have direct control over the XML format. This seemed to work.