Consuming Python Web Services with C#

Consuming Python Web Services written and deployed with Soaplib is possible using C# and Mono. I haven't try on .Net yet but if it works on Mono it definitively will work on .Net. You can read about Soaplib in previous posts.
First of all, write a web service that returns a single string and another that returns an array of strings:

  2. @soapmethod(String, _returns=String)
  3. def srv_getServerData(self, serverName):
  4. return str(Server.objects.filter(nombre=serverName).values())
  6. @soapmethod(_returns=Array(String))
  7. def srv_getServersList(self):
  8. results = []
  9. for servicesValues in Service.objects.values():
  10. results.append(str(servicesValues))
  11. return results

Second: Run the web server and generate the WSDL file. I do that using a Python client.

  2. def getWSDL():
  3. wsdl = getClientInfoService().server.wsdl("https://" +
  4. Server.getServerPublicIp() + ':' +
  5. constants.webServicesPort + constants.serviceUrl)
  6. fileName = '/tmp/service.wsdl'
  7. wsdlFile = open(fileName, 'w')
  8. wsdlFile.write(wsdl)
  9. wsdlFile.close()
  10. print "File generated:", '/tmp/service.wsdl'

Third: Generate the C# proxy reading the WSDL. We need the wsdl tool (package mono-1.0-devel on Ubuntu).
wsdl service.wsdl
That will create the file Service.cs. In this file there are several typecasts that are not working properly. In order to get the data from the service I modify it using sed.

  2. sed 's/return ((.*Response)(results\[0\]))/return ((System.Xml.XmlNode)results\[0\])/g' Service.cs > tmp1
  3. sed 's/public srv_.*Response/public System.Xml.XmlNode/g' tmp1 > Service.cs

Note that my services methods start with the "srv_" prefix, so I use that in the regex above to match them.
Fourth: C# code to get that from the services:

  2. // method 1:
  3. srv_getServerData y = new srv_getServerData();
  4. y.serverName = "pepino";
  5. Service client = new Service();
  6. System.Xml.XmlNode res = client.srv_getServerData(y).ChildNodes[0];
  7. String result = String.Empty;
  8. result = res.InnerText;
  10. // method 2:
  11. Service client = new Service();
  12. srv_getServersList x = new srv_getServersList();
  13. System.Xml.XmlNodeList result = client.srv_getServersList(x).ChildNodes;
  14. String result = String.Empty;
  15. foreach (System.Xml.XmlNode n in result)
  16. result += '\n' + n.InnerText;

It works!!!

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • boots

    In Case B it tries to lock the files in the Bin-directory, which then leads to the locking-conflict described above.

  • job

    I could not agree with you more