Hello this is my first question here, I create a Web Service that is hosted on Azure… And I have a Windows phone client application and a windows 8 metro client application, now when I connected the wp7 application I used these methods to get something from the service on windows phone:
TimeTierBusiness.BusinessesClient client = new TimeTierBusiness.BusinessesClient();
client.GetAllCompleted += new EventHandler<TimeTierBusiness.GetAllCompletedEventArgs>(client_GetAllCompleted);
client.GetAllAsync();
and to get the result I simply go to the client_GetAllCompleted as you can see below:
void client_GetAllCompleted(object sender, TimeTierBusiness.GetAllCompletedEventArgs e)
{
listNearbyBusinesses.ItemsSource = e.Result;
myPopup.IsOpen = false;
}
Now on the windows 8 metro, there is no GetAllCompleted event which I can add to get a result, when I call the client on windows 8 all i get is the GetAllAsync() method which is awaitable…
any help would be much appreciated, as I can’t use this service on my metro app right now
Thanks 🙂
ok so the solution was, to create an async method, see the code below:
//My WCF Service Client
TimeTierBusiness.BusinessesClient bClient = new TimeTierBusiness.BusinessesClient();
//The list I am going to get from the service
public List<TimeTierBusiness.BusinessRatingViewModel> listBusinessViewModel;
This method to fill the list from the service asynchronously
private async void GetAllAsyc()
{
System.Collections.ObjectModel.ObservableCollection<TimeTierBusiness.BusinessRatingViewModel> x = await bClient.GetAllAsync();
listBusinessViewModel = x.ToList();
ItemListView.ItemsSource = listBusinessViewModel;
}
Windows 8 (actually, the .NET 4.5 that comes with it) has one of the most geek-boner enducing feature of all time.. It’s based on the await/async keywords and it essentially allows you to write asynchronous code as if it’s synchronous. Both at work and for pet projects, I end up doing a ton of async code and after seeing this feature, I decided that my next child to be born will be given to the .NET guys as a sacrifice (because the feature is just that good and because I already have 3 kids and don’t really want another one).
The jist of it is that you write the code for calling your async service (in this case) much like you would a regular method, but instead of hooking an event for completion, you simply “await” the result of the async call and in the line below it, continue working with the result – the compiler does some dark magic (if you ever used yield return, it’s a very similar type of dark magic) and turns your method into a series of methods + a state machine that will be called in the correct order.
Read more about it here