Skip to content

C# SDK Quick Start

This guide provides a quick start for using the C# SDK for common operations. You will learn how to install the SDK, configure access credentials, and perform basic operations to get the latest upgrade information.

Notes

  • To make requests using the C# SDK, you need to initialize a Client instance. This article creates a Client by loading the default configuration. For more configuration options, please refer to Configuring the Client.

Prerequisites

  • You have registered for an UpgradeLink account.
  • You have obtained the AccessKey and AccessSecret.
  • You have configured a URL application upgrade strategy.

Obtaining Credentials

img.jpg

Installation

Install via NuGet

  • If NuGet is not installed in your Visual Studio, please install NuGet first.
  • After installing NuGet, create or open an existing project in Visual Studio, then select <Tools> - <NuGet Package Manager> - <Manage NuGet Packages for Solution>.
  • Search for ToolsetLink.UpgradeLinkApi, find ToolsetLink.UpgradeLinkApi in the results, select the latest version, and click Install to add it to your project.

Install via GitHub

Project Integration Method

  • If you have downloaded the SDK package or source code from GitHub and want to install from source, right-click <Solution>, and in the context menu, click <Add> -> <Existing Project>.
  • In the dialog that appears, select the upgradeLinkApiCSharp.csproj file and click Open.
  • Next, right-click <Your Project> - <References>, select <Add Reference>, in the dialog that appears, select the <Projects> tab, check the upgradeLinkApiCSharp project, and click OK.

Quick Start

The following example demonstrates how to initialize a Client and get the latest upgrade information for a Win application.

Get the Latest Upgrade Information for Win Application

csharp
using System;
using System.Reflection;
using NUnit.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using ToolsetLink.UpgradeLinkApi;
using ToolsetLink.UpgradeLinkApi.Models;

namespace UpgradeLinkApi.Tests
{
    [TestFixture]
    public class ClientTests
    {
        // Custom JSON contract resolver for handling [NameInMap] attribute
        private class NameInMapContractResolver : DefaultContractResolver
        {
            protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
            {
                JsonProperty property = base.CreateProperty(member, memberSerialization);
                
                // Find NameInMap attribute
                var nameInMapAttribute = member.GetCustomAttribute(typeof(Tea.NameInMapAttribute)) as Tea.NameInMapAttribute;
                if (nameInMapAttribute != null)
                {
                    // Use the value of NameInMap attribute as JSON field name
                    property.PropertyName = nameInMapAttribute.Name;
                }
                
                return property;
            }
        }
        
        // Custom JSON serialization settings
        private static readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
        {
            ContractResolver = new NameInMapContractResolver(),
            NullValueHandling = NullValueHandling.Ignore
        };
        
        private Client _client;
        private Config _config;

        [SetUp]
        public void Setup()
        {
            // Configure the client for testing
            _config = new Config
            {
                AccessKey = "mui2W50H1j-OC4xD6PgQag",
                AccessSecret = "PEbdHFGC0uO_Pch7XWBQTMsFRxKPQAM2565eP8LJ3gc",
                // Protocol = "HTTP",
                // Endpoint = "127.0.0.1:8888"
            };
            
            _client = new Client(_config);
        }

        [Test]
        public void WinUpgrade_Should_Send_Request_And_Print_Result()
        {
            // Arrange
            var request = new WinUpgradeRequest
            {
                WinKey = "npJi367lttpwmD1goZ1yOQ",
                Arch = "x64",
                VersionCode = 1,
                AppointVersionCode = 0,
                DevModelKey = "",
                DevKey = ""
            };

            // Act
            try
            {
                Console.WriteLine("Sending WinUpgrade request...");
                // Print request information
                Console.WriteLine("Request Information:");
                Console.WriteLine($"  WinKey: {request.WinKey}");
                Console.WriteLine($"  Arch: {request.Arch}");
                Console.WriteLine($"  VersionCode: {request.VersionCode}");
                Console.WriteLine($"  AppointVersionCode: {request.AppointVersionCode}");
                Console.WriteLine($"  DevModelKey: {request.DevModelKey}");
                Console.WriteLine($"  DevKey: {request.DevKey}");
                
                // Serialize request body and print (using the same custom contract resolver as the client)
                string bodyStr = Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSettings);
                Console.WriteLine($"Serialized Request Body: {bodyStr}");
                Console.WriteLine("Note: The request body above matches the serialization format used in the actual request");
                
                var response = _client.WinUpgrade(request);
                Console.WriteLine("Request successful!");
                Console.WriteLine($"response.code: {response.Code}");
                Console.WriteLine($"response.msg: {response.Msg}");
                Console.WriteLine($"response.traceId: {response.TraceId}");
                
                // Print response data (using the same custom contract resolver as the client)
                if (response.Data != null)
                {
                    Console.WriteLine($"Data: {Newtonsoft.Json.JsonConvert.SerializeObject(response.Data, _jsonSettings)}");
                }
            }
            catch (Tea.TeaException ex)
            {
                Console.WriteLine("Request failed - TeaException Details:");
                Console.WriteLine($"Exception Message: {ex.Message}");
                
                // Use reflection to get internal properties of TeaException
                var properties = ex.GetType().GetProperties();
                foreach (var property in properties)
                {
                    try
                    {
                        var value = property.GetValue(ex);
                        Console.WriteLine($"{property.Name}: {value}");
                    }
                    catch (Exception)
                    {
                        // Skip properties that cannot be accessed
                    }
                }
                
                // Print the Data property of the exception
                if (ex.Data != null)
                {
                    Console.WriteLine("Exception Data:");
                    foreach (var key in ex.Data.Keys)
                    {
                        Console.WriteLine($"  {key}: {ex.Data[key]}");
                    }
                }
                
                Console.WriteLine($"Stack Trace: {ex.StackTrace}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Request failed: {ex.Message}");
                Console.WriteLine($"Exception Type: {ex.GetType().Name}");
                Console.WriteLine($"Stack Trace: {ex.StackTrace}");
            }

            // Assert: The test always passes because we only care about printing the results
            Assert.Pass("WinUpgrade request has been sent, results have been printed");
        }
        
    }
}

toolsetlink@163.com