Wednesday, April 14, 2010

basic xml-rpc communication between a python server and c# client

Setting up the python server

import calendar, SimpleXMLRPCServer

#The server object
class Calendar:
    def getMonth(self, year, month):
        return calendar.month(year, month)

    def getYear(self, year):
        return calendar.calendar(year)


calendar_object = Calendar()
server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8888))
server.register_instance(calendar_object)

#Go into the main listener loop
print "Listening on port 8888"
server.serve_forever()



Setting up the C# client

1. Download the helping DLL's from http://xml-rpc.net/
2. Create a proxy interface: 


using System;
using CookComputing.XmlRpc;

namespace XMLRPCclient
{
   
    [XmlRpcUrl("http://127.0.0.1:8888/")]       
    public interface IClientCalendarProxy : IXmlRpcProxy
    {
        [XmlRpcMethod("getMonth")]
        string getMonth(int p1, int p2);

    }
}



3. Init & run your client using the upper defined proxy interface (make sure the URL points to the port of the web service)

using System;
using CookComputing.XmlRpc;

namespace XMLRPCclient
{

    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
           
           

            IClientCalendarProxy proxy = XmlRpcProxyGen.Create<IClientCalendarProxy>();


            string ret = proxy.getMonth(2002,8);
            Console.WriteLine(ret);

        }
    }
}



Useful links: