Show / Hide Table of Contents

Initializing a console application

To create a Console application, you need to:

  • Create an instance of the TTAPIOptions class passing the TT environment to which your application will connect, your application key for this environment, and a timeout interval.

    TT recommends setting the value of the Timeout interval to 5000 milliseconds.

  • Attach a worker dispatcher to the thread on which you will consume events and start it.

  • Initialize TT .NET SDK.

  • Authenticate your credentials.

Client-side mode example

The following code snippet demonstrates an example of this process for client-side mode.

// ...

using tt_net_sdk;

// ...

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Add your app secret Key here. It looks like: 00000000-0000-0000-0000-000000000000:00000000-0000-0000-0000-000000000000
            string appSecretKey = "Your App Key";

            //Set the environment the app needs to run in here
            tt_net_sdk.ServiceEnvironment environment = tt_net_sdk.ServiceEnvironment.UatCert;

            tt_net_sdk.TTAPIOptions apiConfig = new tt_net_sdk.TTAPIOptions(
                     environment,
                     appSecretKey,
                     5000);

            apiConfig.BinaryProtocol = true;

            // Start the TT API on the same thread
            TTNetApiFunctions tf = new TTNetApiFunctions();

            Thread workerThread = new Thread(() => tf.Start(apiConfig));
            workerThread.Name = "TT NET SDK Thread";
            workerThread.Start();

            while (true)
            {
                string input = System.Console.ReadLine();
                if (input == "q")
                    break;
            }
            tf.Dispose();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message + "\n" + e.StackTrace);
        }
    }
}


// ...

using tt_net_sdk;

// ...

public class TTNetApiFunctions
{
    // Declare the API objects
    private TTAPI m_api = null;
    private tt_net_sdk.WorkerDispatcher m_disp = null;


    public void Start(tt_net_sdk.TTAPIOptions apiConfig)
    {
        m_disp = tt_net_sdk.Dispatcher.AttachWorkerDispatcher();
        m_disp.DispatchAction(() =>
             {
                 Init(apiConfig);
             });

        m_disp.Run();
    }

    public void Init(tt_net_sdk.TTAPIOptions apiConfig)
    {
        ApiInitializeHandler apiInitializeHandler = new ApiInitializeHandler(ttNetApiInitHandler);
        TTAPI.CreateTTAPI(tt_net_sdk.Dispatcher.Current, apiConfig, apiInitializeHandler);
    }

    public void ttNetApiInitHandler(TTAPI api, ApiCreationException ex)
    {
        if (ex == null)
        {
            Console.WriteLine("TT.NET SDK Initialization Complete");
            // Authenticate your credentials
            m_api = api;
            m_api.TTAPIStatusUpdate += new EventHandler<TTAPIStatusUpdateEventArgs>(m_api_TTAPIStatusUpdate);
            m_api.Start();
        }
        else if (ex.IsRecoverable)
        {
            // this is in informational update from the SDK
            Console.WriteLine("TT.NET SDK Initialization Message: {0}", ex.Message);
            if (ex.Code == ApiCreationException.ApiCreationError.NewAPIVersionAvailable)
            {
                // a newer version of the SDK is available - notify someone to upgrade
            }
        }
        else
        {
            Console.WriteLine("TT.NET SDK Initialization Failed: {0}", ex.Message);
            if (ex.Code == ApiCreationException.ApiCreationError.NewAPIVersionRequired)
            {
                // do something to upgrade the SDK package since it will not start until it is upgraded 
                // to the minimum version noted in the exception message
            }
            Dispose();
        }
    }

    public void m_api_TTAPIStatusUpdate(object sender, TTAPIStatusUpdateEventArgs e)
    {
        if (e.IsReady)
        {
            // connection to TT is established
            Console.WriteLine("TT.NET SDK Authenticated");
        }
        else if (ex.IsDown)
        {
            // connection is down but TT .NET SDK will automatically attempt to reconnect
            Console.WriteLine("Connection down. Reconnecting...");
        }
        else
        {
            Console.WriteLine("TT.NET SDK Status: {0}", e);
        }

    }

    public void Dispose()
    {
        TTAPI.ShutdownCompleted += TTAPI_ShutdownCompleted;
        TTAPI.Shutdown();
    }

