2012-11-25 48 views
0

我的客戶端正在使用從WSDL創建的代理...是否可以設置服務發送列表而不是MyCustomObject[]WCF服務返回數組而不是列表

我使用

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

但不工作...

我的客戶是在Visual C++

IDE是的Visual Studio 2012 - >不能添加服務引用

+0

你說它「不起作用」;它怎麼不工作? – carlosfigueira

+0

您需要記住,服務不會發送列表或數組。他們發送XML。列表或數組或其他東西在客戶端,而不是服務器。 –

+0

嗨約翰,是的,你是正確的客戶端在他們的數據類型轉換爲XML,JSON等......但爲什麼WCF客戶端在C#可以發送集合甚至自定義集合和服務器接他們?我的客戶端是Visual C++,所以我徘徊,如果我可以有相同的行爲... – masuberu

回答

0

好吧......在我放棄了年底,我剛發來的數據作爲數組,而不是性病:列表...

我這是怎麼做的: 1.-得到的元數據服務使用svcutil/t:元數據(注意:服務必須運行)。 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); 

希望這可以幫助...

1

是的。

/collectionType:如果您直接使用svcutil.exe,或者如果「添加服務引用」 轉到高級 - >集合類型並更改爲任何你想要的。

爲什麼它很可能是使用Web服務/ WCF「服務」時,您的端點總是 接收XML/JSON序列化的數據,比這個數據進行反序列化到你的C++數據類型, ,它取決於你在哪個集合鍵入您想要反序列化包含某些集合的收到數據。

+1

嗨Nightwish91,我認爲從Visual C++「添加服務引用」已禁用Visual Studio 2012,我也試過svcutil,因爲你可以在我的文章中看到,但仍然收到wchar_t **(它的行爲像一個數組)...知道我錯過了什麼嗎? – masuberu