personal comments suggest edit

We went on a triple date last night with Walt, Celine, Dan, and Judy to eat at Jiraffe. They are having some sort of Monday prefixe special. Unfortunately, a lot of French food has egg product in it to give it an added richness, which makes it difficult for my wife. I, on the other hand, had no problem with any of the food. Especially the Fois Gras. Very rich and tasty!

comments suggest edit

I just left to do some thinking where I get my best thinking done. While washing my hands, I realized I should mention a few issues with my previous recommendation.

Adding the SerializableAttribute to a class indicates to .NET that the class may be automatically serialized via reflection. When the class is being serialized, .NET uses reflection to obtain the values of every private, protected, and public member. What this means for your exception class, is that any properties it exposes should themselves be serializable. Should .NET attempt to serialize your class, and your class contains a member that cannot be serialized, it will throw a SerializationException during run-time

Now, I’m not sure if this is the best design, but I often expand any object parameters to my custom exception constructors rather than storing a reference to the object. For example:

[Serializable]
public class MyException : ApplicationException
{
  public readonly int ObjectID;
  public readonly string ObjectName;
    
  /// <summary>   
  /// Constructor stores the properties of sourceObject instead   
  /// of a reference to sourceObject itself.   
  /// </summary>   
  public MyException(string message, MyObject sourceObject)
  {
    ObjectID = sourceObject.ID;
    ObjectName = sourceObject.Name;
  }
}

This allows me not to have to worry about whether or not MyObject is serializable.

Another way to deal with this is to mark any members that are not serializable with the NonSerializedAttribute like so:

 
[Serializable]
public class MyException : ApplicationException
{
  [NonSerialized]
  private MyObject sourceObject;
    
  /// <summary>   
  /// Constructor stores the properties of sourceObject instead   
  /// of a reference to sourceObject itself.   
  /// </summary>   public MyException(string message, MyObject sourceObject)
  {
    this.sourceObject = sourceObject;
  }
}

The .NET runtime will ignore any members with the NonSerialized attribute during serialization. After deserialization, the member will have its default value (null for reference types).

Finally, you can forego automatic serialization and provide your own serialization by having your class implement ISerializable If you do so, you must still mark your class as serializable with the SerializableAttribute

 [Serializable]
public class MyException : ApplicationException
{
  [NonSerialized]
  private MyObject sourceObject;
    
  /// <summary>   
  /// Constructor stores the properties of sourceObject instead   
  /// of a reference to sourceObject itself.   
  /// </summary>   public MyException(string message, MyObject sourceObject)
  {
    this.sourceObject = sourceObject;
  }
}

What do you think?

comments suggest edit

Just had lunch with Erik and Ben at Manpuku. If you’ve never been, Manpuku is on Sawtelle just north of Olympic in the strip mall where Hurry Curry is. If none of that made sense to you, then you probably don’t live in L.A. ;)

In any case, they had the bento boxes and I had an Ishiyaki Bibimpap. Rice with vegetables and ground beef, served in a steaming hot clay bowl with a red hot paste mixed in. Too good.

In any case, sounds like Ben and Erik are doing well at my former establishment of employment. Erik and his wife are expecting in two months. Not much has changed, we spent five minutes catching up on personal lives, and 55 minutes talking about Microsoft Reporting Services and the various code we’re all writing.

comments suggest edit

When writing a custom Exception class, don’t forget to mark the class as Serializable? Why? If the exception is ever used in a remoting context, exceptions on the server are serialized and remoted back to the client proxy. The proxy then throws the exception on the client. By default, .NET types are not serializable. But by adding a simple Serializable attribute decoration on your class, .NET is able to serialize your exception.

[Serializable]
public class MyException : Exception
{
  // Custom Code
  
}

Technorati Tags: Tips

personal comments suggest edit

Let’s see. On Friday we went to see a sneak preview of Starsky and Hutch. If you liked Zoolander, I think you’ll like this. It’s not the same, but has similar type of humor. It’s hard to approach Zoolander in my book, but Starksy and Hutch does well.

Saturday played a game of soccer in the cold rain. Had a stalwart crew of 10 people out there.

And on Sunday, slept in till noon, had lunch in Eagle Rock, and basically had a nice relaxing lazy day.

comments suggest edit

This is probably the funniest thing I’ve ever heard happening on a train:

