2005 2006 2007 2008 2009 2010 2011 2015 2016 2017 aspnet azure csharp debugging elasticsearch exceptions firefox javascriptajax linux llblgen mongodb powershell projects python security services silverlight training videos wcf wpf xag xhtmlcss

Video 1 (FWD) - "Setting up your Firefox Development Environment"

Finally! Here's the long awaited part 1 of my Firefox for ASP.NET 2.0 Developers Video Series. I will be releasing more parts to the series over the next few weeks.

This video is titled "Setting up your Firefox Development Environment" and contains valuable information on setting up your web development environment for maximizing efficiency. More setup information relating to this video will be mentioned in future videos as the utilities used in future videos of course also require setup.

Furthermore, this video is valuable not only to the professional web developer (as well as the non-professionals who still use tables for layout), but it's also valuable for anyone interested in maximizing their web experience.

Below is the link to part 1 of the Firefox for ASP.NET 2.0 Developers video series. You can download Visual Web Developer 2005 Express and Firefox 1.5 below as well.

Constrainted try with retry mechanism for .NET 2.0

Lately I've been working on building a framework of abstractions and features that I feel are rather missing from my daily life. One of these features is a try/retry. In the past I used gotos and a ton of code to do this...but such extra coding was the nature of .NET 1.x. This is the world of .NET 2.0 and therefore I can take it to the next level! My new ExceptionFramework includes a few items to help me with exceptions and I figured I would share the following bit with the world.

using System;

public static class ExceptionManager
{
    public static void TryWithRetryConstraints(
        Int32 retryCount,
        Int32 sleepTime,
        ExceptionConstraintCollection constraints,
        ExceptionTryBlock tryBlock,
        ExceptionCatchBlock catchBlock) {
        Int32 n = 0;
    retry:
        try {
            tryBlock.DynamicInvoke( );
        }
        catch (Exception ex) {
            if (++n < retryCount) {
                foreach (Object constraint in constraints) {
                    if (constraint is Type) {
                        Boolean isException = false;
                        if (((Type)constraint).Name == "Exception") {
                            isException = true;
                        }

                        Type parent = ((Type)constraint).BaseType;
                        while (!isException && parent.Name != "Object") {
                            if (parent.Name == "Exception") {
                                isException = true;
                            }
                            parent = parent.BaseType;
                        }
                        if (isException) {
                            Exception thrownException = ex;
                            while (thrownException != null) {
                                if (thrownException.GetType( ).ToString( ) == constraint.ToString( )) {
                                    Thread.Sleep(sleepTime);
                                    goto retry;
                                }

                                thrownException = thrownException.InnerException;
                            }
                        }
                    }
                    else if (constraint is String) {
                        if (ex.Message.Contains((String)constraint)) {
                            Thread.Sleep(sleepTime);
                            goto retry;
                        }
                    }
                    catchBlock.DynamicInvoke(ex);
                    return;
                }
            }
            else {
                catchBlock.DynamicInvoke(ex);
            }
        }
    }
}

Here are some of the support items...

public delegate void ExceptionTryBlock( );
public delegate void ExceptionCatchBlock(Exception ex);

public class ExceptionConstraintCollection : Collection<object>
{
    public ExceptionConstraintCollection( ) {
        // Always declare default constructors!
    }

    public ExceptionConstraintCollection(params object[] constraintSet) {
        for (int n = 0; n < constraintSet.Length; n++) {
            this.Add(constraintSet[n]);
        }
    }
}

This version allows me to retry only for certain types of failures. There is a simplified version which doesn't take the constraints, but this is one is a bit more useful for my scenarios (I actually think it's a good idea to always constrain it!) I found that the times I wanted to retry involved cases where I either wanted to retry based on a certain type of exception type or based on some text in the string.

