gpt4 book ai didi

python - 将 Python 列表传递给 WCF 服务

转载 作者:太空狗 更新时间:2023-10-30 00:08:21 25 4
gpt4 key购买 nike

我正在尝试将字符串列表(或数组、集合)从 python 传递到 WCF 服务端点

WCF 接口(interface):

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

在 Web.config 中绑定(bind):

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

调用服务的Python代码:

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 捕获数据包并尝试从那里进行调试。希望有人知道一个简单的解决方案。

谢谢。

最佳答案

您需要使用 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 ();
}
}
}

关于python - 将 Python 列表传递给 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6833832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com