Changes between Initial Version and Version 1 of XmlRpcPlugin/DotNet


Ignore:
Timestamp:
Apr 19, 2012 9:30:01 AM (2 years ago)
Author:
rjollos
Comment:

Content moved from DotNet page.

Legend:

Unmodified
Added
Removed
Modified
  • XmlRpcPlugin/DotNet

    v1 v1  
     1= Accessing XmlRpcPlugin from C# = 
     2 
     3It 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#. 
     4 
     5== Step 1. Install the Trac XmlRpcPlugin == 
     6 
     7see XmlRpcPlugin 
     8 
     9 
     10== Step 2. Download and compile XML-RPC.NET == 
     11 
     12[http://xml-rpc.net XML-RPC.NET] 
     13 
     14 
     15== Step 3. Create a .NET project == 
     16 
     17To your new project, add the library you just compiled (above) as a reference. 
     18 
     19 
     20== Step 4. Create the interface == 
     21 
     22Some 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. 
     23 
     24I 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. 
     25 
     26{{{ 
     27using CookComputing.XmlRpc; 
     28 
     29namespace TracPusher { 
     30    [XmlRpcUrl("https://your.server.org/trac/login/xmlrpc")] 
     31    public interface Trac : IXmlRpcProxy 
     32    { 
     33        [XmlRpcMethod("wiki.getAllPages")] 
     34        string[] getAllPages(); 
     35 
     36        [XmlRpcMethod("wiki.putPage")] 
     37        bool putPage(string pagename, string content, struct PageAttributes attr); 
     38    } 
     39 
     40    // define the structure needed by the putPage method 
     41    struct PageAttributes { 
     42        public string comment; 
     43    } 
     44} 
     45}}} 
     46 
     47 
     48== Step 5. Application Code == 
     49 
     50Here's an example of how to use this.  For simplicity, we'll just add a Main to the above 
     51 
     52{{{ 
     53static void Main(string [] args) 
     54{ 
     55   Trac proxy; 
     56 
     57   // Fill these in appropriately 
     58   string user = "yourTracUserName"; 
     59   string password = "yourTracPassword"; 
     60 
     61   /// Create an instance of the Trac interface 
     62   proxy = XmlRpcProxyGen.Create<Trac>(); 
     63 
     64   // If desired, point this to your URL. If you do not do this, 
     65   // it will use the one specified in the service declaration. 
     66   // proxy.Url = "https://trac-rules.org/xmlrpc"; 
     67 
     68   // Attach your credentials 
     69   proxy.Credentials = new System.Net.NetworkCredential(user, password); 
     70 
     71   PageAttributes attr; 
     72   attr.comment = "This is the comment that goes with the new page"; 
     73   bool rc = proxy.putPage("SandBox", // new page name 
     74              "''hi chris'', this page was automatically added via XmlRpc from .NET", // new page contents 
     75              attr // new page attributes 
     76   ); 
     77 
     78   Console.WriteLine("Result: " + rc); 
     79} 
     80}}} 
     81 
     82 
     83== Step 6. Handling Certificate Problems (if any) == 
     84 
     85If 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. 
     86 
     87First, you need to create a server certificate validation callback that always says the certificate is OK, even if it's really not.  Example: 
     88 
     89{{{ 
     90private bool AcceptCertificateNoMatterWhat(object sender, 
     91            System.Security.Cryptography.X509Certificates.X509Certificate cert, 
     92            System.Security.Cryptography.X509Certificates.X509Chain chain, 
     93            System.Net.Security.SslPolicyErrors errors) 
     94{ 
     95   return true; 
     96} 
     97}}} 
     98 
     99Second, ''before'' you create the proxy, do this (for example, at the beginning of Main) 
     100 
     101{{{ 
     102ServicePointManager.ServerCertificateValidationCallback = AcceptCertificateNoMatterWhat; 
     103}}}