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

问题:

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


解决方案:

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

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()

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序列化问题。
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值