WCF 和 DataContractSerializer 序列化 CollectionDataContract 修饰的集合类型是否不同?

发布于 2024-10-05 02:12:09 字数 1305 浏览 9 评论 0原文

我有一个非常简单的自定义集合类型,它继承自 List<>并使用 CollectionDataContract。

当我使用 DataContractSerializer.WriteObject 对其进行序列化时,它按照我期望的方式尊重 CollectionDataContract 属性;但是,当我将它用作 WCF 方法的返回类型时,我得到默认的 ArrayOfFoo。

我想知道服务合同中是否缺少一些装饰。

详细信息:

[DataContract(Namespace = "")]
public class Foo
{
    [DataMember]
    public string BarString { get; set; }
}

[CollectionDataContract(Namespace = "")]
[Serializable]
public class FooList : List<Foo> {}

如果我只是实例化 Foo,然后使用 DataContractSerializer.WriteObject 对其进行序列化,我会得到您所期望的结果:

<FooList>
    <Foo>
        <BarString>myString1</BarString>
    </Foo>
</FooList>

但是,如果我有一个具有这样的方法的服务...

[ServiceContract Name = "MyService"]
public interface IMyService
{
    [OperationContract, WebGet(UriTemplate = "foos/")]
    FooList GetAllFoos();
}

然后对 http://www.someEndpoint.com/foos/,我明白了:

<ArrayOfFoo>
    <Foo>
        <BarString>myString1</BarString>
    </Foo>
</ArrayOfFoo>

我也尝试过指定 Name= CollectionDataContract 属性中的“MyFooListName”。结果相同:DataContractSerializer 获取备忘录; WCF 没有。

I have a really simple customized collection type that inherits from List<> and uses a CollectionDataContract.

When I use DataContractSerializer.WriteObject to serialize it, it respects the CollectionDataContract attribute the way I'd expect; however, when I use it as a return type for a WCF method, I get the default ArrayOfFoo.

I'm wondering if there is some decoration I'm missing in the service contract.

Details:

[DataContract(Namespace = "")]
public class Foo
{
    [DataMember]
    public string BarString { get; set; }
}

[CollectionDataContract(Namespace = "")]
[Serializable]
public class FooList : List<Foo> {}

If I just instantiate a Foo and then use DataContractSerializer.WriteObject to serialize it, I get what you'd expect:

<FooList>
    <Foo>
        <BarString>myString1</BarString>
    </Foo>
</FooList>

However, if I have a service with a method like this...

[ServiceContract Name = "MyService"]
public interface IMyService
{
    [OperationContract, WebGet(UriTemplate = "foos/")]
    FooList GetAllFoos();
}

and then do a GET for http://www.someEndpoint.com/foos/, I get this:

<ArrayOfFoo>
    <Foo>
        <BarString>myString1</BarString>
    </Foo>
</ArrayOfFoo>

I've also tried specifying Name="MyFooListName" in the CollectionDataContract attribute. Same results: DataContractSerializer gets the memo; WCF doesn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满地尘埃落定 2024-10-12 02:12:09

Saeed 给我指明了正确的方向:当我一直希望使用 DataContractSerializer 时,我无意中最终得到了 XmlSerializer。

我最终得到了 XmlSerializer...好吧...通过要求它。

特别是,我在服务中使用 XmlSerializerFormat 修饰了方法,如下所示:

[ServiceContract Name = "MyService"] 
public interface IMyService 
{ 
    //  ... other stuff ...

    [OperationContract, WebInvoke(UriTemplate = "foos/", Method = "POST")] 
    [XmlSerializerFormat]
    Foo PostAFoo(Foo yourNewFoo);
} 

我这样做是希望能够在手工滚动的 Foo XML blob 中宽恕成员顺序。当然,当这样做时,最终会得到 XmlSerializer,而不是 DataContractSerializer。

当我去掉 XmlSerializerFormat 属性时,问题就解决了:WCF 现在正在按照我想要的方式序列化我的 FooList 集合。

Saeed sent me in the right direction: I inadvertently ended up with XmlSerializer, when I had been hoping for DataContractSerializer.

I had ended up with XmlSerializer... well... by asking for it.

In particular, I had decorated methods in my service with the XmlSerializerFormat like this:

[ServiceContract Name = "MyService"] 
public interface IMyService 
{ 
    //  ... other stuff ...

    [OperationContract, WebInvoke(UriTemplate = "foos/", Method = "POST")] 
    [XmlSerializerFormat]
    Foo PostAFoo(Foo yourNewFoo);
} 

I had done this in the hopes of forgiving member order in hand-rolled Foo XML blobs. Of course, when one does this one ends up with XmlSerializer, not DataContractSerializer.

When I take away the XmlSerializerFormat attribute, problem solved: WCF is now serializing my FooList collection the way I want.

凉月流沐 2024-10-12 02:12:09

有关详细信息,请参阅 MSDN

DataContractSerializer 不
支持使用的编程模型
XmlSerializer 和 ASP.NET Web
服务。特别是,它不
支持属性,如
XmlElementAttribute 和
XmlAttribute属性。启用
支持这种编程模型,
必须切换 WCF 才能使用
XmlSerializer 而不是
DataContractSerializer。

因此序列化将由 XMLSerializer 完成,并且您无法更改它。

See MSDN for detail:

The DataContractSerializer does not
support the programming model used by
the XmlSerializer and ASP.NET Web
services. In particular, it does not
support attributes like
XmlElementAttribute and
XmlAttributeAttribute. To enable
support for this programming model,
WCF must be switched to use the
XmlSerializer instead of the
DataContractSerializer.

So the serialization going to be done by XMLSerializer, and you can't change it.

救星 2024-10-12 02:12:09

您在配置 WCF 服务时是否选择了通用类型?如果没有的话,

右键进入配置,然后选择Generic Type,默认是arraylist类型。

Have you selected Generic types while configuring your WCF service? if not then,

right click and go to configuration, then select Generic Type, by default it is arraylist type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文