Skip to content

راه‌اندازی سریع C# SDK

این راهنمای راه‌اندازی سریع برای استفاده از C# SDK برای عملیات‌های معمول ارائه می‌دهد. شما یاد می‌گیرید که چگونه SDK را نصب کنید، اعتبارهای دسترسی را پیکربندی کنید، و عملیات‌های پایه‌ای برای دریافت آخرین اطلاعات به‌روزرسانی انجام دهید.

نکات

  • برای ارسال درخواست‌ها با استفاده از C# SDK، نیاز به راه‌اندازی یک نمونه Client دارید. این مقاله Client را با بارگذاری پیکربندی پیش‌فرض ایجاد می‌کند. برای گزینه‌های پیکربندی بیشتر، به Configuring the Client مراجعه کنید.

پیش‌نیازها

  • شما حساب UpgradeLink را ثبت کرده‌اید.
  • AccessKey و AccessSecret را به‌دست آورده‌اید.
  • استراتژی به‌روزرسانی برنامه Windows را پیکربندی کرده‌اید.

دریافت اعتبارها

img.jpg

نصب

نصب از طریق NuGet

  • اگر NuGet در Visual Studio شما نصب نشده است، لطفاً ابتدا NuGet را نصب کنید.
  • پس از نصب NuGet، پروژه جدیدی ایجاد کنید یا پروژه موجودی را در Visual Studio باز کنید، سپس <Tools> - <NuGet Package Manager> - <Manage NuGet Packages for Solution> را انتخاب کنید.
  • برای ToolsetLink.UpgradeLinkApi جستجو کنید، ToolsetLink.UpgradeLinkApi را در نتایج پیدا کنید، آخرین نسخه را انتخاب کنید، و بر روی Install کلیک کنید تا آن را به پروژه خود اضافه کنید.

نصب از طریق GitHub

روش ادغام پروژه

  • اگر SDK package یا کد منبع را از GitHub دانلود کرده‌اید و می‌خواهید از منبع نصب کنید، روی <Solution> راست کلیک کنید، و در منوی زمینه، بر روی <Add> -> <Existing Project> کلیک کنید.
  • در دیالوگی که ظاهر می‌شود، فایل upgradeLinkApiCSharp.csproj را انتخاب کنید و بر روی Open کلیک کنید.
  • بعداً، روی <Your Project> - <References> راست کلیک کنید، <Add Reference> را انتخاب کنید، در دیالوگی که ظاهر می‌شود، برگه <Projects> را انتخاب کنید، پروژه upgradeLinkApiCSharp را علامت بزنید، و بر روی OK کلیک کنید.

راه‌اندازی سریع

مثال زیر نحوه راه‌اندازی Client و دریافت آخرین اطلاعات به‌روزرسانی برای برنامه Win را نشان می‌دهد.

دریافت آخرین اطلاعات به‌روزرسانی برای برنامه Win

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