我刚刚开始使用 REST 入门工具包,但在尝试构建自己的服务时遇到了障碍。我正在尝试创建一个帐户管理服务,但我无法获得序列化我的对象的服务,并抛出以下错误:
Unable to deserialize XML body with root name 'CreateAccount' and root namespace '' (for operation 'CreateAccount' and contract ('Service', 'http://tempuri.org/')) using DataContractSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.
这是服务的实际代码(基于项目附带的“DoWork”方法):
[WebHelp(Comment = "Creates a Membership account")]
[WebInvoke(UriTemplate = "CreateAccount", RequestFormat = WebMessageFormat.Xml)]
[OperationContract]
public ServiceResponse CreateAccount(CreateAccount request)
{
try
{
// do stuff
return new ServiceResponse()
{
Status = "SUCCESS",
ErrorMessage = ""
};
}
catch (Exception ex)
{
return new ServiceResponse()
{
Status = "ERROR",
ErrorMessage = ex.Message + "\n\n" + ex.StackTrace
};
}
}
最后但并非最不重要的一点是,这是造成所有麻烦的对象:
public class CreateAccount
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public bool SignUpForNewsletter { get; set; }
public string Password { get; set; }
}
我是不是遗漏了什么愚蠢的东西?
提前致谢!
最佳答案
事实证明,我在业务对象的 [DataContract]
属性中遗漏了一个额外的值。
应该是[DataContract(Namespace = "")]
关于c# - 无法在 WCF REST 服务中反序列化 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/966929/