calling webapi from windows client - c# - set datagridview.datasource - JArray- task await-


wepapideki listeyi windows client uygulaması datagridview datasource olarak atamak.


** winform *GetProductAsync*
private HttpClient client = new HttpClient();

private string apiListAdress = "http://localhost/WapiNW/api/Products/GetUrunListesi";

private void FormListe_Shown(object sender, EventArgs e)
{

client.BaseAddress = new Uri(apiListAdress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

}

private async void buttonListele_Click(object sender, EventArgs e)
{
var product = await GetProductAsync(apiListAdress);
try
{
//..............................
this.dataGridView1.DataSource = product; 
//..............................
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}


private async Task<JArray> GetProductAsync(string path)
{
  //............................
HttpResponseMessage responseMSG = await client.GetAsync(path);
//............................ 
if (responseMSG.IsSuccessStatusCode)
{
var webR = (HttpWebRequest)WebRequest.Create(path);
var response = (HttpWebResponse)webR.GetResponse();

var reader = new StreamReader(response.GetResponseStream());
string s = reader.ReadToEnd();

var product = JsonConvert.DeserializeObject<JArray>(s);

return product;

}

return null;

}


** webApi*GetUrunListesi*
//tablo : Northwind database Products ..

public IEnumerable GetUrunListesi()
{
var lst = (from u in db.Products
  select new
{
u.ProductID
,u.ProductName
,u.UnitPrice
,u.SupplierID
,u.CategoryID
,u.QuantityPerUnit
,u.UnitsInStock
,u.UnitsOnOrder
,u.ReorderLevel
,u.Discontinued
,u.Categories.CategoryName
,u.Categories.Picture
}).ToList();


return lst;
}



** webApi*WebApiConfig*
 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
             config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute("api/Products/GetUrunListesi"
, "api/Products/GetUrunListesi"
, new { Controller = "Products"
, Action = "GetUrunListesi" }
);

 
config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            ); 
        }



    }

Yorumlar