c# - 无法在 WCF REST 服务中反序列化 XML

标签 c# xml wcf rest xml-serialization

我刚刚开始使用 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/

相关文章:

c# - 如何在 mvc4 c# 中递减 nullable int by1?

c# - 检查列表是否包含字符串中的所有单词

c#继承麻烦。我在继承链中看不到方法

c++ - 用于数据存储的 Lua 与 XML

java - 在 Java 中查询 XML 的最简单方法

c# - 如何从 C# 向 sql 查询传递未知数量的参数?

java - 如何实现OSGI动态元类型?

WCF 服务 - 无 global.asax

c# - wsDualHttpBinding 总是超时,而其他服务按预期工作

wcf - Visual Studio 无法使用包含 UsernameToken 策略的 Web 服务