Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Python List to WCF Service

I am trying to pass a list (or array, collection) of strings from python to a 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 code calling the service:

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

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

Resulting error:

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.'

I will try to catch the packets using WireShark and try to debug from there. Hoping someone knows a simple solution for this.

Thanks.

like image 256
Jama Avatar asked Jul 26 '11 17:07

Jama


1 Answers

You need to use Suds client factory. So the important piece you're missing is client.factory.create('ArrayOfString').

See unit test below:

#!/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()

I ran the web service in MonoDevelop 3 and Mono 2.10.

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 ();
        }
    }
}
like image 139
Andre Miras Avatar answered Nov 05 '22 08:11

Andre Miras