/// <summary>
        /// Registers a user with the given name and email.
        /// </summary>
        private void Register(string name, string email)
        {
            try
            {
                view.EnableControls(false);
                using (HttpClient client = CreateClient())
                {
                    // Create the parameter
                    dynamic user = new ExpandoObject();
                    user.Name  = name;
                    user.Email = email;

                    // Compose and send the request.
                    StringContent       content  = new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json");
                    HttpResponseMessage response = client.PostAsync("RegisterUser", content).Result;

                    // Deal with the response
                    if (response.IsSuccessStatusCode)
                    {
                        String result = response.Content.ReadAsStringAsync().Result;
                        userToken           = (string)JsonConvert.DeserializeObject(result);
                        view.UserRegistered = true;
                    }
                    else
                    {
                        Console.WriteLine("Error registering: " + response.StatusCode);
                        Console.WriteLine(response.ReasonPhrase);
                    }
                }
            }
            finally
            {
                view.EnableControls(true);
            }
        }
        /// <summary>
        /// Registers a user with the given name and email.
        /// </summary>
        private void Register(string name, string email)
        {
            view.EnableControls(false);

            // Create the parameter
            dynamic user = new ExpandoObject();

            user.Name  = name;
            user.Email = email;

            // Compose the request
            tokenSource = new CancellationTokenSource();
            StringContent content = new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json");

            // Send the request and deal with the response in a separate task
            Task task = Task.Run(() =>
            {
                using (HttpClient client = CreateClient())
                {
                    try
                    {
                        // Send the request
                        HttpResponseMessage response = client.PostAsync("RegisterUser", content, tokenSource.Token).Result;

                        // Deal with the response
                        if (response.IsSuccessStatusCode)
                        {
                            String result = response.Content.ReadAsStringAsync().Result;
                            userToken     = (string)JsonConvert.DeserializeObject(result);
                            view.Invoke((Action)(() => { view.UserRegistered = true; }));
                        }
                        else
                        {
                            Console.WriteLine("Error registering: " + response.StatusCode);
                            Console.WriteLine(response.ReasonPhrase);
                        }
                    }
                    catch (TaskCanceledException)
                    {
                    }
                    finally
                    {
                        view.Invoke((Action)(() => { view.EnableControls(true); }));
                    }
                }
            });
        }
Beispiel #3
0
        /// <summary>
        /// Registers a user with the given name and email.
        /// </summary>
        private async void Register(string name, string email)
        {
            try
            {
                view.EnableControls(false);
                using (HttpClient client = CreateClient())
                {
                    // Create the parameter
                    dynamic user = new ExpandoObject();
                    user.Name  = name;
                    user.Email = email;

                    // Compose and send the request.
                    tokenSource = new CancellationTokenSource();
                    StringContent       content  = new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json");
                    HttpResponseMessage response = await client.PostAsync("RegisterUser", content, tokenSource.Token);

                    // Deal with the response
                    if (response.IsSuccessStatusCode)
                    {
                        String result = await response.Content.ReadAsStringAsync();

                        userToken             = (string)JsonConvert.DeserializeObject(result);
                        view.IsUserRegistered = true;
                    }
                    else
                    {
                        MessageBox.Show("Error registering: " + response.StatusCode + "\n" + response.ReasonPhrase);
                    }
                }
            }
            catch (TaskCanceledException)
            {
            }
            finally
            {
                view.EnableControls(true);
            }
        }