    public void TTAPI_ShutdownCompleted(object sender, EventArgs e)
    {
        Console.WriteLine("TTAPI Shutdown completed");
    }
}

Server-side mode example

The following code snippet demonstrates an example of this process for server-side mode.

// ...

using tt_net_sdk;

// ...

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Add your app secret Key here. It looks like: 00000000-0000-0000-0000-000000000000:00000000-0000-0000-0000-000000000000
            string appSecretKey = "Your App Key";

            //Set the environment the app needs to run in here
            tt_net_sdk.ServiceEnvironment environment = tt_net_sdk.ServiceEnvironment.UatCert;

            tt_net_sdk.TTAPIOptions apiConfig = new tt_net_sdk.TTAPIOptions(
                    tt_net_sdk.TTAPIOptions.SDKMode.Server,
                    environment,
                    appSecretKey,
                    5000);
                    
            apiConfig.ServerInstanceId = “<InstanceIdAssignedToYourMachineByTT>”;

            // Start the TT API on the same thread
            TTNetApiFunctions tf = new TTNetApiFunctions();

            Thread workerThread = new Thread(() => tf.Start(apiConfig));
            workerThread.Name = "TT NET SDK Thread";
            workerThread.Start();

            while (true)
            {
                string input = System.Console.ReadLine();
                if (input == "q")
                    break;
            }
            tf.Dispose();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message + "\n" + e.StackTrace);
        }
    }
}


// ...

using tt_net_sdk;

// ...

public class TTNetApiFunctions
{
    // Declare the API objects
    private TTAPI m_api = null;
    private tt_net_sdk.WorkerDispatcher m_disp = null;


    public void Start(tt_net_sdk.TTAPIOptions apiConfig)
    {
        m_disp = tt_net_sdk.Dispatcher.AttachWorkerDispatcher();
        m_disp.DispatchAction(() =>
             {
                 Init(apiConfig);
             });

        m_disp.Run();
    }

    public void Init(tt_net_sdk.TTAPIOptions apiConfig)
    {
        ApiInitializeHandler apiInitializeHandler = new ApiInitializeHandler(ttNetApiInitHandler);
        TTAPI.CreateTTAPI(tt_net_sdk.Dispatcher.Current, apiConfig, apiInitializeHandler);
    }

    public void ttNetApiInitHandler(TTAPI api, ApiCreationException ex)
    {
        if (ex == null)
        {
            Console.WriteLine("TT.NET SDK Initialization Complete");
            // Authenticate your credentials
            m_api = api;
            m_api.TTAPIStatusUpdate += new EventHandler<TTAPIStatusUpdateEventArgs>(m_api_TTAPIStatusUpdate);
            m_api.Start();
        }
        else if (ex.IsRecoverable)
        {
            // this is in informational update from the SDK
            Console.WriteLine("TT.NET SDK Initialization Message: {0}", ex.Message);
            if (ex.Code == ApiCreationException.ApiCreationError.NewAPIVersionAvailable)
            {
                // a newer version of the SDK is available - notify someone to upgrade
            }
        }
        else
        {
            Console.WriteLine("TT.NET SDK Initialization Failed: {0}", ex.Message);
            if (ex.Code == ApiCreationException.ApiCreationError.NewAPIVersionRequired)
            {
                // do something to upgrade the SDK package since it will not start until it is upgraded 
                // to the minimum version noted in the exception message
            }
            Dispose();
        }
    }

    public void m_api_TTAPIStatusUpdate(object sender, TTAPIStatusUpdateEventArgs e)
    {
        if (e.IsReady)
        {
            // connection to TT is established
            Console.WriteLine("TT.NET SDK Authenticated");
        }
        else if (ex.IsDown)
        {
            // connection is down but TT .NET SDK will automatically attempt to reconnect
            Console.WriteLine("Connection down. Reconnecting...");
        }
        else
        {
            Console.WriteLine("TT.NET SDK Status: {0}", e);
        }
    }

    public void Dispose()
    {
        TTAPI.ShutdownCompleted += TTAPI_ShutdownCompleted;
        TTAPI.Shutdown();
    }

    public void TTAPI_ShutdownCompleted(object sender, EventArgs e)
    {
        Console.WriteLine("TTAPI Shutdown completed");
    }
}
In this article
Back to top