2011-07-26 73 views
6

我想傳遞一個列表(或陣列,集合)的WCF服務端點從蟒蛇傳遞Python列表爲字符串的WCF服務

的WCF接口:

[OperationContract] 
string[] TestList(IEnumerable<string> vals); 

綁定在Web.config中:

<endpoint address="http://localhost:13952/Service/Endpoint.svc" binding="basicHttpBinding" contract="Service.IEndpoint"> 

Python代碼調用SER副:

from suds.client import Client 
url = 'http://localhost:13952/Service/Endpoint.svc?wsdl' 
client = Client(url) 

result = client.service.TestList(('a', 'b', 'c')) 

產生的錯誤:

suds.WebFault: Server raised fault: 'The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:vals. The InnerException message was 'Error in line 1 position 310. Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''. '. Please see InnerException for more details.' 

我將嘗試使用Wireshark來捕捉數據包並試圖從那裏來調試。希望有人知道這個簡單的解決方案。

謝謝。

+1

我不熟悉suds或python,但是從錯誤消息看起來好像您已經發送命名空間信息作爲您的soap消息的一部分,tempuri是自asmx天后的默認命名空間服務微軟堆棧 – kd7

+0

我對python也不熟悉,但是「Expecting state'Element'..遇到'Text'」意味着字符串數組沒有以WCF期望的格式序列化。看到類似的問題[這裏](http://stackoverflow.com/questions/5219505/invoking-a-wcf-method-that-takes-a-list-of-objects-consumed-via-an-iphone-applic)和[這裏](http://social.msdn.microsoft.com/Forums/en/wcf/thread/fae9dbc5-c83e-42b2-808e-1a393e621bb8)。 – Kimberly

回答

4

您需要使用Suds客戶端工廠。 所以你失蹤的重要部分是client.factory.create('ArrayOfString')

見單元下方測試:

#!/usr/bin/env python 
import unittest 
import suds 
from suds.client import Client 

class TestCSharpWebService(unittest.TestCase): 

    def setUp(self): 
     url = "http://localhost:8080/WebService.asmx?wsdl=0" 
     self.client = Client(url) 

    def test_service_definition(self): 
     # You can print client to see service definition. 
     self.assertTrue("orderAlphabetically" in self.client.__str__()) 
     self.assertTrue("ArrayOfString" in self.client.__str__()) 

    def test_orderAlphabetically_service(self): 
     # Instanciate your type using the factory and use it. 
     ArrayOfString = self.client.factory.create('ArrayOfString') 
     ArrayOfString.string = ['foo', 'bar', 'foobar', 'a', 'b', 'z'] 
     result = self.client.service.orderAlphabetically(ArrayOfString) 
     # The result list contains suds.sax.text.Text which acts like a string. 
     self.assertEqual(
      type(result.string[0]), 
      suds.sax.text.Text) 
     self.assertEqual(
      [str(x) for x in result.string], 
      ['a', 'b', 'bar', 'foo', 'foobar', 'z']) 

if __name__ == '__main__': 
    unittest.main() 

我的MonoDevelop 3和Mono 2.10跑了Web服務。

namespace WebServiceMono 
{ 
    using System.Linq; 
    using System.Web.Services; 
    using System.Collections.Generic; 

    public class WebService : System.Web.Services.WebService 
    { 
     [WebMethod] 
     public string[] orderAlphabetically (List<string> list) 
     { 
      var result = list.OrderBy (s => s); 
      return result.ToArray(); 
     } 
    } 
} 
+0

是的,就是這樣 –