I am trying to use Magento C# RestApi library to connect to my Magento Store. I succeed to connect to it, then I want to get a product by it's SKU. I chose to look for an existing SKU, and I wrote this code:
private void button1_Click(object sender, EventArgs e)
{
var client = new MagentoApi()
.SetCustomAdminUrlPart("index.php/admin")
.Initialize("http://www.example.com/magento/",
"57348583384fh8h83h4334h34", "8vh388hhfh487f34h8hiuw3")
.AuthenticateAdmin("admin", "adminpassword");
textBox1.AppendText("Connection successfull \n");
Application.DoEvents();
textBox1.AppendText("Looking for product with sku:" + "convertor-touchscreen \n");
Application.DoEvents();
var response = client.GetProductBySku("convertor-touchscreen").Result;
textBox1.AppendText("Done locating product \n");
Application.DoEvents();
...
}
At first I ran my app, and it froze right after displaying the "Looking for product with sku..."
Then, I started Fiddler to see what is happening, and surprise, my request returns a valid response... I expect my app to add a new line to my textBox... but my app chooses to freeze.
My textBox contains this before it freezes:
Connection successfull Looking for product with sku:convertor-touchscreen
Why does the response never come back to my app?
So here is what I receive (as per Fiddler) in TextView:
{
"10": {
"entity_id": "10",
"attribute_set_id": "4",
"type_id": "simple",
"sku": "convertor-touchscreen",
"name": "Convertor touchscreen",
"meta_title": null,
"meta_description": null,
"url_key": "convertor-touchscreen",
"custom_design": null,
"page_layout": null,
"options_container": "container1",
"country_of_manufacture": null,
"msrp_enabled": "2",
"msrp_display_actual_price_type": "4",
"gift_message_available": null,
"creareseo_discontinued": null,
"creareseo_discontinued_product": null,
"description": "Convertor touchscreen",
"short_description": "Convertor touchscreen",
"meta_keyword": null,
"custom_layout_update": null,
"price": "421.0000",
"special_price": "380.0000",
"weight": "0.1500",
"msrp": null,
"special_from_date": "2015-11-24 00:00:00",
"special_to_date": "2015-11-26 00:00:00",
"news_from_date": null,
"news_to_date": null,
"custom_design_from": null,
"custom_design_to": null,
"status": "1",
"visibility": "4",
"tax_class_id": "2",
"featured": "1"
}
}
So there is a response
Why does this freeze my app? How can I get past this so I can get to interpreting it somehow?
I want to be able to access the attributes of the product from the JSON that appears to return
Your application is freezing because this:
var response = client.GetProductBySku("convertor-touchscreen").Result;
Is causing your code to deadlock. You're blocking on async code. Instead, asynchronously wait on the asynchronous method using await
:
private async void button1_Click(object sender, EventArgs e)
{
var client = new MagentoApi()
.SetCustomAdminUrlPart("index.php/admin")
.Initialize("http://www.example.com/magento/",
"57348583384fh8h83h4334h34", "8vh388hhfh487f34h8hiuw3")
.AuthenticateAdmin("admin", "adminpassword");
textBox1.AppendText("Connection successfull \n");
textBox1.AppendText("Looking for product with sku:" + "convertor-touchscreen \n");
var response = await client.GetProductBySku("convertor-touchscreen");
textBox1.AppendText("Done locating product \n");
}
Side note:
There's really no reason to use Application.DoEvents
here.