【问题标题】:Naming generic collection in WCF在 WCF 中命名泛型集合
【发布时间】:2012-04-23 16:34:05
【问题描述】:

我正在使用从自定义语言生成 WCF 服务的第三方框架。 但是,当使用集合类时,这是生成的输出:

namespace MyNameSpace
{
    using System;
    using System.ServiceModel;

    [MessageContract]
    public class FindSomethingResponse
    {
        [MessageBodyMember(Order=1)]
        public System.Collections.Generic.List<SomethingDC> response;
    }
}

这很好,但在使用服务时会导致一些不希望的结果。这是上面生成的 XSD:

<FindSomethingResponse>
    <ArrayOfSomethingDC>
        <SomethingDC/>
        <SomethingDC/>
        <SomethingDC/>
        ...
    </ArrayOfSomethingDC>
</FindSomethingResponse

“组节点”称为 ArrayOfSomethingDC,但我宁愿将其称为更有意义的名称(例如“Somethings”)。

据我所知,我必须使用 CollectionDataContract 属性来命名节点。但是,我处于无法真正更改生成类的结构的位置(因为它是在第三方框架中完成的),但我只能编辑上述方法。

有没有可能?

【问题讨论】:

  • 以上方法可以编辑什么?
  • 你试过返回IEnumerable&lt;SomethingDC&gt;吗,更正确的名字肯定是SomethingDCs

标签: c# wcf collections named


【解决方案1】:

尝试如下

   namespace MyNameSpace
    {
        using System;
        using System.ServiceModel;

        [CollectionDataContract(Name = "Somethings", ItemName = "SomethingDC")]
        public class CustomList<T> : List<T>
        {
            public CustomList()
                : base()
            {
            }

            public CustomList(T[] items)
                : base()
            {
                foreach (T item in items)
                {
                    Add(item);
                }
            }
        }


        [MessageContract]
        public class FindSomethingResponse
        {
            [MessageBodyMember(Order = 1)]
            public CustomList<SomethingDC> response;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    相关资源
    最近更新 更多