XML-RPC.NET Services and Proxy classes are easy to implement and install.The examples here are coded in C# but any CLS compliant language can be used.
1. Define the Service class. For example, this code is placed in a file called bettyservice.cs:
namespace CookComputing
{
using System;
using CookComputing.XmlRpc;
class BettyService : XmlRpcService
{
[XmlRpcMethod("examples.getStateName")]
public string getStateName(int
stateNum)
{
if (stateNum
== 41)
return
"South Dakota";
else
return
"Don't know";
}
}
}
2. Build the assembly as bettyservice.dll:
csc /r:system.web.dll /r:CookComputing.XmlRpc.dll /target:library bettyservice.cs
3. Create an IIS virtual root, say xmlrpc for this example, and a bin directory underneath it. Place the assembly, bettyservice.dll, and the XML-RPC.NET assembly, CookComputing.XmlRpc.dll, in the bin directory.
4. Ensure that IIS has been configured to map the .XMLRPC extension to the same DLL as .ASPX. This can be done at the site level (click on Configuration button on Home Directory tab of site Properties dialog) or for the virtual directory (click on Configuration button on Virtual Directory tab of the directory's Properties dialog). Alternatively, just use the .ASPX extension.
5. Create a text file called web.config in the xmlrpc root directory with the following contents:
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="betty.xmlrpc"
type="CookComputing.BettyService,
bettyservice" />
</httpHandlers>
</system.web>
6. The Service is now installed and configured. To check this point your browser at this URL:
http://www.cookcomputing.com/xmlrpc/betty.xmlrpc
This automatically generated page will be displayed.
1. Define the proxy class and the code to call it.
using System;
using CookComputing.XmlRpc;
[XmlRpcUrl("http://www.cookcomputing.com/xmlrpc/betty.xmlrpc")]
class Betty : XmlRpcClientProtocol
{
[XmlRpcMethod("examples.getStateName")]
public string GetStateName(int stateNum)
{
return (string)Invoke("GetStateName", new object[] {stateNum});
}
}
public class test
{
public static void Main()
{
try
{
Betty betty = new Betty();
string stateName = betty.getStateName(41);
Console.WriteLine("State 41 is {0}",
stateName);
}
}
Note: the call to Invoke is straightforward to implement for any method signature. The first parameter is the name of the method being defined and the second parameters contains the method's parameters in an array of type Object. The value returned from Invoke is then cast to the type returned by the method. For example a method taking more than one parameter would like this:
[XmlRpcMethod]
public int[] TestMethod(int param1, string param2, double[] param3)
{
return (int[])Invoke("TestMethod",
new object[] {param1, param2, param3}};
}
2. Build the executable:
csc /r:CookComputing.XmlRpc.dll testbetty.cs
3. Copy the XML-RPC.NET assembly, CookComputing.XmlRpc.dll, to the same directory as testbetty.exe.
4. Run the executable. The console output will be:
State 41 is South Dakota
| © Charles Cook, 2001 All Rights Reserved. |
30
December 2001
|