Facebook C# SDK - Glimpse into the Future

The Move

I'm excited to announce that Facebook C# SDK will be moving to a new home at Github (https://github.com/facebook-csharp-sdk/facebook-csharp-sdk) along with SimpleJson (https://github.com/facebook-csharp-sdk/simple-json).

Moving forward we will be using Github for source control and bugs/suggestions (all bugs from codeplex have already been migrated) and Github pages for documentation (https://github.com/facebook-csharp-sdk/facebook-csharp-sdk.github.com). Stackoverflow with tag "facebook-c#-sdk" will be used for asking questions.

NuGet packages will be used as primary distribution for compiled binaries. This means Github downloads will not contain all the releases for compiled binaries but rather the most recent binaries only. Developers using Visual Studio earlier then 2010 or Express Editions will require to use the nuget command line tool.

The Future

With the move to Github we are also announcing a pre-release version of vNEXT (v6.0.1-alpha). A lot has happened in the past few months with introduction to Enhanced OAuth Dialog, Open Graph and ETags to name a few that a time has come for us to rethink how we develop Facebook apps. This has lead us to completely rewrite v6 from scratch.

Tons of features has been added to v6 along with removal of existing features which doesn't make much sense in the present context.

Anonymous Objects as Parameters

One of the most exciting feature to land in v6 is using anonymous object as parameters rather then Dictionary<string,object> or ExpandoObject. (Dictionary and ExpandoObject will continue to work).

var fb = new FacebookClient();
dynamic result = fb.Get("4", new { fields = new[] { "id", "name" } });

GZip/Deflate

GZip/Deflate has been enabled for desktop client profiles (.NET 3.5+). Currently only batch requests returns gzip responses. (https://developers.facebook.com/bugs/235553716524315)

FacebookMediaStream

Rather then converting stream to byte array and using FacebookMediaObject, you can now directly work with FacebookMediaStreams. Stream used in FacebookMediaStream must be manually disposed.

var fb = new FacebookClient(accessToken);
string attachementPath = @"C:\image.jpg";

using (var stream = File.OpenRead(attachementPath))
{
    dynamic result = fb.Post("me/photos",
                                new
                                    {
                                        message = "upload using Facebook C# SDK",
                                        file = new FacebookMediaStream
                                                {
                                                    ContentType = "image/jpg",
                                                    FileName = Path.GetFileName(attachementPath)
                                                }.SetValue(stream)
                                    });
}

ETags

You can now optimize your graph api’s using ETags. For etag you need to pass the special parameter called _etag_

For the first request it should be empty string. _etag_ is specific to Facebook C# SDK.

dynamic result1 = fb.Get("me", new { fields = "id,name", _etag_ = string.Empty});

This will tell the fb c# sdk to return a JsonObject with headers and body.

dynamic headers = result1.headers; // JsonObject of headers.
dynamic body = result1.body; // The actual json response.
// to access the actual json response use result.body.x instead of just result.x
string id = result1.body.id;

Then you can use the etag from the previous response to get the next responses.

dynamic result2 = fb.Get("me", new {fields = "id,name", _etag_ = result1.headers.ETag});
dynamic headers = result1.headers;
// always check if the response has a body.
if(result2.ContainsKey("body")) {
    // we've got the updated response.
    string id = result1.id;
}
else {
    // the last request was the latest.
    // so do nothing.
}

Note: result1.header.ETag (make sure ETag contains the right capitalization). It is exactly how Facebook returns the response header.
when _etag_ is string.Empty it will always return a body, so you don't need to check result1.ContainsKey("body") for it.

ETag support in batch requests

var fb = new FacebookClient(accessToken);

dynamic result = fb.Batch(
    new FacebookBatchParameter("me", new { _etag_ = string.Empty }),
    new FacebookBatchParameter("me", new { _etag_ = "\"ac9e51b60e883e294cc98f35f70a1ec8fdf0e736\"" }),
    new FacebookBatchParameter("me") { Data = new { headers = new Dictionary<string, object> { { "If-None-Match", "\"ac9e51b60e883e294cc98f35f70a1ec8fdf0e736\"" } } } });

Removal of Query/QueryAsync/QueryTaskAsync method

Now that Facebook official supports Fql using graph api, we are removing the Query methods from FacebookClient.

In v6 you can execute Fql query using the following methods.

Single Fql

var result = fb.Get("fql", new { q = "SELECT uid from user where uid=me()" });

MultiQuery Fql:

var resultMulti = fb.Get("fql", new
                                    {
                                        q = new[]
                                                {
                                                    "SELECT uid from user where uid=me()",
                                                    "SELECT name FROM user WHERE uid=me()"
                                                }
                                    });

Named Muliquery:

var resultMultiNamed = fb.Get("fql",
                            new
                                {
                                    q = new
                                    {
                                        id = "SELECT uid from user where uid=me()",
                                        name = "SELECT name FROM user WHERE uid IN (SELECT uid FROM #id)",
                                    }
                                });

Removal of Facebook.Web.dll and Facebook.Web.Mvc.dll

Starting from v6, we are depreciating Facebook.Web.dll and Facebook.Web.Mvc.dll and will no longer be providing it. Over the year we have seen lot of developers using the server side (ASP.NET) as a proxy to Facebook rather then using the Facebook Javascript SDK. We highly encourage users to use both Facebook Javascript SDK and Facebook C# SDK where appropriate. We will continue to support earlier version in v5 branch. Rather then provide Facebook.Web.dll and Facebook.Web.Mvc.dll we will be creating a new repositories in github showing best practices on how to create Facebook apps, Canvas Apps, Windows Phone Apps.

Attributes such as CanvasAuthorize doesn’t make much sense now as the user is now allowed to remove certain permissions and yet grant access to your app when using Enhanced OAuth Dialog. Currently with v5, you will end up with redirect loop incase the user removes the permission.

Since decoding Facebook cookies are not official supported nor documented by Facebook, we are also removing FacebookWebClient. Lot of bug reports in Facebook C# SDK has been based on cookie issues. Starting from v6, you will have to use the Facebook Javascript SDK to get the access token and pass it to the server using secure https connection or use the Facebook OAuth Login Dialog.

Decoding signed request (ParseSignedRequest/TryParseSignedRequest) has been moved to FacebookClient instead.

var fb = new FacebookClient();
dynamic signedRequest = fb.ParseSignedRequest("app_secret", Request.Params["signed_request"]);

Documentation

You can find the documentation of Facebook C# SDK at http://docs.csharpsdk.org/. You can fork https://github.com/facebook-csharp-sdk/facebook-csharp-sdk.github.com/tree/master/docs repository and send us pull requests.

Although we think the current pre-release of v6 has been highly improved in the API point of view, we are still open to changes depending on the feedback we receive. We would love to hear your feedback before the beta release and the final RTW.

Install-Package Facebook -version 6.0.1-alpha

Make sure to also read the Facebook C# SDK Roadmap by Nathan Totten for more details at http://blog.ntotten.com/2012/02/07/facebook-c-sdk-road-map/.


blog comments powered by Disqus