C# ExampleΒΆ

This is an example of how to use the Memset API with C# and XML-RPC.NET.

Please note that float point numbers are represented as double in C#.

Substitute API_KEY_HEX with a valid API key.

/*
* Memset API example with C# and XML-RPC.NET library.
*
* Tested wit Mono:
*   gmcs -r:CookComputing.XmlRpcV2.dll xmlrpctest.c
*   mono xmlrpctest.exe
*/

using System;
using CookComputing.XmlRpc;

// structure returned by service.info
public struct ProductInfo
{
    public string name;
    public string status;
    public string nickname;
    public DateTime start_date;
    public DateTime expiry_date;
    public double renewal_price_amount;
    public string renewal_price_currency;
    public double renewal_price_vat;
    public string product_type;
}

// structure defining a job status
public struct JobStatus
{
    public string id;
    public string type;
    public string status;
    public string product; // optional
    public bool finished;
    public bool error;
}

// Memset API XML-RPC interface (only some examples)
[XmlRpcUrl("https://api.memset.com/v1/xmlrpc/")]
public interface IMemsetAPI : IXmlRpcProxy
{
    [XmlRpcMethod("system.listMethods")]
    string[] listMethods();

    // all methods use named parameters, so StructParams is required
    [XmlRpcMethod("service.info", StructParams = true)]
    ProductInfo product_info(string name);

    [XmlRpcMethod("server.reboot", StructParams = true)]
    JobStatus server_reboot(string name);

    // ...
}

class xmlrpctest
{
    static void Main(string[] args)
    {
        try
        {
            IMemsetAPI proxy = XmlRpcProxyGen.Create<IMemsetAPI>();
            proxy.Credentials = new System.Net.NetworkCredential("API_KEY_HEX", "*");

            string[] methods = proxy.listMethods();
            Console.WriteLine("Current method list:");
            foreach (string method in methods) 
            {
                Console.WriteLine(method);
            }

            ProductInfo pi = proxy.product_info("myserver1");
            Console.WriteLine("Product info: {0} status is {1}", pi.name, pi.status);

            Console.WriteLine("Trying to reboot the server...");
            JobStatus js = proxy.server_reboot("myserver1");
            Console.WriteLine("Error {0}: id {1}, status {2} ({3} for {4})",
                            js.error, js.id, js.status, js.type, js.product);
        }
        catch (XmlRpcFaultException fex)
        {
            Console.WriteLine("XMLRPC FAULT: {0}", fex.FaultString);
        }
        catch (Exception ex)
        {
            Console.WriteLine("EXCEPTION: {0}", ex.Message);
        }
    }
}

Previous topic

Java Example

Next topic

Ruby Example

This Page