11. Using XML-RPC with Microsoft .NET

(This section of the XML-RPC HOWTO was generously provided by Charles Cook.)

Charles Cook has implemented XML-RPC for Microsoft's .NET environment. You can find his XML-RPC.Net library at the Cook Computing website.

The following code requires the .NET SDK to be installed. To build and run XML-RPC.Net client and server applications, unzip cookcomputing.xmlrpc.dll from the XML-RPC.Net distribution and copy to the required directories.

XML-RPC.Net is compliant with the Common Language Specification and so can be used to build XML-RPC clients and servers using any CLS-compliant programming language. At the time of writing this means the languages provided by Microsoft—C#, VB.Net, JScript, and Managed C++—but in the future will include other languages such as Eiffel, Perl and Python.

sample.sumAndDifference client and server samples are presented here in both C# and VB.Net. The client samples make synchronous calls but it is also possible to make asynchronous calls—refer to the XML-RPC.Net documentation for further information.

11.1. A C# Client

Save the following code in a file called getSumAndDiffCS.cs:

using System;
using CookComputing.XmlRpc;

public struct Result 
{
  public int sum;
  public int difference;
}

[XmlRpcUri(
  "http://aspx.securedomains.com/cookcomputing/sumanddiffvb.aspx")]
class Proxy : XmlRpcClientProtocol
{
  [XmlRpcMethod("sample.sumAndDifference")]
    public Result SumAndDifference(int x, int y)
  {
    return (Result)Invoke("SumAndDifference", new Object[]{x,y})[0]; 
  }
}

public class App
{
  public static int Main(string[] args)
  {
    try
    {
      Proxy theProxy = new Proxy();
      Result ret = theProxy.SumAndDifference(5, 3);
      Console.WriteLine("5 + 3 = {0}", ret.sum);
      Console.WriteLine("5 - 3 = {0}", ret.difference);
    }
    catch(XmlRpcClientException cex)
    {
      Console.WriteLine("Client exception: {0} {1}", 
        cex.FaultCode, cex.FaultString);
    }
    catch(XmlRpcFaultException fex)
    {
      Console.WriteLine("Server exception: {0} {1}", 
        fex.FaultCode, fex.FaultString);
    }
    return 0;
  }
}

Use the following command to build the client:

csc /r:cookcomputing.xmlrpc.dll getSumAndDiffCS.cs

11.2. A Visual Basic .NET Client

Save the following code in a file called getSumAndDiffVB.vb:

Imports CookComputing.XmlRpc

Module Module1
  
  Structure Result
    Dim sum, difference As Integer
  End Structure
  
  Class <XmlRpcUri( _
    "http://aspx.securedomains.com/cookcomputing/sumanddiffcs.xmlrpc")> _
  Proxy : Inherits XmlRpcClientProtocol

    Public Function <XmlRpcMethod("sample.sumAndDifference")> _
    SumAndDifference(ByVal x As Integer, ByVal y As Integer) As Result
      Dim results As Object() = Me.Invoke("SumAndDifference", _
                                 New Object() {x,y})
      Return CType(results(0), Result)
    End Function

  End Class
  
  Sub Main()
    Dim theProxy As New Proxy()
    Try
      Dim ret As Result = theProxy.SumAndDifference(5, 3)
      Console.WriteLine("5 + 3 = {0}", ret.sum)
      Console.WriteLine("5 - 3 = {0}", ret.difference)
    Catch cex As XmlRpcClientException
      Console.WriteLine("Client exception: {0} {1}", _
                        cex.FaultCode, cex.FaultString)
    Catch fex As XmlRpcFaultException
      Console.WriteLine("Server exception: {0} {1}", _
                        fex.FaultCode, fex.FaultString)
    End Try
  End Sub
  
End Module

Use the following command to build the client:

vbc /r:cookcomputing.xmlrpc.dll getSumAndDiffVB.vb

11.3. A C# Server

XML-RPC.Net enables the implementation of XML-RPC Services running in the IIS 5 web server environment.

Save the following code in a file called SumAndDiffCS.cs:

using System;
using CookComputing.XmlRpc;

public struct Result 
{
  public int sum;
  public int difference;
}

[XmlRpcService(Description=
  "Sample XML-RPC.Net Service implemented in C#")]
public class SumAndDiffService : XmlRpcService
{
  [XmlRpcMethod("sample.sumAndDifference", Description=
    "This function takes two integers as arguments and returns an "+
    "XML-RPC struct containing two elements: sum, the sum of the "+
    "two integers, and difference, the difference between the two "+
    "integers")]
  public Result SumAndDifference(int x, int y)
  {
    Result ret;
    ret.sum = x + y;
    ret.difference = x - y;
    return ret;
  }
}

Use the following to build the client:

csc /r:System.Web.dll,cookcomputing.xmlrpc.dll /target:library SumAndDiffCS.cs

Create an IIS virtual root, say cookcomputing for this example, and a bin directory underneath it. Place the assembly (SumAndDiffCS.dll) and the XML-RPC.Net assembly (cookcomputing.xmlrpc.dll) in the bin directory.

Create a text file called config.web in the cookcomputing root directory with the following contents:

<configuration>
  <httphandlers>
    <add verb="*" path="sumanddiffcs.aspx" type="SumAndDiffService, 
SumAndDiffCS" />
  </httphandlers> 
</configuration>

The Service is now installed and configured. To check this navigate your browser to the URL of the Service where you will see an automatically generated documentation page.

11.4. A Visual Basic .NET Server

Save the following code in a file called SumAndDiffVB.cs:

Imports System
Imports CookComputing.XmlRpc

Public Structure Result
  Dim sum, difference As Integer
End Structure

Public Class <XmlRpcService(Description:= _
  "Sample XML-RPC.Net Service implemented in VB.Net")> _
SumAndDiffService : Inherits XmlRpcService
  
  Public Function <XmlRpcMethod("sample.sumAndDifference", _
  Description:= "This function takes two integers as arguments "+ _  
                "and returns an XML-RPC struct containing two "+ _
                "elements: sum, the sum of the two integers, and "+ _
                "difference, the difference between the two "+ _
                "integers")> _
  SumAndDifference(ByVal x As Integer, ByVal y As Integer) As Result
    Dim ret As Result
    ret.sum = x + y
    ret.difference = x - y
    Return ret
  End Function
  
End Class

Use the following to build the client:

vbc /r:System.Web.dll,cookcomputing.xmlrpc.dll /target:library SumAndDiffVB.vb

Create an IIS virtual root, say cookcomputing for this example, and a bin directory underneath it. Place the assembly (SumAndDiffVB.dll) and the XML-RPC.Net assembly (cookcomputing.xmlrpc.dll) in the bin directory.

Create a text file called config.web in the cookcomputing root directory with the following contents (note that the entries are case sensitive):

<configuration>
  <httphandlers>
    <add verb="*" path="sumanddiffvb.aspx" type="SumAndDiffService, 
SumAndDiffVB" />
  </httphandlers> 
</configuration>

The Service is now installed and configured. To check this navigate your browser to the URL of the Service where you will see an automatically generated documentation page.