tsJensen

A quest for software excellence...

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.

Automate Your Customer Service Complaints

I recently ordered something online. Let's hypothetically say it was a pair of glasses. I won't say which company because your experience will in all likelihood be better than mine. In point of fact, I have a crazy prescription that is hard to make and throws every optical lab for a loop the first time they see it.

Actually, I ordered two pairs. One pair was produced and arrived in a reasonable timeframe. The other has yet to arrive, hence my hesitance to name names. It's been weeks and weeks now. I would call and receive placating assurances that the matter would be looked into and that all would be resolved. I sent email nearly every other day inquirying. No answer. No status update. No way to look at the order status online--okay, yes that's partly my fault. Know before you press "confirm order."

So how did I solve this great quandry. Just a few lines of code which I will share below produced an emailed response within the hour. Have fun with it. Use it at your own risk. I take no responsibility for how you make yourself heard. And I've removed the identifying strings to avoid embarrassing the vendor and further endangering my order. BTW, their response was:

"We apologize for the inconvenience you experienced with us. We are remaking the glasses you ordered in our lab. You will receive them in about 10 days."

Here's the simple code:

using System;
using System.Collections.Generic;
using System.Text;

namespace
CrazyEmail
{
  class Program
  {
    static void Main(string[] args)
    {
       string n = Environment.NewLine;
       string nn = n + n;
       string body = "Hello," + nn
         + "I can think of no other way..." + nn
         + "Can you please respond..." + nn
         + "You can either..." + nn
         + "Thanks," + nn
         + "-Tyler";

       System.Net.Mail.MailMessage msg = 
         new System.Net.Mail.MailMessage("myemail@address.com", customer@service.com
         "order #xyz status inquiry"
, body);

       System.Net.Mail.SmtpClient client = 
         new System.Net.Mail.SmtpClient("mail.myserver.com"
);

       for (int i = 0; i < 100; i++)
       {
         client.Send(msg);
         Console.WriteLine(i.ToString());
       }

       Console.WriteLine("Done.");
       Console.ReadLine();
     }
  }
}

 

Hopper&lt;T&gt;: Queue&lt;T&gt; Class in C# that Caches to Disk

In a recent project, I had a Windows Service that was receiving many thousands of requests which could only be processed at specific intervals. Soon I found that queuing so many requests in a simple Queue<T> class ran away with all my machine's memory.

So what to do?

I didn't really want to change my code. I like the Queue<T> class. So I wrote a class I call Hopper<T> which acts like Queue<T> but caches items to disk. It's constructor gives the class the information it needs to do all the work. Then you just use it like a Queue<T>.

public Hopper(
 
string cachePath,
 
string uniqueFileExtension,
 
int hopperCapacity,
 
int reorderLevel)

This class takes advantage of the System.Runtime.Serialization and the System.Runtime.Serialization.Formatters.Binary namespaces.

Download the code and give it a try. And be sure to let me know what you think.

Hopper.zip (2.74 KB)

Parsing HTML with C# by the Book (DTD)

Recently I had to write an HTML parser for a project I've been working on for some time now. First I tried translating an open source C++ parser but it really wasn't what I wanted and it was also under the GPL. After contacting the author and realizing (or re-remembering) that I could not use a GPL derivative in a commercial library or application, I scrapped that and went back to the source: the official HTML DTD.

Re-remembering how to read a DTD after not having done so for so long was a chore, but the folks at Autistic Cuckoo helped. So I found a very helpful tutorial. I spent the next day or two writing the code in the file you linked below. I took some inspiration from a few files I found while browsing the FireFox code under the Mozilla license. The rest of it came from studying the DTD and trying to figure out a way to encapsulate that in a usable object model.

Here's an example of how to use it:

HtmlDocument doc = new HtmlDocument(url, html);
StringBuilder sb = new StringBuilder();
Collection<HtmlTag> pcdata = doc.GetList(DtdElement.A);
foreach (HtmlTag tag in pcdata)
{
  if (!tag.EndTag)
  {
    Dictionary<string, string> attributes = doc.GetAttributes(tag);
    sb.AppendLine("");
    sb.AppendLine("A: " + doc.ReadSlice(tag.Slice));

    foreach (KeyValuePair<string, string> pair in attributes)
    {
      sb.AppendLine(" " + pair.Key + "=" + pair.Value);
    }
  }
}

I'm releasing it under the BSD license, which I like much more than the GPL as I'm not really a "true" free software zealot. The only think I ask is that if you fix a bug or make an improvement, please share it with me and I'll put up a new version here.  

NetBrick.Net.OpenUtils1.zip (35.31 KB)