WCF服务返回数组而不是List

时间:2012-11-25 13:39:35

标签: arrays wcf list

我的客户端正在使用从WSDL创建的代理...是否可以将服务设置为发送List而不是MyCustomObject[]

我正在使用

svcutil /t:metadata /ct:System.Collections.Generic.List`1 localhost:8080/managedApp 

但不起作用......

我的客户端使用Visual C ++

IDE是Visual Studio 2012 - >无法添加服务参考

2 个答案:

答案 0 :(得分:2)

是的,是的。

/ collectionType:如果您直接使用svcutil.exe,或者“添加服务引用” 转到高级 - >集合键入并更改为您想要的任何内容。

为什么可能在使用Web服务/ WCF“服务”时总是使用您的终端 接收以XML / JSON序列化的数据,而不是将这些数据反序列化为C ++数据类型, 并且由您决定在哪种集合类型中反序列化包含某些集合的接收数据。

答案 1 :(得分:0)

好的......最后我放弃了,我只是将数据作为数组发送而不是std:list ...

我是这样做的: 1.-使用svcutil / t:metadata从服务中获取元数据(注意:服务必须运行)。 2.-创建代理wsutil * .wsdl * .xsd 3.-将代理文件(.h和.c)添加到我的客户端,并使用代理功能到服务。

如果您不熟悉c编程,那么数组有点棘手......

DataContract:

    [DataContract]
        public class CompositeType
        {
            CompositeType2[] ctListValue;
            bool boolValue;
            string stringValue;

            [DataMember]
            public bool BoolValue
            {
                get { return boolValue; }
                set { boolValue = value; }
            }

            [DataMember]
            public string StringValue
            {
                get { return stringValue; }
                set { stringValue = value; }
            }

            [DataMember]
            public CompositeType2[] CtListValue
            {
                get { return ctListValue; }
                set { ctListValue = value; }
            }
        }

        [DataContract]
        public class CompositeType2
        {
            bool boolValue;
            string stringValue;

            [DataMember]
            public bool BoolValue
            {
                get { return boolValue; }
                set { boolValue = value; }
            }

            [DataMember]
            public string StringValue
            {
                get { return stringValue; }
                set { stringValue = value; }
            }
        }

和我的客户端调用数组:

// *** COMPOSITETYPE CALL
    CompositeType * co = new CompositeType();
    co->BoolValue = true;
    co->StringValue = L"Im co";

    CompositeType2 co0;
    co0.BoolValue = true;
    co0.StringValue = L"Im co0";

    CompositeType2 co1;
    co1.BoolValue = true;
    co1.StringValue = L"Im co1";

    CompositeType2 co2;
    co2.BoolValue = true;
    co2.StringValue = L"Im co2";

    CompositeType2 ** comType2; // <-- this is my CompositeType2[] I will send to the service
    comType2 = new CompositeType2*[3];

    comType2[0] = &co0;
    comType2[1] = &co1;
    comType2[2] = &co2;

    co->CtListValue = comType2;
    co->CtListValueCount = 3;

    CompositeType* result2;

    BasicHttpBinding_IHelloWorldService_SayHelloCompType(
            proxy, co, &result2,
            heap, NULL, 0, NULL, error);

希望这会有所帮助......