If I do a call to the server that it could take a lot of time, it’s recommended the use of async methods in the server contract.
In this case, to create the proxy, I have to indicate that I want to create the async methods.
However, I was wondering that If I use threading in the client to make the calls to the service, perhaps it’s not needed to use async methods, because this secondary thread can wait the result without blocking the client. I am using .NET 4.0, so I can use tasks.
This is correct? If this is correct, what is the best way, use async methods or use thrading in the client?
Thanks.
Daimroc.
I would personally recommend async methods, mostly as that’s the direction Microsoft is clearly taking with .NET 4.5 – there’ll be language support in C# 5 to make it really simple to consume asynchronous APIs. Your client code won’t need to worry about marshaling between threads to make sure the UI is only updated in the right thread, creating threads separately etc – it can just “await” the service results, and it will “do the right thing”.
There’s nothing magical in this support – any fundamental underlying concurrency issues will still need to be handled – but the platform will take care of the error-prone boilerplate for you, which will make it much easier to see and handle the inherent, unavoidable complexity.