C#


 

In this article we discuss some examples for API interaction. It should be noted that these are indeed examples and not production-grade code.

Add a service reference

Visual Studio can generate classes to interact with WCF services. Follow the following steps in Visual Studio to generate the code.

  1. Go to: Project -> Add Service Reference…
  2. Enter the following information
    Address: https://api.cloudbilling.nl/V1_3_2.svc
    Namespace: CB
  3. Click OK

Get all customers

The code sample below shows a complete C# console application that retrieves all customers. It expects the service reference as shown above.


using System;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace CBApiClientSandbox
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new CB.APIServiceEndpointClient();
            string token = "...";

            using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("ApiToken", "http://api.cloudbilling.nl/2018/11/auth/token", token));

                var customers = client.CustomersGetAll();
                if (customers.ResultCode != CB.ResultCodeEnum.Success)
                {
                    throw new Exception($"Could not get customers: {customers.ResultMessage}");
                }
                Console.WriteLine($"There are {customers.Result.Length} customers");

                // Call other methods from inside this scope.
            }
        }
    }
}