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

WebForms Handling/Scoping User Stream Data

Apr 30, 2014 at 5:17 AM
Edited Apr 30, 2014 at 5:18 AM
I have a WebForms page that starts a User Stream as shown below (inside a button click event handler).

For testing I am trying to just grab the json as a string and add it to a List<string>. At run time the stream content is present and is added to the List, but when the stream the List count appears as 0.

I am seeing this as a scoping problem of some kind but can't see why this wold be.

How should this be handled - are we forced to persist data to disk at the time the Status shows up?
   int count = 0;
   int max = 5;
   List<string> lines = new List<string>();
   var s = (from strm in twitterCtx.UserStream
                where strm.Type == UserStreamType.User
                select strm).StreamingCallback(strm =>
                     if (strm.Status == TwitterErrorStatus.RequestProcessingException)
                          WebException wex = strm.Error as WebException;
                          if (wex != null && wex.Status == WebExceptionStatus.ConnectFailure)
                               throw (wex);
                          throw new Exception(strm.Error.ToString());
                   if (++count >= max)

        //right here the count is zero
        int lc = lines.Count;
Apr 30, 2014 at 4:56 PM

With WebForms (or MVC) the objects you're working with in memory only exist for the lifetime of the current request. Therefore, you set up your stream object each time and after that, it goes away. The effect is that you might get a message or two from the stream, which would be rare because the request will likely complete before the stream can initialize, but you probably won't see anything.

So, the solution is to ensure your stream stays in memory all the time. One way to do this is with a Windows Service, which starts when the computer starts and doesn't close unless you manually close it or the computer shuts down. Another option is to put it in an Azure Cloud Worker Process. The goal is to get the stream running in an environment where it won't stop unless you explicitly tell it to do so.

As you might be thinking, this will require a lot more infrastructure because now you need a way to get tweets from the stream to your application, which means you could use some type of message queuing or service bus. Then you have to display the data on the screen and you would be looking at something like SignalR, ajax polling, or page refreshes.

Apr 30, 2014 at 8:21 PM
Thanks Joe,

Oh of course! The UserStream creates its own HTTP Request/Context outside of the current WebForms context, is that right?

This was for testing we intent to go to a Windows Service/MSMQ configuration but figuring out data structures (since the Stream parser no longer works) seemed easier to do outside of a service.

Is there documentation for the various Json objects returned by the L2T User Stream?

Thanks for all the work on LinqToTwitter Joe!
Apr 30, 2014 at 9:27 PM
Yes, here's some information on the streaming message types:

And here's Twitter's field Guide:

I have an open issue to add a stream parser:

There's also another stream parser project, referenced in the issue above that might be helpful.

Apr 30, 2014 at 10:18 PM
Thanks Joe, much appreciated!