Yesterday my mom and I went on a train ride that is often billed as being a way for couples to spend a special occassion. The train was full of couples celebrating anniversaries, birthdays and other special occassions. Quite a number of couples were making out openly at the end of the train ride whose main features are a picturesque dinner on the train and a stop with a tour of a local winery.

One of the less romantic aspects of this train ride is that for the most part you have to share a table with another couple facing them. this means they get to overhear your conversation and interrupt yours. The couple we shared our table with were celebrating the guy’s birthday and his girlfriend was treating him to a special day out that ended with the train ride. After we got back on the train from the winery tour the unexpected happened. They were engaged in conversation and he was comparing her favorably to ex-girlfriends, then all of a sudden he got down on one knee and pulled out a box with a ring in it. After a stunned silence she took it, said some words softly then said “I appreciate the sentiment but the timing is inappropriate” and handed it back. This was followed by her voicing her concerns about his ability to support them and him rattling of how much he made a month plus various bonuses, etc. I think it went downhill from there.

All through this I was staring out the window trying to make small talk with my mom but failing miserably. If and whenever I do end up proposing to someone I’ve definitely learned a thing or two about what not to do.

[Via Dare Obasanjo aka Carnage4Life]

comments suggest edit

UPDATE: Whoops! I should have read the SLA for my free hosting provider more carefully. There’s a size limit to the files I may post on my ftp server. I will work to rectify this situation by finding a new FTP site. In the meantime, email me and I will send you the documents referenced below.

This is a work in progress. Pick up the Word doc here. Please make updates and changes with change tracking on and I’ll be sure to incorporate them into the master document.

Also, just for kicks, I generated MSDN style documentation of the RssBandit class library using NDoc. You can pick that up here

If the files are unavailable, let me know. Sometimes files get deleted if I go over my quota.

comments suggest edit

My latest task is to produce reports useful for fraud detection. We’ve recently had incidents in which users from Vietnam opened up several accounts via stolen credit cards, deposited money via Paypal, then commit a charge back.

I won’t go into details about the specifics of these reports, but they simply identify common patterns in fraudulent activity and alert us to the potential mischief.

csharp code datetime comments suggest edit

What is the proper way to add three hours to a DateTime for the PST timezone? Is it this?

var d = DateTime.Parse("Oct 26, 2003 12:00:00 AM");
var later = d.AddHours(3.0);
Console.WriteLine(later); // displays 10/26/2003 03:00:00 AM which is NOT correct!

A valiant attempt, but wrong. Instead try this:

var d = DateTime.Parse("Oct 26, 2003 12:00:00 AM");
var later = d.ToUniversalTime().AddHours(3.0).ToLocalTime();
Console.WriteLine(later); // displays 10/26/2003 02:00:00 AM which is correct!

Or better yet, use DateTimeOffset.

const string dateFormat = "MMM dd yyyy h:mm tt zzz";
var d = DateTimeOffset.ParseExact("Oct 26 2003 12:00 AM -07:00", dateFormat, CultureInfo.InvariantCulture);
var later = d.AddHours(3.0);
Console.WriteLine(later);

Why all the rigamarole of converting to universal time and back to local time? A little thing we like to call Daylight savings time. Without the conversion, adding three hours would have set the time to 3:00 AM which would be wrong.

Realizing this probably would have saved me from many hours of intense debugging sessions. I remember one particular case with a system of scripts and tools I set up to analyze the very large log files for a big client. It hummed along nicely until one fine spring day when it failed miserably. After an entire day of tracking down the source of the problem, I finally nailed it down to a script’s mishandling of Daylight Savings.

To find out more about proper DateTime handling, read the following article concerning best practices with manipulating date times.

comments suggest edit

agentLately it seems that everybody is playing “six-degrees of separation” by sprouting a social networking service. Some of the most prominent are Friendster.com, Tribe.com, MySpace.com, and Google’s Orkut. The promise these services make is that by joining, you’ll be where all your friends are and in the process make new friends and business contacts, though in reality they tend to look like glorified dating services. Look around the blogosphere and you’ll find several opinions concerning the problems and weaknesses of these services. For example, Dare’s take on social networking services, Warren Ellis’s evaluation of Orkut, and Don Park’s suggestion for improving existing services. Robert Scoble, a popular blogger, points out in this blog entry, that none of these networks interoperate. A person ends up entering his data into each site over and over again.

