ASP.NET, Threading and Application Pools

Thread Abortion
Why is my app working in my Windows XP Pro machine and not on the Windows Server 2003 production machine? I have two ASP.NET apps that run on the same server and interact between one another. Why? Because eventually they will go their separate ways behind a load balancer onto many machines. And I was logging unexplained ThreadAbort exceptions.

Clue: IIS 6 and Application Pools
After working on fine tuning my code nearly all day and still getting the same result, it dawned on me that the two unique Application Pools that I had created on which the two ASP.NET apps would run had properties. Doh! Check out the properties.

Application Pool Properties
I made the following changes on the assumption that because each request to the application would launch work on a ManagedThreadPool and return something immediately. Because of that, the settings of the application pool would allow the process to be killed and/or cycled while executing on those working threads and generate the unexpected ThreadAbort exception.

  • Recycling tab - disable the check boxes
  • Performance tab - diable the check boxes
  • Health tab - uncheck "Enable rapid-fail protection"

Positive Result
Ran the test again and alakazam! No ThreadAbort exceptions.

Threading on ASP.NET
There have been several issues I've run into over the past few months in dealing with handling work after the request has long since been sent back to the client. Using the ThreadPool vs the ManagedThreadPool from Stephen Toub and this Application Pool thing have been among the trickiest.

What are the dangers?
I'm sure there are dangers in disabling the safety net around the Application Pool. So I'm making sure that these two apps run on their own. I may experiment with re-enabling them one at a time and observing the result. If you have any advice for me on the matter, I'd love to hear from you.

RssJam.com - My Foray Into Feeds

A month or two ago, I started looking for an RSS client again. I had tried some a year or two ago but never really liked them. I now have greater reason to keep track of a few blogs and other RSS sources. There seem to be quite few now.

All the Readers a Local Client
Okay. Not all, but nearly all of the RSS readers are local clients which means installing another piece of software on a machine already full of it. What I wanted was a daily or thrice daily email with updates on all my favorite feeds. Simple. But none of the online sites I browsed provided what I wanted.

Rolling My Own
So I had a free weekend on my hands. I could have spent it fishing but I don't fish. I could have spent it golfing but I don't golf anymore. So I registered RSSJAM.COM and wrote a little file based ASP.NET app that would do what I wanted.

Daily Emails
Now I get three emails a day with a nice HTML formatted message that shows me my feeds with the stories that have been posted in the last 24 hours listed at the top.

Explosive Growth
After a month or two of telling a few people about it, only 16 people have signed up to use RssJam for free, so I don't expect it to be a huge hit or make me a ton of money. But it's a fun and useful tool. And for those of you who spent the time to read this post, if you want, just shoot me an email and ask for the code. I'll be happy to zip it up and send it to you. Who knows, you might set up your own site inside your team to keep track of all your teams blogs. Or whatever.

The link to email me is at the bottom of the left column here.

.NET Regex Fixed on x64

The Bug - Submitted June 5, 2006
On June 5, 2006, I submitted a bug to Microsoft their Connect site and expected that it was an exercise in futility. Who would listen to me? Here's what I submitted to them.

Title: Long pattern string results in race condition on x64 system but not x86 system
A long pattern string in a Regex constructor works fine on my x86 Windows XP development machine but results in a race condition that eats RAM very fast until an "out of memory" condition occurs an the process is killed on the x64 Windows Server 2003 machine. In steps below, I will cut and paste the code which resulted in the condition--the input exceeds 2000 characters so I will remove some of the lines that concatenate the pattern string but one can easily add additional lines to achieve the result that I experienced. To resolve it or work around it, I simply split the Regex into 19 Regex objects and that resolved the problem.

This happened after developing a pattern to remove unwanted words from text before I run the keyword extraction algorithm I wrote based on this whitepaper by a brilliant young researcher named Yutaka Matsuo and the honorable professor Mitsuru Ishizuka at the University of Tokyo.

Fixed - July 7, 2006
I received an email from the Connect system to let me know the bug had been fixed. Of course, it was a nice, poorly formatted plain text email spit out by the system. Where's the love? Still, I was impressed that the system (hence somebody who programmed the system) bothered to send a note to let me know that the bug was confirmed and fixed.

I'm sure a million or more of my readers have already enjoyed this experience, but for those small few that have not, I thought I'd share. So don't hold back. When you find a bug in the framework or just want to complain, visit the Connect site and see what happens.

 

HttpWebRequest, ServicePoint and BindIPEndPointDelegate

I've spent months fighting with 3rd party components and my own hand rolled code that would allow me to achieve essentially the same thing you can with the HttpWebRequest class with one additional feature: binding a specific end point (IP address) to the object so that a remote server would recognize the call as coming from that specific IP address.

