WooCommerce.NET icon indicating copy to clipboard operation
WooCommerce.NET copied to clipboard

GetAll() Problem

Open manossmar opened this issue 4 years ago • 7 comments

Hello,

I have a problem getting started with this Nuget.

the problem is with the await operator. This is a clear and new project just to make some tests.

please take a look at the attached picture. Any help will be greatly appreciated

WooCommerce.NET version 082

Screenshot 2021-03-11 101009

manossmar avatar Mar 11 '21 08:03 manossmar

try var products = wc.Product.GetAll().Result;

XiaoFaye avatar Mar 11 '21 08:03 XiaoFaye

Thank you for your prompt reply. I still get the same message

On Thu, Mar 11, 2021 at 10:16 AM James Yang [email protected] wrote:

try var products = wc.Product.GetAll().Result;

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/XiaoFaye/WooCommerce.NET/issues/577#issuecomment-796555009, or unsubscribe https://github.com/notifications/unsubscribe-auth/AS7LT5D6OGYDOY3SWP6SZADTDB35ZANCNFSM4Y7ZFZDA .

manossmar avatar Mar 11 '21 08:03 manossmar

Actually now I get a runtime error Screenshot 2

manossmar avatar Mar 11 '21 08:03 manossmar

Never mind that. Everything works ok. thanks for your great help

manossmar avatar Mar 11 '21 08:03 manossmar

For the record, afaik, to use await, the function signature needs to be: public async Task<T> SomeAsyncFuction()

LetsCNow avatar Mar 11 '21 13:03 LetsCNow

AFAIK a WPF event handler should be async void and then you could await within this event handler.

(about the only place where you should use async void instead of async Task)

So an alternative solution:

private async void button1_Click(object sender, EventArgs e)
{
    var products = await wc.Products.GetAll();
    /* ... */
}

OP, please be careful with stuff such as Result, Wait, etc, because they can bite you if you don't know their idiosyncrasies, see for example https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

gaazkam avatar Mar 14 '21 09:03 gaazkam

You'll probably want to read up on asynchronous programming as well. https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

ColdFire169 avatar Mar 19 '21 14:03 ColdFire169