Write For Us


What is XML RPC?

XML RPC is a standard for calling functions / passing parameters to these functions / retrieving return values from these functions. The caller program and the called function typically exist on separate machines. If a server is available and it provides some functions then a client anywhere can call these functions via a simple HTTP (or HTTPS) URL.


The parameters are sent from the client to the server in a form of XML text. The server then receives the parameters and carries out the actions required by the to be called function. The return value is then returned to the client in the form of XML text as well. All this communication is performed via the more than widely used HTTP (or HTTPS) protocol. It's obvious from the above calling structure that XML RPC is totally platform independent and that it can be employed in virtually any environment due to the simplicity / effectiveness of the calling structure.

This tutorial will teach the reader how to implement an XML RPC capable client from VB.NET application and what the external components that can be used to achieve this target.

XML RPC Server and Client

XML RPC Server

Any kind of web services those are located in a server machine.

XML RPC Client

Is the program located on your machine and sends a request to a web service located on XML RPC server.

In this tutorial we will build a client application and use a test server available for test operations.

XML RPC in Microsoft .NET Framework with VB.NET

Although widely used, there is no native support for XML RPC in Microsoft .NET. Fortunately, there is a well established XML RPC library written by Charles Cook specifically for this purpose and called XML-RPC.Net. XML-RPC.Net is a .NET class library for implementing XML-RPC clients and servers.

You can obtain XML-RPC.Net from www.xml-rpc.net

Inside the download you will find the 'CookComputing.XmlRpc.dll' which is the DLL / Assembly that represents the xml-rpc.net library. The responsibility of this library is to put your ordinal function call into a form suitable to the used transfer protocol (In other words forms it into a packet), open the connection; send a request to the server containing the requested web service. The server run the request (run the called method) then returns the result to the xml-rpc.net library. The xml-rpc.net library will parse the incoming package, extracts the result from it, and returns it as a normal type to the called method. All the transfers in the above scenario are done using the XML language.

How to setup the XML-RPC.Net library into your VB.NET project?

To make use of the xml-rpc.net library inside your .net project you have to do the followings:

- Put the 'CookComputing.XmlRpc.dll' library in the same directory with the project's executable file (.EXE).
- Add a reference to the library in to your project by using the references node in Solution Explorer window.
- Add a reference to the 'CookComputing.XmlRpc' namespace in the 'imports' section at the top of your source code file as shown below.

Imports CookComputing.XmlRpc

Now the XML-RPC.Net library is set-up into your project and you can write code to make use of it.

Writing source code that makes use of XML-RPC.Net

We will build a small program that makes a simple addition operation. We will use the test math service available at "http://www.cookcomputing.com/xmlrpcsamples/math.rem"

Open up your Visual Studio 2005 and create a new Visual Basic project. In the 'Solution Explorer' window, left click the project name and click 'Add Reference...' on the appeared pop up menu as shown below.

Figure 1 - The Add Reference dialog box

Click the 'Browse' tab of the 'Add Reference' dialog box. Browse to the project's 'Bin' directory where you put your 'CookComputing.XmlRpc.dll' file. Select it then click 'Ok'. The DLL is now added to the references section of your project.

Design a suitable graphical user interface as the following one.

Figure 2 - Graphical User Interface

From the properties window name the first text box "TB_Num1", name the second one "TB_Num2", name the button "Btn_Add", and finally name the label "Lbl_Result".

Double click the form body to activate the Form1's load event handler, and double click the button to activate its click event handler. Now view the source code file of 'Form1'. At the top of the code file add the following imports statement to removes the need to qualify names when you use any defined methods or types from the referenced DLL.

Imports CookComputing.XmlRpc

To be able to perform calls to the needed services you have to do the followings:

Define the following interface outside the "Form1" class scope. Just put it on top of the "Form1" class declaration.

Interface IMath
    <CookComputing.XmlRpc.XmlRpcMethod("math.Add")> _
Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
End Interface

The above lines of code define a new interface called "IMath". Inside the interface is a function called "Add" takes two integer parameters and returns an integer as a result. The purpose of the line above the function prototype is to provide the suitable association between the declared 'Add' function and its counterpart in the test math services. So, when you type "Add(a,b)" in your code the "math.Add" function (which is located at the XML RPC server) will be called instead.

In the "Form1" class define the following variables.

Private mathProxy As IMath
Private ClientProtocol As XmlRpcClientProtocol
Private TxtUrl As String

As you see the first line defines a variable of the type "IMath". The second one defines a "ClientProtocol" variable of type "XmlRpcClientProtocol" which is a predefined type in the "CookComputing.XmlRpc" namespace. The last line defines an ordinary string variable.

In the "Form1_Load" event handler add the following lines of code.

Private Sub Form1_Load(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles MyBase.Load
    mathProxy = CType(XmlRpcProxyGen.Create(GetType(IMath)), IMath)
    ClientProtocol = CType(mathProxy, XmlRpcClientProtocol)
    TxtUrl = "http://www.cookcomputing.com/xmlrpcsamples/math.rem"
End Sub

The first line of code in this function calls the static "Create" method of "XmlRpcProxyGen" to create an instance of a dynamically generated class which implements the interface and derives from "XmlRpcClientProtocol". The second line returns the result of explicitly converting the "mathProxy" into "XmlRpcClientProtocol" type. The third line just assigns the URL of the server providing the XML RPC service. Note that the same URL could be used for a set of services (functions) like 'Subtract', 'Multiply', or 'Divide'.

In the 'Btn_Add_Click' event handler add the following lines of code.

Private Sub Btn_Add_Click(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles Btn_Add.Click
    ClientProtocol.Url = TxtUrl
    Cursor = Cursors.WaitCursor
        Me.Lbl_Result.Text = ""
        Dim a As Integer = Me.TB_Num1.Text
        Dim b As Integer = Me.TB_Num2.Text
        Dim result As Integer = mathProxy.Add(a, b)
        Me.Lbl_Result.Text = result.ToString
    Catch ex As Exception
    End Try
    Cursor = Cursors.Default
End Sub

ClientProtocol.Url = TxtUrl assigns the URL of the test server to the 'ClientProtocol' URL property. In the 'Try' block we assign the two numbers written in text boxes to variables 'a' and 'b'. The mathProxy.Add(a, b) function of the defined 'mathproxy' is called and the result is assigned to the 'result' variable. Then the result is displayed in the label as shown below.

Figure 3 - This simple add operation is done in another server machine not on the current machine that runs the program.

You can download sample Using RPC With VB.NET sample Visual Studio project, used in this tutorial.

Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   

comments powered by Disqus