2015-11-29 50 views
1

我想使用Magento的C#RESTAPI庫連接到我的Magento商店。 我成功地連接到它,然後我想通過它的SKU獲得產品。我選擇了去尋找現有的SKU,我寫了這個代碼:Magento的C#RESTAPI調用凍結我的應用程序

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(); 
    ... 
} 
  • 起初我跑我的應用程序,它顯示後立即凍結「尋找與SKU產品......」

  • 然後,我開始小提琴手,看看發生了什麼,和驚喜,我請求返回一個有效的迴應...我希望我的應用程序到一個新行添加到我的textBox ......但我的應用程序選擇凍結。

我textBox中包含該凍結之前:

連接全成尋找產品與 SKU:轉換器,觸摸屏

爲什麼響應再也回不來了我的應用程序?

所以這是我收到(按提琴手)中的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" 
    } 
} 

所以有響應

爲什麼會出現這種凍結我的應用程序?我怎麼能通過這個,所以我可以以某種方式解釋它?

我希望能夠從出現到返回

回答

1

您的應用程序凍結,因爲這個JSON訪問該產品的屬性:

var response = client.GetProductBySku("convertor-touchscreen").Result; 

導致你的代碼死鎖。 You're blocking on async code。相反,異步等使用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"); 
} 

旁註:

還有的實在沒有理由在這裏使用Application.DoEvents

+0

「等待操作者只能異步方法中使用」 – user1137313

+1

@ user1137313你標記與'async'修改你的方法,我在回答了什麼? –

+0

非常感謝。我只是浪費在這:( – user1137313