jeudi 15 février 2018

Web API CRUD with .net Client

I am trying to build a client desktop app to perform CRUD operations on a Web API, I've been able to read, but when I am trying to create a new item, static HttpClient client = new HttpClient();

    static async Task RunAsync()
    {
        // Update port # in the following line.
        client.BaseAddress = new Uri("http://localhost:61895/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

        try
        {
            HttpResponseMessage response = client.GetAsync("api/products").Result;  // Blocking call!  
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Request Message Information:- \n\n" + response.RequestMessage + "\n");
                Console.WriteLine("Response Message Header \n\n" + response.Content.Headers + "\n");
                // Get the response
                var productJsonString = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Your response data is: " + productJsonString);

                // Deserialise the data (include the Newtonsoft JSON Nuget package if you don't already have it)
                deserialized = JsonConvert.DeserializeObject<IEnumerable<Product>>(productJsonString);
                success = true;
            }
            else
            {
                success = false;
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }

    }

I am getting this error:

This instance has already started one or more requests. Properties can only be >modified before sending the first request.




Aucun commentaire:

Enregistrer un commentaire