/// <summary> /// Executes a batch request asynchronously. /// </summary> /// <param name="facebookClient">The facebook client.</param> /// <param name="batchParameters">The batch parameters.</param> /// <returns>The task of the result.</returns> public static Task <object> BatchTaskAsync(this FacebookClient facebookClient, params FacebookBatchParameter[] batchParameters) { Contract.Requires(batchParameters != null); Contract.Requires(batchParameters.Length > 0); var tcs = CreateSource <object>(null); EventHandler <FacebookApiEventArgs> handler = null; handler = (sender, e) => TransferCompletionToTask <object>(tcs, e, () => e.GetResultData(), () => facebookClient.PostCompleted -= handler); facebookClient.PostCompleted += handler; try { facebookClient.BatchAsync(batchParameters, tcs); } catch { facebookClient.PostCompleted -= handler; tcs.TrySetCanceled(); throw; } return(tcs.Task); }
private void BatchRequestAsyncExample() { var fb = new FacebookClient(_accessToken); // since batch request is actually a POST request internally, // make sure to add the event handler for PostCompleted. fb.PostCompleted += (o, e) => { // incase you support cancellation, make sure to check // e.Cancelled property first even before checking (e.Error!=null). if (e.Cancelled) { // for this example, we can ignore as we don't allow this // example to be cancelled. // you can check e.Error for reasons behind the cancellation. var cancellationError = e.Error; } else if (e.Error != null) { // error occurred this.BeginInvoke(new MethodInvoker( () => { //MessageBox.Show(e.Error.Message); })); } else { // the request was completed successfully // now we can either cast it to IDictionary<string, object> or IList<object> // depending on the type. or we could use dynamic. dynamic result = e.GetResultData(); // note: batch requests doesn't support generic versions of e.GetResultData<T>() // make sure to be on the right thread when working with ui. this.BeginInvoke(new MethodInvoker( () => { // always remember to check individual errors for the batch requests. if (result[0] is Exception) MessageBox.Show(((Exception)result[0]).Message); dynamic first = result[0]; string name = first.name; // note: incase the omit_response_on_success = true, result[x] == null // for this example just comment it out //if (result[1] is Exception) // MessageBox.Show(((Exception)result[1]).Message); //if (result[2] is Exception) // MessageBox.Show(((Exception)result[1]).Message); //if (result[3] is Exception) // MessageBox.Show(((Exception)result[1]).Message); //if (result[4] is Exception) // MessageBox.Show(((Exception)result[1]).Message); //if (result[5] is Exception) // MessageBox.Show(((Exception)result[1]).Message); //if (result[6] is Exception) // MessageBox.Show(((Exception)result[1]).Message); //if (result[7] is Exception) // MessageBox.Show(((Exception)result[1]).Message); })); } }; fb.BatchAsync(new[]{ new FacebookBatchParameter { HttpMethod = HttpMethod.Get, Path = "/4" }, new FacebookBatchParameter(HttpMethod.Get, "/me/friend", new Dictionary<string, object> { { "limit", 10 } }), // this should throw error new FacebookBatchParameter("/me/friends", new { limit = 1 }) { Data = new { name = "one-friend", omit_response_on_success = false } }, // use Data to add additional parameters that doesn't exist new FacebookBatchParameter { Parameters = new { ids = "{result=one-friend:$.data.0.id}" } }, new FacebookBatchParameter("{result=one-friend:$.data.0.id}/feed", new { limit = 5 }), new FacebookBatchParameter().Query("SELECT name FROM user WHERE uid="), // fql new FacebookBatchParameter().Query("SELECT first_name FROM user WHERE uid=me()", "SELECT last_name FROM user WHERE uid=me()") // fql multi-query //,new FacebookBatchParameter(HttpMethod.Post, "/me/feed", new { message = "test status update" }) }); }