facebook.Components.FacebookService.CreateSession(String authToken) times out

Oct 15, 2009 at 3:05 PM

I have an iFrame application and I am using FDT 2.1. The application is written using the MVC 1.0 framework.

Calling CreateSession(String authToken) times out randomly (lets say with a chance of about 20%, I am not really sure. It's kind of random). This is very annoying because we have advertisement to direct users to the application but some users fail to even add the application!

How should I fix this? 

Here is the stack trace logged with Elmah (http://code.google.com/p/elmah/):

System.Net.WebException: The operation has timed out
at System.Net.HttpWebRequest.GetRequestStream()
at facebook.API.postRequest(String requestUrl, String postString)
at facebook.API.SendRequest(IDictionary`2 parameterDictionary, Boolean useSession)
at facebook.auth.getSession(String auth_token)
at facebook.API.CreateSession()
at facebook.Components.FacebookService.CreateSession(String authToken)

 

Nov 9, 2009 at 8:07 PM

I modified the source code to catch the time out and recompiled the dlls. The time out issue is gone.

Developer
Nov 10, 2009 at 5:24 AM

Where did you modify that?  I am seeing a similar 20% drop rate on adding an app also, but it seems that facebook says that "Session key invalid or no longer valid" and sometimes will go into a redirect loop.  It looks as if it is a facebook bug from what I have read on thier forums for the redirect, http://bugs.developers.facebook.com/show_bug.cgi?id=6743

I thought this could be related to what you ran into.  Did you also see this error?  I also am running 2.1 and MVC.

Thanks,

Jim

 

Nov 10, 2009 at 6:39 AM

Find internal static WebResponse postRequest(string requestUrl, string postString) in Facebook\API.cs and replace it with the following and recompile the FDT solution to get new DLLs. See if that works for you. No guarantees. Use at your own risk.

Good luck.

 

        internal static WebResponse postRequest(string requestUrl, string postString)
        {

            var webRequest = WebRequest.Create(requestUrl);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";

            if (!String.IsNullOrEmpty(postString))
            {
                var parameterString = Encoding.ASCII.GetBytes(postString);
                webRequest.ContentLength = parameterString.Length;
                Stream buffer = null;
                int retryCounter = 0;
                try
                {
                    do
                    {
                        try
                        {
                            buffer = webRequest.GetRequestStream();
                            buffer.Write(parameterString, 0, parameterString.Length);
                            break;
                        }
                        catch (WebException e)
                        {
                            retryCounter++;
                            if (buffer != null)
                            {
                                buffer.Close();
                            }
                            Thread.Sleep(500);
                            if (retryCounter >= 3)
                            {
                                throw;
                            }
                        }
                    } while (true);
                }
                finally
                {
                    if (buffer != null)
                    {
                        buffer.Close();
                    }
                }
            }

            return webRequest.GetResponse();
        }