Here's is how I use this version of the try/retry in one of my WPF applications. This is my little way of testing my laptop's RAM speed (as well as the CLR speed). I would grab 1GB of ram, free it, and quickly try to get it again (won't work). The below try/retry helps me get a feel or how fast .NET 2.0 can free up the memory and get it again (sometimes it CAN get the 1GB back).

First, I set up the constraints collection which holds the types and strings to which retries will be constrained to. This could of course be inline with the primary call.

ExceptionConstraintCollection constraints = new ExceptionConstraintCollection(
        typeof(OutOfMemoryException),
        "invocation");

Here is the actual call to the try/retry. I tell the try/retry manager how many times to retry (6), how many milliseconds to wait between each try (1000), what constraints to put on the try/retry (the above constraints collection), the logic to run as the try block (a delegate; anonymous in this case), and the code to run as the catch block (also a delegate; also anonymous in this case) and then let it do all the work for me.

ExceptionManager.TryWithRetryConstraints(6, 1000, constraints,
    delegate {
        group = new MemoryGroup();
        MessageBox.Show("Success");
    },
    delegate(Exception ex) {
        ExceptionManager.Report("Unable to allocate memory group.", ex);
    });

Now there are some obvious issues with this. The first: speed. Well, that's the nature of abstraction. You get ease of use and simplicity to life, but it costs a bit. The second is...this just seem weird, doesn't it? It just seems like there's a whole new bag of issues associated with this. Actually, this doesn't really bother me either. I'm keeping my code small enough so that I don't break the Ritcher anonymous delegate rule (never more than 3 lines of code in an anonymous block including comments) and I'm keeping in my mind that top level exceptions will really have the text of "Exception has been thrown by the target of an invocation." (which is, of course, true, but this means I better not be doing this stuff straight with COM interop!) As long as I keep that last one in mind I should be fine.

In any case, this has really been helping me out and have been proving to be a very powerful solution to the lack of a try/retry in the CLR.

C# 2.0 Self-Test

In the .NET course I'm teaching (".NET Complete") I gave a little project to the class, of which all of them are new to OO design, C#, .NET, and all the related items. The project requirement was for them to study the example and see what it does. The point of the project was to allow them to test their knowledge of C# syntax, collections, generics, and nullable types

It's almost as useless as "Hello World". Or,rather, hello world is almost as useless as this.

You simply type in string and it tokenized it into characters...but they didn't know that.  Anyhow here you go...

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace CodeExamples
{
    class Program
    {
        class Storage<T> where T : ICollection<Char>, new( )
        {
            private Dictionary<String, T> list = new Dictionary<String, T>( );

            public void ShowLines( ){
                Int32? i;
                foreach(KeyValuePair<String, T> collection in list) {
                    i = null;
                    if(collection.Key.Length > 2) {
                        i = collection.Key.Length;
                    }

                    Console.Write((i ?? -1) + " : " + collection.Key + " : ");
                    foreach(Char thing in collection.Value) {
                        Console.Write(thing);
                    }


                    Console.WriteLine();
                }
            }

            public void Save(String text) {
                T parts = new T( );
                foreach(Char c in text.ToCharArray( )) {
                    parts.Add(c);
                }

                if(!list.ContainsKey(text)) {
                    list.Add(text, parts);
                }
            }
        }

        static void Main(String[] args) {
            Storage<Collection<Char>> storage = new Storage<Collection<Char>>( );

            Boolean done = false;

            while(!done) {
                String text = Console.ReadLine( );

                if(text == "end") {
                    done = true;
                    continue;
                }

                storage.Save(text);
            }

            storage.ShowLines( );
            Console.ReadLine( );
        }
    }
}

Firefox for ASP.NET 2.0 Developers Video Series

In September I recorded a video series I entitled the Firefox Web Developer Series. I've since renamed it to the Firefox for ASP.NET 2.0 Developers Video Series (clearly because the original title wasn't long enough). I put the project on hold because of the .NET 2.0 course I was teaching, which took almost all my time, but now I'm ready to take the time to get back into videos.

This series is for ASP.NET 2.0 developers who want to enhance their ECMAScript (JavaScript) skills and see how the powerful web development suite known as Firefox as simplify their lives as much as .NET 2.0 Framework does. In the series I use Beta 2 of Visual Web Developer 2005 and Firefox 1.0, but everything should be good for the final version of VWD2005 and for Firefox 1.5.

I will be releasing videos from this series over the next few weeks. For now, here is my introduction.

You can download the first video from the video below. You can download Visual Web Developer 2005 Express and Firefox 1.5 below as well.

.NET 2.0 trycatch and trycatch(Exception ex)

One thing that I found rather interesting about .NET 2 that's different from .NET v1 is how v2 handles non-exception throwables differently.

First, a little background...

In case you don't know, the following two pieces of code are completely different.

try {
    Thrower.Start( );
}
catch (System.Exception ex) {
    System.Console.Write("Error!");
}
try {
    Thrower.Start( );
}
catch {
    System.Console.Write("Error!");
}

The difference is not that the first catches exceptions from unmanaged code and the second second doesn't, the first actually does catch those exceptions. The difference is also a bit more important than the mere fact that in the first one case we capturing the exception data in an exception type and in the other we aren't. The real difference stems from the fact that that in .NET you can throw things other than exceptions and in v1 you used the latter code to catch those non-exception throwables.

Here's an example of how to throw a non-exception. Yeah, you basically have to use the IL, or a compiler that doesn't check for this stuff.

// ThrowerLib.il
.assembly ThrowerLib { }

.class public Thrower {
    .method static public void Start( ) {
        ldstr "Oops"
        throw
        ret
    }
}

Here we are throwing "Oops". That's obviously a string, not something derived from System.Exception. In v1 you could use try{}catch(Exception ex) {} as a catch all for all exceptions, but that would not catch this error as it's not an exception. To catch this, you would also have a catch{} at the end.

OK, so how's v2 different? In this world, the non-exception item thrown is wrapped up in a real exception. So, in .NET v2 you can actually use your normal try {} catch(Exception ex){ } to also catch these non-exception throwables as well.

They are actually thrown as a RuntimeWrappedException. So the following app compiled an run in .NET v1 would explode into pieces, but would would gracefully end in .NET v2.

// ThrowerLib.il
.assembly ThrowerLib { }

.class public Thrower {
     .method static public void ThrowException( ) {
         ldstr "ThrowException exception from the IL world!"
         newobj instance void [mscorlib]System.Exception::.ctor(string)
         throw
         ret
     }

     .method static public void ThrowString( ) {
         ldstr "Weird exception!"
         throw
         ret
     }
}
// ThrowerHarness.cs
namespace ThrowerExample
{
    class ThrowerHarness
    {
        static void Main(string[] args) {
            try {
                Thrower.ThrowException( );
            }
            catch (System.Exception ex) {
                System.Console.WriteLine("System.Exception error: " + ex.Message);
            }
            catch {
                System.Console.WriteLine("Non System.Exception based error.");
            }

            try {
                Thrower.ThrowString( );
            }
            catch (System.Exception ex) {
                System.Console.WriteLine("System.Exception error: " + ex.Message);
            }
            catch {
                System.Console.WriteLine("Non System.Exception based error.");
            }
        }
    }
}

Powered by
Python / Django / Elasticsearch / Azure / Nginx / CentOS 7

Mini-icons are part of the Silk Icons set of icons at famfamfam.com