Accessing XmlRpcPlugin from C#

It is possible to access Trac via the XmlRpcPlugin plug-in from Microsoft's .NET framework. This should work for any .NET language (including Visual Basic). The steps below are written in C#.

Step 1. Install the Trac XmlRpcPlugin

see XmlRpcPlugin

Step 2. Download and compile XML-RPC.NET


Step 3. Create a .NET project

To your new project, add the library you just compiled (above) as a reference.

Step 4. Create the interface

Some day it would be nice to have the complete WikiRPC described; for now, you can just piece together the methods you need as you need them. Create an interface that extends IXmlRpcProxy. In this example, the interface is named "Trac". Then, declare methods that are tagged as XmlRpcMethod as below.

I have included two examples. The first returns a list of all the pages on your wiki. The second posts a new page (or a new revision to an existing page) to the wiki.

using CookComputing.XmlRpc;

namespace TracPusher {
    public interface Trac : IXmlRpcProxy
        string[] getAllPages();

        bool putPage(string pagename, string content, PageAttributes attr);

    // define the structure needed by the putPage method
    struct PageAttributes {
        public string comment;

Step 5. Application Code

Here's an example of how to use this. For simplicity, we'll just add a Main to the above

static void Main(string [] args)
   Trac proxy;

   // Fill these in appropriately
   string user = "yourTracUserName";
   string password = "yourTracPassword";

   /// Create an instance of the Trac interface
   proxy = XmlRpcProxyGen.Create<Trac>();

   // If desired, point this to your URL. If you do not do this,
   // it will use the one specified in the service declaration.
   // proxy.Url = "";

   // Attach your credentials
   proxy.Credentials = new System.Net.NetworkCredential(user, password);

   PageAttributes attr;
   attr.comment = "This is the comment that goes with the new page";
   bool rc = proxy.putPage("SandBox", // new page name
              "''hi chris'', this page was automatically added via XmlRpc from .NET", // new page contents
              attr // new page attributes

   Console.WriteLine("Result: " + rc);

Step 6. Handling Certificate Problems (if any)

If your trac uses https, make sure it has a valid, verifyable certificate. If you use a self-signed or expired certificate, all is not lost, but you have to take a few additional steps.

First, you need to create a server certificate validation callback that always says the certificate is OK, even if it's really not. Example:

private bool AcceptCertificateNoMatterWhat(object sender,
            System.Security.Cryptography.X509Certificates.X509Certificate cert,
            System.Security.Cryptography.X509Certificates.X509Chain chain,
            System.Net.Security.SslPolicyErrors errors)
   return true;

Second, before you create the proxy, do this (for example, at the beginning of Main)

ServicePointManager.ServerCertificateValidationCallback = AcceptCertificateNoMatterWhat;
Last modified 7 months ago Last modified on Jan 12, 2014 8:28:55 AM

Attachments (1)

Download all attachments as: .zip