A better model for social networking is the blogging community itself. Most blogs these days contain a list of other blogs called a blogroll. Typically a blogroll is a list of other blogs that the blogger finds interesting. Click on a link within a blogroll and you’re instantly transported to another blog with its own blogroll. Continue navigating blogrolls and you may cover the entire blogging community, or just end up with a headache and a strong desire never to hear about another person’s opinion of Janet Jackson’s Super Bowl boob.

These blogrolls create a de-facto network, though I hesitate to call it a “friendship” network as the social networking services do, but it is a network nonetheless. Many call the larger network of blogs, the “blogosphere”.

It wouldn’t be too difficult to create a software component that could index your blogging network and provide features similar to Friendster: “Haacked is in your network, would you like to kick him out?”, or “You have 13,237 bloggers in your network. You spend too much time online.” Perhaps this would be a web control hosted by a blog, or a new feature for an RSS aggregator.

Many of these blogs are hosted by some sort of blogging back-end software whether it is http://www.blogger.com, http://radio.userland.com/, or my personal favorite Subtext. With some of these tools, there are more powerful ways to establish networks than simply following links in a blogroll. For example, after reading a blog entry about the definition of a track back, I realized that social networking occurs both ways. Not only are the owners of the blogs you link to (and those they link to and so on and so on) members of your network, but those that link to you (referrals) and those that comment on your site and create trackbacks to your site are also members.

Trackbacks are particularly interesting in that they are a form of machine communication between your blog and the blog of another author. “Hi, I’m Haacked’s blog. He recently wrote a post that referred to an entry in your blog and trashes you and your family. Here’s the URL. Have a nice day!” This communication provides more information than a simple link. Not only does the recipient of this trackback know that I read her blog, but she also knows that I was motivated enough to refer to her site in a post on my own blog.

In such an exchange, your blog is acting as a software agent on your behalf. Think about that for a second. In Subtext for example, the simple act of posting a blog entry with a link to another person’s blog grants your blog permission to contact her blog (on your behalf) and communicate your acknowledgment of her blog entry.

Now imagine if the next generation of these blogging tools contained new features focused on this idea of a blog as social networking tool and personal agent. Your blog could have many capabilities to act as an agent on your behalf. It’s nice to know that your blog is looking out for you.

For example, you might give your blog several pieces of personal information and create some rules about who is allowed to gain access this information in sum or in part. Taking the concept of the blogroll one step further and creating various new “rolls”, a rule might be, “Anyone that is in my “close-friend-roll” may have access to all of this information. Anyone in my “colleague-roll” may have access to my business information. And anyone who trackbacks to me may have my email address.” Now, the next time I link to your blog entry, your blog might volunteer some information about your blog to my blog, at which point my blog may refuse or accept this information (depending on my preferences).

Taken further, blogs may move past being a simple journal of your everyday thoughts, but become your representative on the web. Tired of filling out your billing and shipping information every time you shop online? Add the selected merchant sites to your “secured-merchant-roll” and the next time you purchase something from Amazon.com, just give them your URL and let them get your billing and shipping information.

This rich communication between blogs would occur through XML standards built into the next generation of blogging software. The current crop of blogging tools already supports a variety of XML standards such as the MetaWeblog API, TrackBack API, Comment API, and Blogger API. Perhaps these new communication abilities would be built on top of these existing APIs or require new APIs altogether.

The beauty of this approach is the fact that these APIs are open and not proprietary. Not to beat a TLA to death, but this architecture is an example of Microsoft’s favorite TLA these days, SOA or Service Oriented Architecture. It doesn’t matter which platform your blog is implemented on. As long as it supports these various API’s and can make an http request, it can participate in this rich communication.

personal comments suggest edit

SnowboardingYesterday Akumi, Laura, Judy, and I drove to Mountain High (not a reference to smoking out) for a day of snowboarding. This would be my second time snowboarding since the big Conversion from skiing. Right now, I would rate myself as an intermediate snowboarder and advanced intermediate skiier.

Mountain High is only a two hour drive away from Los Angeles, which makes it ideal for a day trip. The only problem is that it’s only two hours away, meaning anybody and everybody shows up. To start the day, we took a lift up the beginner hill to get warmed up, and the hill looked like the aftermath of a battle scene from Lord Of The Rings. I’m talking Pellenor fields here, with bodies strewn all over the place. The beginners hill is in reality an obstacle course that would test even Hillary Lindh (Alaskan reference).

In any case, we had a great day of boarding. Akumi and I even tried a few jumps, though she fared much better than I did. When approaching the jumps, I need to learn to stay on my front edge. Rather than a “Need For Speed”, I have an intense “Fear Of Speed” when on a snowboard approaching a ramp. I’ll start my approach, gain some speed, and as I get close, start riding on my back edge where I feel very stable on the approach, but very unstable on the landing.

I welcome any snowboarding tips in my comments section.

comments suggest edit

I just finished reading the Log4Net Introduction and FAQ and realize I’ve been missing out all these years. I won’t retrofit my current App (just yet), but I definitely need to give Log4Net a try with future projects.

In particular, I like the hierarchical logging that it enables, both with log levels and with the equivalent of logging namespaces. For exampl, you can create a log named “Security” and another log with “Security.Access”. If you set a log level on Security, the Security.Access will inherit that log level (unless you also specify a log level for it). Very cool.

comments suggest edit

Logs Scott Hanselman asks the question Should one go to Production with a Debug build? Although I find that an intriguing question, I’m more intrigued by the fact that his company exclusively uses Log4Net exclusively.

In my current project, I am using the built in Tracing framework that is part of the System.Diagnostics namespace. It supports multiple logging levels and you can add custom Trace listeners. So my question is, what does a framework like Log4Net offer that the Tracing framework doesn’t?

csharp dotnet comments suggest edit

Ok, I admit it. I’m a unit-tester-aholic. I’m compulsive about it. Sometimes going overboard:

string s = "hello world";
Assert.AreEqual(s, "hello world", "s changed. How about that?");

MbUnit is my friend. I feel a great sense of accomplishment when I can write a fully automated end to end unit test of an email Newsletter mailer. Previously, testing such an app required me to run the code, open up Outlook, check my email, and “uh huh, looks good.&8221; Now, by using a custom SMTP server class running on a separate thread (and on a custom port), my unit test can send a batch of emails and then ask the server if everything looks as its supposed to. “Aye aye. Everything checks out capt’n.”

However, there are certain cases that threaten to cure my obsessive compulsive behavior. For example, unit testing the pathological cases. Now ideally, these cases should happen infrequently enough that perhaps you can let them go. Sweep them under the carpet. Nobody has to know. Besides, who runs out of memory these days?

raising hand Well I did today. And I didn’t have a unit test for it. I could probably be forgiven for this one. I’m writing a Windows service that reads data into 10 DataSets. As each DataSet is read, I asynchronously open an SMTP connection and begin performing a mail-merge on that data set. I made sure to cap the number of concurrent connections to 10.

Now this app passed all my unit tests, but when I deployed it to the production server, it ran out of memory. After some analysis with a memory profiler, I discovered that I had violated Performance Rule #6, “90% of performance problems are designed in, not coded in.”

Each chunk of data I read in from the database is wrapped in a RecipientBundle class. The service then gives this class to an SmtpPool class which then schedules it to be sent on an available thread. The SmtpPool class keeps an ArrayList of the scheduled bundles to avoid sending duplicate bundles.

Hopefully you see the problem with this approach. I had a complete brain shutdown when I wrote the code to add the bundle to the ArrayList. Since the ArrayList is only used to avoid duplicate bundles, all that is necessary is to store the ID of the bundle. By adding the bundle, the ArrayList is holding a reference to the bundle, thus making sure it never gets garbage collected until the entire mailing is complete. Bad move. These bundles should be generation 0 objects. Created, sent, garbage collected.

So in any case, I’m thinking about whether or not I should write a unit case for dealing with OutOfMemoryExceptions. There are a lot of difficulties in doing so. Typically, for a case like this, I will try to write a unit test that fakes it. I will talk about how I do that later.

I already have a unit test for dealing with the ThreadAbortException. That’s also a story for another time as there are several difficulties with deaing with the ThreadAbortException. For example, it doesn’t seem to get raised in my methods that are running asynchronously. They just seem to die without a whimper. Also, there’s a slight issue with the CLR such that the finally block isn’t guaranteed to execute.

Till next time