Python通过suds调用WCF服务 传递Python List 到 WCF Service

来源:互联网 发布:sqlserver数据恢复 编辑:程序博客网 时间:2024/05/01 13:07
问题:

python传递list (or array, collection) of strings 到一个WCF service endpoint

The WCF interface:

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

Binding in Web.config:

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

Python调用WCF服务:

from suds.client import Clienturl = '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抓包,观察序列化后的soap数据,发现list序列化有错误,导致服务端无法反序列化。


解决方案:

使用Suds client factory。此步骤非常重要:

client.factory.create('ArrayOfString')

示例如下:

#!/usr/bin/env pythonimport unittestimport sudsfrom suds.client import Clientclass 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()

web service:

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 ();        }    }}
使用client.factory.create('ArrayOfString')后可以很好的解决python list序列化问题。
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 商品没有支付宝怎么办 金立s8热点打不开怎么办 淘宝店铺异常2怎么办 付款付错了怎么办 手机淘宝太卡怎么办 卖家不同意退货怎么办 游戏退出无响应怎么办 手机淘宝购物车打不开怎么办 淘宝店铺没有访客怎么办 淘宝店铺0流量怎么办 微信经常封号怎么办 网上拍卖堂违约怎么办 dnf4开组队制裁怎么办 红酒木塞丢了怎么办 红酒塞子进去了怎么办 淘金币即将过期怎么办 淘金币过期怎么办2018 换详情排名下降怎么办 长城宽带不用了怎么办 快递到了想退货怎么办 淘宝退货商家拒收怎么办 淘宝运费险失败怎么办 忘记购买运费险怎么办 咸鱼买家申请退款怎么办 熟猪肉有点变味怎么办 和领导意见不一致怎么办 骑手提前点送达怎么办 ubuntu安装报错怎么办 液相色谱两峰分不开怎么办 液相色谱柱老堵怎么办? 没有装usb驱动怎么办 ipad速度越来越慢怎么办 美萍管理软件打不开怎么办 小米4开机黑屏怎么办 小米电脑死机了怎么办 小米8手机死机怎么办 oppa7开不了机怎么办 oppo手机wlan打不开怎么办 三星s6进水黑屏怎么办 银行卡不支持快捷支付怎么办 路由器忘记管理员密码怎么办