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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/966929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:25:15  来源:igfitidea点击:

Can't deserialize XML in WCF REST service

c#xmlwcfrestxml-serialization

提问by Joel.Cogley

I've just started playing with the REST starter kit, and I've hit a road block trying to build my own service. 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:

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

无法使用 DataContractSerializer反序列化具有根名称“CreateAccount”和根命名空间“”(用于操作“CreateAccount”和合同(“Service”,“ http://tempuri.org/”))的XML 正文。确保将 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!

提前致谢!

采纳答案by Doanair

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. Also, specify the return format like so:

我注意到你也有一个返回类型。确保返回类型也用 DataContract 属性标记。另外,像这样指定返回格式:

ResponseFormat = WebMessageFormat.Xml

回答by PaulWarren

If you don't have it already, I think a [DataContract] attribute above your CreatAccount class.

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

回答by Joel.Cogley

It turns out I was missing an extra value in the [DataContract]attribute on the business object.

结果我[DataContract]在业务对象的属性中遗漏了一个额外的值。

Should be [DataContract(Namespace = "")]

应该 [DataContract(Namespace = "")]

回答by Anthony K

I had a similar problem, but I did have the DataContract attribute. 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.
e.g.

我有一个类似的问题,但我确实有 DataContract 属性。但是,当尝试将 xml 读回对象时,我缺少的是根元素中的 xmlns="http://uri.org" 属性。
例如

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