Magento C#RestApi调用冻结我的应用程序

时间:2015-11-29 12:34:05

标签: c# rest magento

我正在尝试使用Magento C#RestApi库连接到我的Magento商店。 我成功连接到它,然后我想通过它的SKU获得产品。我选择寻找现有的SKU,我写了这段代码:

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寻找产品后立即冻结..."

  • 然后,我开始Fiddler看看发生了什么,并且惊讶,我的请求返回一个有效的回复......我希望我的应用程序在我的textBox中添加一个新行...但我的应用程序选择冻结

我的textBox在冻结之前包含了这个:

  

连接成功寻找产品   SKU:转换器,触摸屏

为什么回复永远不会回到我的应用程序?

所以这是我在TextView中收到的(按照Fiddler):

{
    "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"
    }
}

所以有回复

为什么这会冻结我的应用?我怎样才能解决这个问题,以便以某种方式解释它?

我希望能够从看似返回的JSON访问产品的属性

1 个答案:

答案 0 :(得分:1)

你的申请冻结了,因为:

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