wiki:XmlRpcPlugin/DotNet

Accessing XmlRpcPlugin from C#

It is possible to access Trac via the XmlRpcPlugin plugin 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

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

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 shown 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 {
    [XmlRpcUrl("https://your.server.org/trac/login/xmlrpc")]
    public interface Trac : IXmlRpcProxy
    {
        [XmlRpcMethod("wiki.getAllPages")]
        string[] getAllPages();

        [XmlRpcMethod("wiki.putPage")]
        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 is 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 = "https://trac-rules.org/xmlrpc";

   // 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, verifiable certificate. If you use a self-signed or expired certificate, you have to take a few additional steps.

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

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, add this, for example, at the beginning of Main:

ServicePointManager.ServerCertificateValidationCallback = AcceptCertificateNoMatterWhat;
Last modified 6 years ago Last modified on Jul 19, 2018, 7:56:37 PM

Attachments (1)

Download all attachments as: .zip