Exemple #1
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ThreadException += new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod);
            try
            {
                DialogResult result;
                using (var loginForm = new LoginForm())
                {
                    result = loginForm.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        var server     = "localhost";
                        var port       = 50000;
                        var ipHostInfo = Dns.GetHostEntry(server);
                        var ipAddress  =
                            ipHostInfo.AddressList.FirstOrDefault(t => t.AddressFamily == AddressFamily.InterNetwork);
                        if (ipAddress == null)
                        {
                            throw new Exception("No IPv4 address for server");
                        }

                        var data = new Request()
                        {
                            Function  = Functions.Authorization,
                            Attribute = new List <Attribute>()
                            {
                                new Attribute()
                                {
                                    Name  = "login",
                                    Value = loginForm.textBox1.Text
                                },
                                new Attribute()
                                {
                                    Name  = "password",
                                    Value = loginForm.textBox2.Text
                                }
                            }
                        };

                        var testClient = new TestClient(new TcpClient());
                        testClient.Connect(server, port);
                        var response = testClient.SendRequest(data);
                        if (response.Result.Code == 0)
                        {
                            Application.Run(new MainForm(testClient));
                        }
                        else
                        {
                            throw new Exception("Пользователь не существует");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        private async void button1_Click(object sender, EventArgs e)
        {
            var request = new Request {
                Function = Functions.GetGoodsList
            };

            var result = await testClient.SendRequest(request);

            var goods = result.Data.Nested.Data.Select(x => new Goods()
            {
                Id          = Convert.ToInt32(x.Input.Single(o => o.Key == nameof(Goods.Id)).Value),
                Name        = x.Input.Single(o => o.Key == nameof(Goods.Name)).Value,
                Description = x.Input.Single(o => o.Key == nameof(Goods.Description)).Value,
                Price       = Convert.ToDecimal(x.Input.Single(o => o.Key == nameof(Goods.Price)).Value, CultureInfo.InvariantCulture)
            }).ToList();

            dataGridView1.DataSource = goods;
        }