This project has moved and is read-only. For the latest updates, please go here.

Proper Exception Handling with Streaming API

Aug 2, 2013 at 4:19 PM
Edited Aug 2, 2013 at 4:24 PM
Hey Joe,

Great product, really appreciate your quick responses to users and the time you've spent on this code.

The goal for me is to have the streaming API to be constantly running without my interaction. If an exception occurs that breaks the process, I'd like it to reconnect and continue.

I have a problem with exception handling for the streaming API. How do I catch exceptions while using the streaming API?

Right now, I'm having two different unhandled WebExceptions being thrown:
  • The request was aborted: Could not create SSL/TLS secure channel
  • Something along the lines of the connection being closed
These exceptions are thrown randomly during runtime between a few minutes and several hours.

It's definitely happening behind the scenes since I have a try catch enclosing my StreamingCallback lambda expression that isn't hit. Because it's in your code on a separate thread, the only way I really know how to catch it is to register an UnhandledExceptionEventHandler. However, that really isn't the solution I need considering how I can't stop the application from crashing when it reaches that point.

What do?


P.S. For what it's worth, I have this process working on it's own thread. Shouldn't make a difference, but I figured I'd mention it.
Aug 3, 2013 at 8:26 PM
Hi Bill,

LINQ to Twitter uses APM, so you can't catch an exception that way, but I do have a hook to let you know when an exception occurs. There's a TwitterErrorStatus propery in the callback parameter named Status that you can check to see if an exception occurred. Here's how I used it for the Sample stream:
        static void SamplesDemo(TwitterContext twitterCtx)
            Console.WriteLine("\nStreamed Content: \n");
            int count = 0;

            (from strm in twitterCtx.Streaming
             where strm.Type == StreamingType.Sample
             select strm)
            .StreamingCallback(strm =>
                if (strm.Status == TwitterErrorStatus.RequestProcessingException)

                Console.WriteLine(strm.Content + "\n");

                if (count++ >= 10)
The StreamingCallback parameter is type StreamContent with a couple properties, Status and Error. Status is type TwitterErrorStatus and tells you whether the current response is valid or contains an error from Twitter, such as the RequestProcesssingError you see above. If you detect an Exception, you can use the Error property to access the exception and perform whatever processing you need.

When Twitter closes the stream on you, Status will be TwitterErrorStatus.RequestProcessingError and Error will contain a WebException. To re-start, you'll need to create a new instance of TwitterContext and reconnect. Twitter has some guidance on reconnecting, on their Streaming APIs page, with recommended back-off strategies that's good to review for anyone doing streaming.