Use C# to develop server-side programs

WebAPI.Client provides network transmission data between the receiving server and the device, which is developed by Asp.Net Core SignalR technology.


Use WebAPI.Client to develop server-side Console Application.
  1. Use Visual Studio 2022 to develop software, create a new Console Application project
  2. Add project reference:IoT.API.WebApi.Client.dll
  3. Find Program.cs and add the following code to the Main method:
    using System;
    using System.Threading.Tasks;
    using System.Threading;
    using Newtonsoft.Json;
    using IoT.API.WebApi.Client;
    
    namespace IoT.Example.ConsoleApp
    {
        internal class Program
        {
            public static CancellationTokenSource _cancellationTokenSource = null;
            static void Main(string[] args)
            {
                Console.WriteLine("Initializing...");
                AppClient.GetInstance().Initialize("9b90ddc743c44125a67f0f847749b19c", "User-3", "NGY5OTAwMzRkNWZlNDI4MzgzZDc0ODBkZGQwZDEzNmM=");
    
                Console.WriteLine("Starting...");
                var terminal = AppClient.GetInstance().PhysicalManager;
                terminal.Connect();
                terminal.OnReceiveMessage += (obj) => {
                    Console.WriteLine(obj);
                };
                Console.WriteLine("Has started");
                
                _cancellationTokenSource = new CancellationTokenSource();
                Task.Run(async () => {
                    while (!_cancellationTokenSource.IsCancellationRequested)
                    {
                        terminal.Send("Get", AppClient.GetInstance().GetToken(), "869627031275308");
                        await Task.Delay(1000);
                    }
                });
    
                Console.WriteLine("");
                Console.WriteLine("Press key 'q' to stop the Service.");
                Console.WriteLine("-------------------------------------------------------------------");
                while (Console.ReadKey().Key != ConsoleKey.Q)
                {
                    Console.WriteLine("");
                    continue;
                }
    
                _cancellationTokenSource.Cancel();
    
                Console.WriteLine("");
                Console.WriteLine("The Service has been stopped!");
            }
    
        }
    }
    
                        
  4. After completing the above operations, you can use the method in AppClient.GetInstance().PhysicalManager to call the data in the program. Please refer to the sample program for details.



Download the Console sample program

The sample program, the operating environment requirements are .Net Core 5

  1. Example Source Code:https://cloud.tzonedigital.cn/f/dev/Example.ConsoleApp/source_code.zip