I looked through the description of each of the HttpWebRequest's members in the MSDN documentation. I searched the internet high and low. I banged my head against buggy third party components. And in desperation attempted to roll my own. All efforts resulted in a thorougly disappointing result.

Bring on the Delegates

Two days ago, I was chatting with a friend and I expressed my desperation. I told him I'd drive to his house and give him a fifty dollar bill if he could tell me how to use the HttpWebRequest object and assign or "bind" the local IP. He started digging. And I tried one last Google search using HttpWebRequest and IPEndPoint, I think it was, in the search. And I found this little gem about 5 seconds before my friend suggested taking a look at the HttpWebRequest's ServicePoint member and the ServicePoint's BindIPEndPointDelegate member.

Glory Hallelujah!

One of my new best friends is someone I don't know: Malar Chinnusamy. Thanks a million, Malar.

So to the point of the title of this little rant. It is the delegate stupid. The lesson learned is when you can't figure out how to do something with the .NET Framework, check out the delegates. And the delegates of the object's members. And so on...

public delegate IPEndPoint BindIPEndPoint(ServicePoint servicePoint,
  IPEndPoint remoteEndPoint, int retryCount);

That little gem is a beautiful thing. It lead to this:

private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint,
  IPEndPoint remoteEndPoint, int retryCount)
{
    if(retryCount < 3)
        return new IPEndPoint(IPAddress.Parse("192.168.10.60"), 0);
    else
        return new IPEndPoint(IPAddress.Any, 0);
}

and...

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ServicePoint.BindIPEndPointDelegate =
  new BindIPEndPoint(BindIPEndPointCallback);

I told my friend that I wish I knew and understood everything in the framework. He said that he wished I knew it all too. So, when there's a shortage of helps and great blog posts out there, don't give up. Follow the delegates and you won't look as stupid as I felt once I found my new friend's post.

Code Fonts and ClearType

I ran across the Consolas font from Microsoft today. I love it. I'd been using Courier New in 9pt and have now switched to Consolas 10pt. Much easier on the eyes. But not at first. It displayed quite poorly until I enabled ClearType in my display settings.

I've been using LCD monitors for a long time and why I've not switched to ClearType, I'll never know. It's like my glasses suddenly became more effective.

How many other cool things have I missed? One can never really know.

Geek Dinner and Superman

Last Thursday I attended my first Geek Dinner. Great food. Good conversation. And then we went to the Wynnsong and watched Superman Returns in our own reserved theater thanks to their great management. And many thanks to Phil Burns and DevUtah for hosting it.

Dinner was at Tucanos in Provo. Great food. Fun atmosphere.

The movie was vintage Superman. I expected more but my kids will love it.

And if you live in Utah, come join us for the next Geek dinner. If you don't, then start your own. Way fun!

Does Pay Per Click Work?

I"m not a marketing guru. Never have been. Never will be. You too? So how do we maximize traffic to our blog, our side project, or our main gig? Well, we tell our clients to hire us, the expert, when they need some coding done.

So hire an expert.

I know just the expert. I've watched these guys in action. They know what they're doing. Check them out at http://www.webevident.com/ppc-management.php.

They can handle all your pay per click campaigns. And you would be surprised how much traffic they can drive to your site on a very tight budget. They do a free analysis for you, so you have nothing to lose by at least checking them out.  

A Requirements Management Allegory

My wife won the lottery. Two hundred thousand dollars. Uncle Sam took half. She said, "I want a new car. Go buy me a new car."

So I took the checkbook and bought a brand new Honda Accord for $30,000. When I arrived home my wife said, "I didn't want an Accord. I want an SUV."

On the way back to the dealership, an accident occurred. I escaped with my life but the car was a total loss.

I still had the checkbook so I wrote a check for $40,000 and took home a nice, new Dodge Durango. I was so pleased with myself.

But my wife was not. She said, "The Durango is too small and I don't like the color red."

So I turned around and took it back to the dealer. I asked for my money back but he whipped out the magnifying glass and pointed out the small print: "absolutely, under no circumstances can you get a refund."

"Besides," said the salesmanager, "we've already spent the money and we can't take a new car in trade. It's just policy."

So I drove the Durango to the Ford dealership and on the way was rearended by a large truck. The Ford dealership gave me $10,000 in trade and I wrote a check for $40,000 more for the last of the new Ford Excursions.

I drove the Excursion home. Finally my wife was happy. "Now let's go buy the boat," she said.

"Sorry, honey," I said. "We're out of money."

So we have this giant SUV and we can't afford to put gas in it, and we have nothing to pull behind it.

But, we do have an SUV that cost $100,000 and in three years will be worth less than $20,000. And as a compensating note, I can haul a ton of groceries with it which helps save the cost of gasoline to get to the grocery store in the first place.

Now if only we could find another lottery to win.