1
Vote

MyWebPagesStarterKit error if Twitter.com is down and Twitter Status Feed control is used.

description

MyWebPagesStarterKit version: 1.3.0
 
I built a page which uses the Twitter Status Feed section. Last night Twitter was doing scheduled maintenance, so their site was technically up, but not returning any data. My web site blew up in TwitterStatusFeed.cs in the GetTwitterXML function. Apparently there is no error handling for a situation where Twitter is there, but it refuses to hand over the status updates.
 
The error occurs on this line:
 
document.Load(reader);
 
I don't know much about C #, but I'm guessing this is because the reader comes back with nothing so the XMLDocument.Load method fails.
 
I think you can duplicate this error by editing this line and changing the URL to something that doesn't exist. Or you may have to use the correct URL and pass in an invalid UserName.
 
string xmlUrl = string.Format("http://twitter.com/statuses/user_timeline.xml?screen_name={0}&count={1}", Username, MaxItemsToShow);
 
I'm attaching a text file with the stack trace produced when I edit the line above to have an invalid URL. I'm not sure if that is the exact same error that I got when their site was down for maintenance, but I suspect it's the same situation.
 
Let me know if you need any more details.
 
-- Bobby Neal

file attachments

comments

dgett wrote Oct 27, 2010 at 7:54 PM

I was able to duplicate this issue and resolved it by replacing the following code from App_Code\Sections\TwitterStatusFeed.cs :
    public XPathNavigator GetTwitterXml(bool invalidateCache)
    {
        string xmlUrl = string.Format("http://twitter.com/statuses/user_timeline.xml?screen_name={0}&count={1}", Username, MaxItemsToShow);

        if (!invalidateCache && (HttpContext.Current.Cache[xmlUrl] != null))
        {
            return ((XmlDocument)HttpContext.Current.Cache[xmlUrl]).CreateNavigator();
        }
        else
        {
            XmlDocument document = new XmlDocument();
            XPathNavigator navigator;

            using (XmlTextReader reader = new XmlTextReader(xmlUrl))
            {
                document.Load(reader);
                navigator = document.CreateNavigator();
                formatXmlDocument(navigator);
            }
            HttpContext.Current.Cache.Add(xmlUrl, document, null, DateTime.MaxValue, TimeSpan.FromMinutes(5), System.Web.Caching.CacheItemPriority.Normal, null);
            return navigator;
        }
    }





I replaced the above code with the following(I added a try-catch to the original code):
    public XPathNavigator GetTwitterXml(bool invalidateCache)
    {
        string xmlUrl = string.Format("http://twitter.com/statuses/user_timeline.xml?screen_name={0}&count={1}", Username, MaxItemsToShow);

        if (!invalidateCache && (HttpContext.Current.Cache[xmlUrl] != null))
        {
            return ((XmlDocument)HttpContext.Current.Cache[xmlUrl]).CreateNavigator();
        }
        else
            try
            {
                XmlDocument document = new XmlDocument();
                XPathNavigator navigator;

                using (XmlTextReader reader = new XmlTextReader(xmlUrl))
                {
                    document.Load(reader);
                    navigator = document.CreateNavigator();
                    formatXmlDocument(navigator);
                }
                HttpContext.Current.Cache.Add(xmlUrl, document, null, DateTime.MaxValue, TimeSpan.FromMinutes(5), System.Web.Caching.CacheItemPriority.Normal, null);
                return navigator;
            }
            catch
            {
                return null;
            }
    }


After making the changes shown above the twitter status feed will not be visible in the event it is unable to retrieve the needed data.

Best wishes,

Dan