简体   繁体   English

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

[英]Can't deserialize XML in WCF REST service

I've just started playing with the REST starter kit, and I've hit a road block trying to build my own service. 我刚刚开始使用REST入门工具包,并且遇到了构建自己的服务的障碍。 I'm trying to create a service for account management, and I can't get the service to serialize my objects, throwing the following error: 我正在尝试创建用于帐户管理的服务,但无法获得该服务来序列化我的对象,并引发以下错误:

Unable to deserialize XML body with root name 'CreateAccount' and root namespace '' (for operation 'CreateAccount' and contract ('Service', ' http://tempuri.org/ ')) using DataContractSerializer. 无法使用DataContractSerializer反序列化具有根名称'CreateAccount'和根名称空间'(对于操作'CreateAccount'和合同('Service',' http://tempuri.org/ ')的XML正文)。 Ensure that the type corresponding to the XML is added to the known types collection of the service. 确保将与XML对应的类型添加到服务的已知类型集合中。

Here's the actual code for the service (based off of the 'DoWork' method that came with the project): 这是该服务的实际代码(基于项目随附的'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
        };
    }
}

And last, but not least, here's the object that's causing all the trouble: 最后但并非最不重要的是,这是引起所有麻烦的对象:

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; }
}

Am I missing anything stupid? 我想念任何愚蠢的东西吗?

Thanks in advance! 提前致谢!

It turns out I was missing an extra value in the [DataContract] attribute on the business object. 原来,我在业务对象的[DataContract]属性中缺少一个额外的值。

Should be [DataContract(Namespace = "")] 应该是[DataContract(Namespace = "")]

It appears the problem is a namespace clash between your method name "CreateAccount" and your input type "CreateAccount". 看来问题出在方法名称“ CreateAccount”和输入类型“ CreateAccount”之间的命名空间冲突。

Also, you have to mark your CreateAccount type as a DataContract like so: 同样,您必须将CreateAccount类型标记为DataContract,如下所示:

[DataContract]
public CreateAccount
{
    [DataMember]
    public string LastName { get; set; }

    ...
}

If you want to keep the same name, you can specify a namespace for the CreateAccount class. 如果要保持相同的名称,可以为CreateAccount类指定一个名称空间。

I noticed you have a return type as well. 我注意到您也有返回类型。 Ensure the return type is marked with the DataContract attribute as well. 确保返回类型也标记有DataContract属性。 Also, specify the return format like so: 另外,指定返回格式,如下所示:

ResponseFormat = WebMessageFormat.Xml

如果您还没有的话,我认为是CreatAccount类上方的[DataContract]属性。

I had a similar problem, but I did have the DataContract attribute. 我有一个类似的问题,但是我确实有DataContract属性。 What I was missing though was the xmlns="http://uri.org" attribute from the root element when trying to read the xml back into the object. 我所缺少的是当尝试将xml重新读回对象时,根元素的xmlns =“ http://uri.org”属性。
eg 例如

<Root_Element xmlns="http://uri.org"><Child_Element/>...</Root_Element>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM