Monday, July 13, 2009

Daily Links 13/07/2009

Silverlight 3 RTW Released

Silverlight RIA Services July Preview
now with go-live licence

Silverlight Toolkit July Release

Pushing Data From the Server to Silverlight 3 using a Duplex Wcf Service

20 Most Interesting Silverlight Tutorials

Silverlight 3 Polling Duplex Chat and Realtime Stock Updates

Creating a ComboBox Style AutoCompleteBox Control in Silverlight

EntitySpaces, Silverlight, and WCF a Fantastic Combination

Everything search
Search files and folders on your NTFS files instantly. It's not an indexing service, so it won't hog your resource like Google Desktop / Windows Search. From the forum: "When I read about it in PCWorld, I thought it might be worth a look. When I saw that it did wildcards (and was portable!), I figured it was definitely worth a try. When I read that it did REGULAR EXPRESSIONS I was certain I wanted it. And when it indexed all my huge volumes in a couple seconds, I nearly had a stroke"

Thursday, June 25, 2009

Event Extensions: Methods for firing events and managing event listeners

In most modern C# applications, generous usage of events have become the norm. Gone are the days where we were afraid to use them because of the "black magic" code that they seemed to execute.

Today, perhaps the opposite is true, and they are used too lightly, with disregard for the memory leeches they are (those small, black  slimly kind that you're never aware of what damage they're doing until it's too late).

Here's some code to make usage of the leeches a little easier :).

The typical firing of an event from C# code looks something like this:

   1: if (MyEvent1 != null)
   2: {
   3:     MyEvent1(this, EventArgs.Empty);
   4: }

There are 2 essential problems with this code:

  1. It's too many lines of code for something so mundane.
  2. What happens if there are more than 1 listeners listening to the event, and one of them throws an exception? Normal .NET behaviour dictates that the other listeners never get to hear about the event, but what if it's important that all listeners know about the event before application execution is halted?

Solution to problem #1: wrap the logic in an extension method

   1: MyEvent1.Raise(this, EventArgs.Empty);

there - isn't that better? Here's sample of the implementation:

   1: public static void Raise(this EventHandler handler, object sender, EventArgs e)
   2: {
   3:     if (handler != null)
   4:     {
   5:         handler(sender, e);
   6:     }
   7: }

Solution to problem #2: expand on the extension method (now that all the logic is conveniently in one place)

   1: MyEvent1.Raise(this, EventArgs.Empty, true);

...where the boolean parameter tells the extension method to make sure that all the event listeners get to hear about the event before throwing an exception. The exception that is then thrown, is an EventListenerException, which is just basically a wrapper for all exceptions that occurred on the event listener's event handling code.

sample implementation: (called from the event extension method)

   1: private static void ManageEventListeners(IEnumerable<Delegate> delegates, object sender, EventArgs e)
   2:       {
   3:           EventListenerException eventListenerException = null;
   4:           foreach (Delegate listner in delegates)
   5:           {
   6:               //the listner could throw an unhandled exception in the handler method
   7:               //make sure that the other listners still handle event if this is the case
   8:               try
   9:               {
  10:                   listner.DynamicInvoke(new[] {sender, e});
  11:               }
  12:               catch (TargetInvocationException ex)
  13:               {
  14:                   //add a new event listner
  15:                   EventListener eventListener = new EventListener
  16:                                                   {
  17:                                                       Listener = listner,
  18:                                                       ListenerException = ex.InnerException
  19:                                                   };
  20:                   
  21:                   //NOTE: The exception which occurs is "TargetInvokationException", but the 
  22:                   //actual exception which occurs in the listner is contained in the innerException
  23:  
  24:                   //add listner to eventListnerException
  25:                   if (eventListenerException == null)
  26:                   {
  27:                       eventListenerException = new EventListenerException();
  28:                   }
  29:                   eventListenerException.EventListenerCol.Add(eventListener);
  30:               }
  31:           }
  32:  
  33:           //if an exception was thrown by one of the listner's event handlers
  34:           if (eventListenerException != null)
  35:           {
  36:               throw eventListenerException;
  37:               //then re-throw the exception that thrown in the handler 
  38:           }
  39:       }

Here's some sample code with unit tests included in case you need to play with the code (NUnit 2.4.7 & VS 2008)



PS: It's Silverlight compatible (of course :) )

Daily Links 25/06/2009

Silverlight: Product Maintenance Application ( Part 1 - Authentication, Roles and Logging In )

Silverlight - Resizing Childwindow

Synchronous Web Service Calls with Silverlight 2: Dispelling the async-only myth

Distribute Mobile Applications On Windows Marketplace

Visual Studio's Database Publishing Wizard and the new 1-Click Web Deployment

Visual Studio 2010: Web application packaging and publishing

Microsoft ADO.NET Entity Framework Feature Community Technology Preview 1

Feature CTP Walkthrough: Self Tracking Entities for Entity Framework

Feature CTP Walkthrough: Code Only for Entity Framework

Sunday, June 14, 2009

Daily Links 14/06/2009

Improving the performance of web services in SL3 Beta
Uses new SL3 Binary encoding

Implementing UserName Password & WS-Security with Silverlight

55 Best Ways To Track Your Website Daily Traffic

Sending Email From a Gmail Account with C# .NET and Web Services

Convert IP Address To Location and Address to Geo Location Using C# VB.Net

UppercuT build framework (for SVN SC) - tutorials

UppercuT TeamCity Integration Series

Best Practices for Handling Change in Your WCF Applications

Free Vector Graphics Program
Can save output directly to xaml format

Vectorize Bitmaps to XAML using Potrace and Inkscape

10+ Stunning Websites for Download Free Icon Sets

Royalty Free Icons

Wednesday, June 3, 2009

Daily Links - 03/06/2009

Silverlight 3 to launch July 10

Silverlight Stopwatch class in C#

UppercuT - The Insanely Easy to Use Automated Build Framework

Implementing the [ThreadSafe] Singleton Pattern in C#

T4 Editor plus UML-Style modeling tools for Visual Studio 2008/2010

Indian MVP Inspired by Silverlight 3!
some handy tutorial links

using SharpZipLib compression in conjunction with isoloated storage

.NET RIA Services: Get your metadata from anywhere!

WCF Binary Encoding with SL3

A simpler, conventions based DependencyProperty structure for silverlight using CustomControlBase

Monday, May 25, 2009

Daily Links 25/05/2009

Visual Studio 2010 and .NET Framework 4 Beta 1 - Downloads

Silverlight and Visual Studio 2010 Beta 1
Some Silverlight 3 bits don't play nice with VS 2010, so you have to pick one :|

Silverlight 3 with Ria Authentication service

CLOG: Client logging solution for silverlight

silverlight super textbox

Silverlight Toolkit Chart tweaking made easy

Silverlight Splash Screen links:
Silverlight Loader:
splash screens in managed code - getting started
How to: Define a Simple Silverlight Splash Screen
an example

Silverlight loading animation

Visual Studio 2010 Beta 1 Cheat Sheet

POCO in the Entity Framework: Part 1 - The Experience

Model First in Entity Framework 4

Tuesday, May 19, 2009

Daily Links 19/05/2009

.NET RIA Services May 2009 Preview

Building Modular Silverlight Applications

Using Silverlight 2 With ADO.NET Data Services

Some very encouraging links from the Entity Framework team...
Sneak Preview: Persistence Ignorance and POCO in Entity Framework 4.0
Sneak Preview: Model First in the Entity Framework 4.0
Sneak Preview: Deferred Loading in Entity Framework 4.0
Sneak Preview: N-Tier development with Entity Framework 4.0
Sneak Preview: Entity Framework 4.0 Testability Improvements

How To: Use Membership in ASP.NET 2.0

Create Membership tables in another database than the standard aspnetdb.mdf

How to Configure Your ASP.NET 2.0 Account's Membership Database