Exemple #1
0
        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" })
            });
        }