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

Using XAG to Create a WCF Service

Here's a good example of what you could use XAG for. Using XAG, the below XML document compiles to a WCF Service Project. It contains an interface, a class implementing the interface, a few data contracts, and all appropriate references.

As you can see, you can include a literal configuration in the XML structure. This will be included as an app.config in your project.

What is XAG? XAG is a new FREE .NET 2.0/3.0 architecture and development tool allowing the creation of entire VS2005 projects in a single XML structure. You can access XAG at http://www.jampadtechnology.com/xag/

<Assembly xmlns:x="http://www.jampadtechnology.com/xag/2006/11/">
    <Reference Name="System.Runtime.Serialization " />
    <Reference Name="System.ServiceModel" />
  <Folder Header="Data Contracts">
    <Person x:Key="Person" Type="Class" AutoGenerateConstructorsByProperties="True" AccessModifier="Public" Namespace="AcmeCorp.Sales ">
        <Attribute Type="DataContract" Namespace="System.Runtime.Serialization" />
        <FirstName Type="String" />
        <LastName Type="String" />
        <Address1 Type="String" />
        <Address2 Type="String" />
        <City Type="String" />
        <State Type="String" />
        <PostalCode Type="String" />
    <CreditCard x:Key="CreditCard" Type="Class" AccessModifier="Public" Namespace="AcmeCorp.Sales">
        <Attribute Type="DataContract" Namespace="System.Runtime.Serialization" />
        <Number Type="String" />
        <Cvv2 Type="String" />
        <Name Type="String" />
        <ExpDate Type="String" />
    <Item x:Key="Item" Type="Class" AccessModifier="Public" Namespace="AcmeCorp.Sales">
        <Attribute Type="DataContract" Namespace="System.Runtime.Serialization" />
        <ItemCode Type="String" />
        <Name Type="String" />
        <Price Type="Decimal" />
    <Cart x:Key="Cart" Type="Class" AccessModifier="Public" Namespace="AcmeCorp.Sales ">
        <Attribute Type="DataContract" Namespace="System.Runtime.Serialization" />
        <Alias x:Key="LineItemsCollection" Name="LineItemsCollection" Type="System.Collections.ObjectModel.Collection [{Type Item}]" />
        <LineItems Type="{Type LineItemsCollection}" />
    <Sale x:Key="Sale" Type="Class" AccessModifier="Public" Namespace="AcmeCorp.Sales">
        <Attribute Type="DataContract" Namespace="System.Runtime.Serialization " />
        <Person AccessModifier="Public" Type="{Type Person}" InstantiateObject="True" />
        <CreditCard AccessModifier="Public" Type="{Type CreditCard}" />
        <Cart AccessModifier="Public" Type="{Type Cart}" />
  <Folder Header="Services">
    <IProcessorService x:Key="IProcessorService" Type="Interface" AccessModifier="Public" Namespace="AcmeCorp.ServiceContracts">
        <Attribute Type="ServiceContract" Namespace="System.ServiceModel" Reference="System.ServiceModel" />
        <ProcessSale ReturnType="Void">
            <Attribute Type="OperationContract" Namespace="System.ServiceModel" />
            <Parameter Name="sale" Type="{Type Sale}" />
    <ProcessorService Type="Class" AccessModifier="Public" Namespace="AcmeCorp.Service">
        <Interface Name="{Type IProcessorService}" Namespace="System" />
// Sample Console Application

using System;
using System.ServiceModel;

namespace ConsoleApplication1
    class Program
        static void Main(string[] args) {
            using (ServiceHost service = new ServiceHost(typeof(AcmeCorp.Service.ProcessorService))) {
                service.Open( );
                Console.ReadLine( );
        <service name="AcmeCorp.Service.ProcessorService" behaviorConfiguration="Service1Behavior">
              <add baseAddress="http://localhost:3827/"/>
              <add baseAddress="net.pipe://localhost/"/>
          <endpoint address="" contract="AcmeCorp.ServiceContracts.IProcessorService" binding="netNamedPipeBinding"/>
          <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
          <behavior name="Service1Behavior">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="true"/>

Introducing the XML Assembly Generator

OK, so I'm back... with a gift to the world. For the last month or so I've been working on a project in my spare time that has already been a great help in my architecture and development work. About a month ago I got COMPLETELY fed up with writing code manually and I've always despised UML and graphical code generation systems (like Visio for Enterprise Architects-- I hate Visio anyhow!), but I'm completely addicted to XML and have been for years. What I wanted (demanded!) was a declarative (NON GUI!!!!!) programming tool that allows brainstorms/architectures/projects to be written in pure XML, so that I can think in concepts without having to work directly with code. So, I decided that it was about time that I create a system that will compile a XAML-like structure to a complete VS2005/C# project.

The system has already been a tremendous help to me. In fact, when I got the initial features of the system setup, I used it to create the rest of the system. This helped speed up development because I think in terms of trees, branches organization, and XML. When I was architecting the internal components of the compiler, I was doing all my work in XML using my XAML-like format. Then, when I got a design I kinda liked, I would run it through my compiler (at the time just a console application) and VOOM I got my C# project.

So, as of today, I'm releasing to the public in alpha form under the project-name "XAG" or "XML Assembly Generator". I considered the project-name "XAC" for "XML Assembly Compiler" as it's more of a compiler than anything else... but "XAC"? That's a real name and I found that beyond lame.

Though there is a complete documentation page linked from the XAG start page, here are some of the features that XAG provides.

  • Creation of classes
  • Creation of structs
  • Creation of enumerations
  • Creation of delegates
  • Creation of interfaces
  • Creation of physical folders
  • Creation of custom attributes
  • Creation of type aliases ("using A = B;")
  • Automatic Interface Implementation
  • Usage of Polymorphism
  • Strongly-linked objects (the resource "x:Key" subsystem)
  • Inclusion of properties
  • Inclusion of fields
  • Inclusion of constructors
  • Inclusion of methods
  • Inclusion of events
  • Auto generated constructors for properties
  • Static members
  • ref and out parameters
  • Inline instantiation of properties and fields
  • Inclusion of app.config data
    • I would like to point out a few special features of the system besides what should be obvious. First, I tried to make the syntax as close to XAML as possible, including the concept of resources. So, not only can you create types, but you can strongly-link types together just like you can do in XAML with resources. For example, you can create a class called "Item" and give it a key of "MyItem01" then you can create another class and declare a property using the type referring to the key "MyItem01". XAG will do all the magic for you in finding the real name of the type. This same concept is used for many things including inheritance, interface implementation, and aliasing generics. This helps you keep your structures strongly-linked, so you can ensure that when you want to inherit from a specific class named "MyClass" you get the specific one you want, not another one with the same name, but in a different name space. This also decouples the name of the type from the key. So you can rename the class all day long and not have to update any references. As a side note, just like with WPF/XAML, in order to access a resource, you have to first declare it. I don't think that's too much to ask.

      Second, have you ever created a type with multiple properties and then had to create a constructor to set all the properties? I hate doing that. I appreciate it when it's done (who isn't frustrated at the WPF guys forgetting to do that on their UIElements!?), but still... it just seems like work that should be done for you. C# 3.0 makes this work easier... but XAG simplifies this for you today. So, when you want to create a class with properties that has a constructor to set the properties, simply create the class and the properties and set AutoGenerateConstructorsByProperties to true and BAM, your type will magically have a constructor (in addition to the parameterless one) that maps parameters to properties.

      Third, when compiling the XML to C# (or VB2005 or IL or whatever the destination is--given I add those abilities), XAG looks for compliance to many coding practices. For example, if you try to set a class field to public, the compiler will give a warning and tell you it's changing it to private. I personally hate it when people write code that doesn't obey the .NET coding laws. So, I'm not allowing some of the most common things that lead to screw ups. Also, structs have many rules that are enforced. In fact, structs have more rules enforced than all other types combined. This is not so much for compliance to the .NET coding laws, but, rather, because structs just have more rules than classes. For example, say you have a struct with three instance Int32 fields and you want a constructor. Well, first off, you can't have a parameterless constructor and secondly, if you have a constructor at all, you have to initialize all the fields before control leaves that constructor. When creating constructors, XAG knows about your fields and initializes them for you.

      Here are some sample XML documents that will compile to VS2005 projects:

      The simplest ever:

      <assembly />

      Something a bit more interesting:

      <Assembly xmlns:x=" http://www.jampadtechnology.com/xag/2006/11/ ">
        <MyClass x:Key="MyClass" Type="Class" Namespace="ClassNamespace" />

      OK, that wasn't interesting at all... So, here's one that should explain things a bit better:

      <Assembly xmlns:x=" http://www.jampadtechnology.com/xag/2006/11/">
        <MyClass x:Key="MyClass"
            <Id Type="Int32" />
            <Name Type="String" />
        <MyOtherClass Type="Class" AccessModifier="Public" Inherits="{Type MyClass}" Namespace="ClassNamespace" />

      This last one creates a project with two classes, one inheriting from the other. The first class has two properties and two constructors, one which is parameterless (as you should always have!) and one which takes two parameters and sets them to their respective properties.

      Looking at that, you may think it's too much overhead. Well, this isn't a sissy C# file creator. This thing actually creates the entire project and returns it to you as a single zip file. So, even if you wanted to start a new project, this system will help you tremendously. Simply open the XAG screen, select a template (optional), type in your code, hit Create Project, and you'll immediately get a ZIP file containing all folders, all files, an AssemblyInfo.cs file, and the project file. You will have everything you need to start your solution or to continue your solution by simply adding the XAG project to your existence solution.

      The XAG website also currently has a few tools you may find useful. Not only can you use the initial screen to create your XAG projects, but you can also use it to check for well-formed XML and also to format your XML. The entire website is Ajax based, so responsiveness is unsurpassed. As a side note, in case anyone is wondering, I did all the Ajax code manually as I find it much more efficient than using Atlas, however... I am using Atlas for web service calls to the main compilation server.

      As a warning, I'm still cleaning up the website (it's alpha!) so there may be problems in selling all compilation errors. I'm also redoing major parts of how compiler warnings are displayed, so, for now, there might not be any compiler warnings (it depends on when you look at the site!) Ideally, I would like the system to give a list of compiler errors and warnings to the end user (you!) It does in the console version, but I've yet to completely add that to the website.

      In the future I'll probably be modifying the syntax a bit (and therefore adding a release notes/change log here) and adding a few features and simplifications that I've been meaning to add for a while. For example, I wrote a much more powerful subsystem for generics, but decided that I should hold off on a public release of that to version 2.0. I will also be monitoring the exception logs and fixing bugs as they come up. I'm sure I'll be adding a ton of "if(v != null)" in this initial release.

      You can access this application at http://www.jampadtechnology.com/xag/. You can hit the intro link to see how it works, but it's fairly obvious. Most importantly, PLEASE SKIM THE DOCUMENTATION. I didn't say read it. Only super-geeks with no lives READ documentation. Just skim it and take in the overall idea and syntax. Lastly, please feel free to use any examples in the documentation, documents in the samples section (which I will be expanding upon), or provided templates to start or assist in your usage of the system.


Firefox 2.0 Officially Released!

Firefox 2.0 Officially Released!

The world's most powerful web access system has now been released in a new version: 2.0.

A few of the new features in Firefox 2.0 are:

  • In any text box you now have a SPELL CHECKER! So, if you are typing a comment in a form... you have little red underlines for misspelled words (goodness, I'm seeing them all over this e-mail. I just have to right click and fix it).
  • It's prettier for Mac and Vista people.
  • When you type in the search bar it starts to give you suggestions as to what you may be looking for.
  • Phishing protection -- Phishing is when you go to a website that says "Welcome to eBay! Give us your credit card number to continue service!". Firefox (and well most web browsers now these days) helps protect against that.
  • It handles even MORE powerful websites and intranets (Internet Explorer is finally caught up to 2003).
  • You ever want to restart your computer or just for (some odd reason and hopefully very rare reason) shut your down your computer and want to remember what you were doing? Well, now you can close Firefox and when you reopen it you have the option of continuing right when you left off (thank you Firefox 2.0 spell checker-- I could NOT spell continuing!). So, if you have 9 or so tabs open then you can have them all back when you restart your Firefox 2.0. Opera people have had this for a long time and many Firefox users had an extension that gave them this ability, but now it's finally native.

For those of you who are in a corporate environment (which is just about everyone in my target audience I suppose), to deploy Firefox in your corporate environment you won't want to install Mozilla Firefox directly, but rather, you will probably want to use FrontMotion Firefox. You can get this from FrontMotion's website. As of the time of this writing they haven't released the new 2.0 MSI (how could they... it was JUST officially released!), but in a few days they should have it posted. This Firefox is an MSI version that allows deployment via Active Directory ("AD"), which if very cool and REALLY needs to be native to Firefox anyhow. FrontMotion also provided administrative templates for your Firefox AD deployment. Their mozilla.adm template is a very powerful template allow customization of many aspects of the Firefox internal registry.

As a reminder, you can extend Firefox in any which way you want. I don't mean by using "add ons" in general, but I mean extensions. Don't like something? Fix it... Here are some extensions that I have installed everywhere.

  • All-in-One Sidebar
  • IETab
  • Download Statusbar

FrontMotion will also customize your corporate Firefox to have to extensions that you want in your MSI. What if you want to install Firefox extensions AFTER the MSI install? Well, the way to do that is simple and it does not involve a sissy GUI. Most people think that when you install a Firefox extension it's installed in the entire system. That's not true. Firefox extensions are per profile, not per operating system login nor per system. If you want to install an extension on a system use the following command.

firefox.exe --install-global-extension MyExtensionName.xpi

That will install the Firefox extension globally on the machine. This command is something you can definately package in an MSI yourself and if you know anything about MSI and AD deployment, then you know that you can set prerequisites, which will allow you to deploy the extensions after Firefox 2.0 is installed. Now, as with all properly designed software, Firefox extensions are versioned. Extension are versioned not only to have their own version, but they are versioned to work with specificially tested versions of Firefox (thats actually a Firefox extension development best practice). So, version your Firefox extension MSI packages so that you can tell AD to deploy the new versions as needed.

Given my audience, I think it would be a good idea to make a few statements about about the architecture of Firefox. This week I was at the HDC06 convention and one of the keynote speakers said something to the effect of "COM was never really implemented anywhere else other than in Microsoft operating systems" (that's a super paraphrase). Obviously that's not true... but one place it is definately not true is with Firefox. Internally Firefox has a technology called XPCOM (Cross Platform COM) that looks and feels much like Microsoft's implementation of COM (though a lot cleaner). You have interfaces and you query interfaces... blah blah blah. If you know COM, you know know what I mean.

Firefox also has it's own internal registry kinda like the Windows registry, except it's actually intuitive and it's not yet bloated to the point of complete uselessness. To access the Firefox registry, simply put "about:config" (with out the quotes) in the address bar. This allows you to customize just about anything about Firefox. As with anything, some of the really awesome stuff isn't directly seen in there (i.e. there aren't entries or "keys" for them by default), but the many Mozilla-specific websites have great documentation on how to customize and tweak your web system to your own likings. There's not really any limit to what you can do with the Firefox registry, so any documentation you see will by in some sense be incomplete, though MozillaZine's documenation is great for much of the Firefox registry defaults. You can tweak anything form the lowest geekiest component to something web developer-ish like toggling strict JavaScript mode on (... remember the Option Strict in VB? Same type of thing...) So, you may wanna dive into that depending on your needs (btw, if you see bold entries... that means they have been changed from their defaults or they have been added -- by an extension, by you, or by something else; they just aren't at their default state.) With regard to AD deployment, this Firefox registry type of stuff is what the mozilla.adm AD template will allow you to control in your AD group policy.

Lastly, for those of you have haven't seen my Firefox Web Developer videos, you can view them at the below links. Even though the videos were created using Firefox 1.0, everything in them is still true in Firefox 2.0. In the future I may released more videos, but for now what's posted is all I have prepared. I had one last video in the series , but "something" happened and it got cut in half.

So, if you haven't done so already GetFirefox.com today and help spread the goodness around the world and into your corporation

Links in this Entry

My Firefox Web Developer Video Series Link

New Blog Engine

The other day I got sick and tired of the fact that I didn't have labels on my entires... sure Blogger Beta allows for that, but you have to be on their server to use it (and understandably so). So last weekend I wrote my own blog engine in .NET 2.0. Sure I could have used a pre-built one (like DasBlog), but I kinda like the level of control I get from using things with my architecture and my coding style. Yes, I know that's a ridiculous and somewhat arrogant excuse, but I really do like to have it in my own architecture and design even if own as a training tool I can use in the future.

Anyhow... the new system has been up for a few days and if you see any weird behavior on it (like a blog entry link taking you to the homepage or whatever), please be assured that I get a notification e-mail about the problem and will quickly look into it. I'm still considering it in "beta".

With the new blog engine you can access labels to view various entires at once as well as see articles by month by going to http://www.netfxharmonics.com/year/month/ like http://www.netfxharmonics.com/2006/10/.

There are various other nice internal features too, like the ability for each entry to have more than one URL. Some of my blog entries are actually linked via different links, so that's something that is definitely important to me.

Oh, how did I do that you ask? HttpHandlers! These ASP.NET pieces of goodness are great. My entire blog is really just one public page: default.aspx (plus the internal supporting user controls, masterpage and clientside JavaScript and CSS pages). There isn't really a link called http://www.netfxharmonics.com/2005/12/video-1-fwd-setting-up-your-firefox.aspx. When ASP.NET sees that URL being requested, the HttpHandler parses it and sends the information off to the default.aspx page which then looks up the blog entry data (via an adapter which then accesses LLBLGen) and sends that data back to the client who thinks that's a real page (even Google indexes it as a real page).

Another cool thing is something I mentioned before: each page can have more than one url. So, for example, the following two URLs show the same data (via a simple lookup in a table which contains the URL names).

As far as tracking, I did throw in a simple tracking system so I can log all traffic via IP address, session, and many other things. I really only did that so I can watch myself go through the site for debugging purposes. My REAL tracking is done via the awesome Google Analytics service which I have been using since it's release. I even have it on all the e-commerce sites we host at the office (replacing WebTrends!). Google Analytics is awesome. Google is awesome!

This weekend I'm going to be builtng in a Atom/RSS creation system that will give a feed to FeedBurner for the RSS subscribers to see (if you aren't using FeedBurner to serve your feeds, you really should--it abstracts the URL from your site so you can change your REAL feed location all you want and it gives you great stats as well). I'm actually going to implement my IAP (doesn't really stand for anything) engine which allows developers to dynamically create report templates and RSS feeds declaratively. We also use this at the office... it's actually really cool. It's not only an RSS server, but also an RSS aggregator. So, at the office, we use my IAP engine in the company portal to show blocks of information (they are simply RSS feeds shown via a custom ASP.NET control like ). So... implementing the new RSS system should be really simple.

As far as a client... I think that the web is kind of a hassle for client work. Personally I've grown to hate it. Back in the day I was all about web applications. I thought they were so cool, but I'm all about smart clients now. So, I wrote my blog editing application in WPF which, naturally, communicates with the server via WCF. The interface was rather simple to build in WPF as WPF it has the the advantages of WinForms with regard to simplicity and the power and awesome control tree model of ASP.NET. Great stuff...

The internal database is SQL Server 2005 Express. I swear... that thing is so cool. I've been using it for some time now and I have to say that I SO don't see myself ever needing the standard edition. Even at the office we don't need the standard edition for most of our databases (our CRM system needs standard for it's 60GB database, but other than that... Express works great). Oh... and YES, LLBLGen Pro 2.0 is the data access layer. That thing is so awesome.

As far as hosting. I love abstraction and delegation, so I don't see myself ever doing self-hosting. That said, I'm always looking for the best deal. That doesn't mean LOWEST price. "Best deal" in my situation is a metric of many factors. So, I use CrystalTech's dedicated web hosting for hosting the site (as well as MANY others) and I use the same box for my development work as well. It may just be a Celeron, but I have 1GB of RAM it in with everything I need. I can do anything from WPF to WF to WCF to Atlas on this thing. Granted I have to work with my own DNS and IIS configuration... but, serious, if you can't do that, you shouldn't even be in the biz.

I think that covers most everything. So, if you see something weird in the system, I can almost guarantee that there was an internal exception thrown and that I was notified of it with detailed in information of the failure.

Brad Abram's Presentation

Anyone who knows me knows I'm an architect and designer at heart... every time I see construction work I just stare at it and study how everything is being planned. Heck, I learned much of software architecture from watching Discovery Channel's Extreme Engineering. I love the stuff... ergo, Brad Abrams is my hero. His work is astonishing. As far as I'm concerned he is the glue that kept the BCL together.

Everyone NEEDS to check out his latest presentation on design guidelines. This is some good stuff. Usually I think PowerPoint is a mind numbing waste of time, but this presentation is great. By the way, if you don't OWN the design guidelines book by Krzysztof Cwalina and Brad Abrams, YOU NEED TO. If you never looked at it, please stop writing code today.

Check it out...

C# 3.0 Features at MSDN Nuggets

The MSDN Nuggets website has just released a TON of awesome videos about new C# 3.0 features. Personally I love the stuff they are doing with the LINQ project. It's going to make the awesome C# 2.0 language even more powerful and flexible.

The site also released a cool video about how to get Vista's glass to work in your apps. By the way, if you're interested in the workflow foundation, there are a ton of videos on that as well.

Here are the videos...

Troelsen's COM and .NET Interoperability Book

Now this is cool... Andrew Troelsen's book "COM and .NET Interoperability" is FREE in e-book format at apress.com (click on Free E-Books under one of the headings). I highly recommend this book to anyone who wants to either continue to use their current COM components in the .NET world or for people who want to use their .NET abilities in COM-based technologies.

Many times I hear people say they would have to need for this. Well, let me give you some ideas of what I do at the office. Every now and again the web developer at the office will get overloaded with work and it will bubble up to the architect (me!) Well... the older websites are all in classic ASP and while I spend my share of time in the trenches with classic web application development, there is NO way I'm going to do any VBScript development. (heck, even back then I did most of my work in PerlScript!)

My solution is simple: .NET to COM interop. Most of my work is done in .NET and I just expose the interfaces to COM. You really think I'm going to consume a web service with manual XML structures in VBScript? Uh... think again.

By the way, for those web services I consume and expose to COM I wrote my own web service consuming framework (because so many people expose COMPLETELY INVALID services to the world in pseudo-"SOAP" that .NET won't get near it). I then expose these new "services" to the COM world via component services. Oh yes, the book has an entire chapter just on serviced components (COM+ Interop).

The Uncatchable Exception

There's once aspect of exception handling that I've found that many seasoned .NET veterans know about. Take a look at the following code...

class Node {
    private Node node;

    public Node ChildNode {
        get {
            if (node == null) {
                node = new Node( );
            return node;
        set { node = value; }

class Program
    static void ProcessNode(Node node) {

    static void Main(string[] args) {
        try {
            ProcessNode(new Node( ));
        catch (StackOverflowException ex) {
        catch (Exception ex) {

Code Segment 1

What do you figure the output will be? Well, most people say the output is something like "Operation caused a stack overflow." That's a nice guess, but this exception thrown from the CLR is actually not catchable. This is the uncatchable exception. It actually jumps directly outside of your try block and shuts your application down. What you actually see is what's in the below figure.


Figure 1

The CLR does not allow you to have infinite loops in your applications at all. So much so in fact that it will literally kill your entire application to make sure you follow the rules.

Now, if you're thinking about it... you may be think "CLR threw it? Hmm... I wonder... I read something about the ability to throw things that aren't exceptions". So, you may be tempted to try this...

try {
    ProcessNode(new Node( ));
catch {
    Console.WriteLine("Unknown exception");

Code Segment 2

This actually doesn't help the situation at all. Actually, if you've read my previous blog entry on the RuntimeWrappedException, then you would know about the .NET wrapping plan. Under .NET's default rules, thrown objects which do not inherit from System.Exception are wrapped in an instance of RuntimeWrappedException. So, even if the exception thrown was one of those, it would have been caught by the original code anyhow.

This StackOverFlowException application failure is actually a very helpful rule the CLR enforces. It forces you to go back and make sure you don't have a recursion bug in your code, because this is not really a user-specific exception it's literally a bug in your application. So, before you throw a fit when you see this, just go back and find the bug in your recursion code.

It's important to note however, that there's nothing inherently special about the StackOverFlowException itself. Take a look at this code...

static void ThrowStackOverFlowException( ) {
    throw new StackOverflowException( );

static void Main(string[] args) {
    try {
        ThrowStackOverFlowException( );
    catch (StackOverflowException ex) {

Code Segment 3

This time your output really is "Operation caused a stack overflow.". So, if you throw it yourself you can catch it. The uncatchable exception isn't really StackOverflowException, but rather the CLR's throwing of the StackOverflowException.

There are other ways to completely kill an application. One of the more subtle of ways is via use of System.Environment.FastFail(String message). This static method will immediately kill and application and will not be caught by any catch blocks anywhere. So, if you really want to screw up an application simply do the following...

public class SalesOrderException : Exception
    public SalesOrderException( ) {

    // More constructors go here...

Code Segment 4

Now every time your application has a SalesOrderException, it will explode into pieces. There's absolutely no way to recover from this. The above code is something that a disguntled employee would love to check in.

That said, looking at it in another light, Environment.FastFail(String) can actually be a great debugging tool. For example, say you have an application that is just downright giving you some weird output. You have no idea why. You know it's wrong, but there are just no exceptions bubbling to the surface to help you out. Well, if you have access to Visual Studio 2005's Debug->Exceptions... menu item, you can actually tell Visual Studio to allow you to see those first chance exceptions. If you don't have that, however you can put Environment.FastFail(String) in an exception, and use deductive reasoning and process of elimination to find out where your problem in.

You may actually be wondering "What is the point of Environment.FastFail(String)?" It's actually quite useful for anyone severely uptight about security or someone withing really low level in a system. If you're absolutely sure there's an unrecoverable corruption in your system, then you can call this method to kill this application. Basically what you're doing is "blue screening your application". The operating system is untouched, but you application is gone. Another situation someone can use Environment.FastFail in in security situations. If you absolutely need to verify some piece of security for the application to continue at all (i.e. you don't want anyone to catch an exception you throw and continue insecure), then you can use this method to "bluescreen" the app.

Base64 PNG Server

In my mind, one of the coolest things tht modern web browsers can do is deal with base64 PNG images. PNG images are the "new standard" in web images. They can be very small in size or they can be larger as true color images depending on your needs. They don't replace everything, but they do replace a lot.

A base64 PNG image is a PNG image encoded as base64. Base64 encoding is a way to encode non-printable characters (stuff you can't see, but the computer can read) into printable characters (things like letters and numbers).

Base64 PNG images (which are text) can actually be read by modern web browsers as real images. In fact, it's one of my qualification requirements for being a modern web browser (actually there are MANY requirements in my mind). You can actually use base64 PNG images directly in CSS. Here's an example...


Well, it's not SUPPOSED to be small! It's supposed to be embeddable. And it is. But think for a moment. You can take an image (not just PNG mind you, GIF can do this as well) and turn it into Base64. Now, can't you dynamically load images on the client? Well, yes you can... All you have to do is do a remote call to somewhere which will send the Base64 stream back.

One you get the stream back all you have to do is prefix the base64 stream with "data:image/png;base64," and assign the entire value to the src property (attribute) of an img object.

Here's an example I put together a few months ago of how you can do all this... Firefox users only please! IE6 won't get NEAR base64 images.

PNG Client/Service Example

Actually, this is also a great example of how to work with web remoting (I just CAN'T call it Ajax, that's too weird) and how to dynamically work with XML files.

Excerpts from my XHTML 1.1 Chapter

One of the great things about XHTML 1.1 is that you are never allowed to serve it's content as text. You can send every type of XHTML 1.0 as text all day long, but never XHTML 1.1. You say you never send as text anyhow? Sure you do... that's what the text/html content-type is all about. The default for every web server (that I know of) is to send "browser" content (i.e. HTML, XHTML) as the text/html content-type. To use a different content-type you have to specifically say so.

The typical way to send XHTML 1.1 content is actually with the application/xhtml+xml content-type. To server a page using this type in .NET, you simply state the following in the early parts of your .NET page rendering (at the Init or Page events).


Response.ContentType = "application/xhtml+xml";


Response.ContentType = "application/xhtml+xml"

When you do this, you kick modern web browsers, like Firefox, into what I like to call "parsing mode", "ultra strict mode", or "application mode" depending on my mood, but what it really is is XML parsing mode. In my lectures I often say "HTML is fundamentally unparsable". What I really mean is "HTML is fundamentally unparsed". That is, browsers tokenize HTML (via scanning), rather than parse it via XML parsing. Not to say that web browsers parse XML per se, but they could fundamentally do so it they wanted to. XHTML is XML and therefore "XHTML is parseable". Kicking modern web browsers into this ultra strict mode actually forces the browser to parse the page in an XML centric manner. What's this mean? It means that if your XML (XHTML) is not well-formed, it will throw an error. It's important to note that when you are in this ultra strict mode, the browser is not a validator (that would be REALLY cool), but is more of a well-formedness checker and is the closest things we have to a runtime compiler (which, I know, seems like an oxymoron.)

So, why do this work to get this ultra strict mode? For one simple reason: QA! Quality assurance requires that you take your work seriously. You can't just throw together a bunch of pages and throw them out on the web. If you were writing C#, C++, or Java you would be forced to run your code through a compiler to check for errors. By using ultra strict XHTML mode, you again get this forced compliance.

One word of caution...and it's a common word of caution. Gosh, no matter what I say or what I do on the web it always seems to be the same word of caution: this doesn't work in IE! This is because IE, by default, has absolutely no idea what application/xhtml+xml is. If you try to send this type of content to IE, it will probably try to download the file locally. Now there are times when it will load fine, but what usually is happening in these cases is that the content type is specified in the Windows registry to tell IE how to render it. Since we are talking about the web here, and not the Intranet world we have to follow the universal rules of the W3C, ECMA, and...well least common modern denominators (I throw the word modern in there just in case anyone wants to say that Netscape Communicator 4 is the LCD...and for the record, I don't care about any 4th generation browsers.)

So we need to make sure that IE doesn't get this ultra strict content type. Consequently, we won't have ultra strict mode in IE. Now, if you're paying attention at all you will notice that we have a page with at least two content-types required to support two different planets of browsers (IE, the rest of Earth). You can't send two at the same time. Not a problem. All we need to do is whip out a quick condition based upon what the browser can and cannot do.

When it comes to server-side browser detection some people like like to rely on the UserAgent string of the browser. This is actually a very bad idea due to how easy it is to modify a browser's UserAgent string (especially in IE with all the IE toolbars out there!). I actually read somewhere where someone said that "using the UserAgent is the fastest way to hell" I'd have to go along with that hyperbole.

Here's what you really do: test if the browser can accept the application/xhtml+xml content-type. If they can use it, use it. If not, don't. That's all there is to it, but before I get into the code there's one more thing. We're not talking just about the content-type here, were also talking about the document type (the DTD). If the condition comes back negative, that is, it does not accept xhtml+xml, then you can't use XHTML 1.1 either. So you also have to control the content-type nad the document type in the same shot. This is also not a problem.

Here's a standard method I use in the pages I want to be in ultra strict mode.

In the ASP.NET page, I replace the doctype with the following.

<asp:literal id="litDoctype" runat="server"></asp:literal>

Now in the code-behind I do the following...


Boolean debugMode = false;
private void SetDoctype( ) {
    Boolean mimeTypeOverride = false;
    if (Request.QueryString["xhtml"] != null && Request.QueryString["xhtml"] == "1") {
        mimeTypeOverride = true;

    Boolean realBrowser = false;
    if (Request.ServerVariables["HTTP_ACCEPT"] != null || mimeTypeOverride) {
        String httpAccept = Request.ServerVariables["HTTP_ACCEPT"];
        if (httpAccept != null && httpAccept.IndexOf("application/xhtml+xml") > -1 || mimeTypeOverride) {
            realBrowser = true;

    if (realBrowser && !debugMode) {
        Response.ContentType = "application/xhtml+xml";
        litDoctype.Text = "";
        litDoctype.Text = "\n";
        litDoctype.Text += "\n";
    else {
        Response.ContentType = "application/xml";
        litDoctype.Text = "\n";


Dim debugMode As Boolean = False
Sub SetDoctype()
    Dim mimeTypeOverride As Boolean = False

    If Request.QueryString("xhtml") <> Nothing And Request.QueryString("xhtml") = "1" Then
        mimeTypeOverride = True
    End If

    Dim realBrowser As Boolean = False
    If Not (Request.ServerVariables("HTTP_ACCEPT") Is Nothing) Or mimeTypeOverride Then
        Dim httpAccept As String = Request.ServerVariables("HTTP_ACCEPT")
        If Not (httpAccept Is Nothing) And httpAccept.IndexOf("application/xhtml+xml") > -1 Or mimeTypeOverride Then
            realBrowser = True
        End If
    End If

    If realBrowser And Not debugMode Then
        Response.ContentType = "application/xhtml+xml"
        litDoctype.Text = ""
        litDoctype.Text = "" & vbNewLine
        litDoctype.Text += ""
        Response.ContentType = "application/xml"
        litDoctype.Text = ""
    End If
End Sub

This code is actually longer than you might expect, but this version is a bit more robust than a simple condition. The first to notice about this code is obvious: it checks to see if the browser can accept the application/xhtml+xml content type by checking the HTTP_ACCEPT server variable. Depending on the result, the browser either gets application/xhtml+xml content type and the XHTML 1.1 doctype or text/html content type and XHTML 1.0 Transitional.

Secondly, as you can also see, I've included a debug mode which, if set to true, will tell the page to serve the "less-strict" doctype. This comes in handy when you want to make sure you get the "less-strict" doctype. I'm using a private class boolean field named debugMode as a means to put the entire page into debugMode. You could also use a query parameter to put just this one part into debug mode.

Finally, you can see something rather odd towards the beginning of the code. Even though we are doing all of this to guarantee well formed XML, this does not in any way give us any information about validation. That's where this other part comes in. If you were go point the W3C validator at this page it would actually get the XHTML 1.0 Transitional doctype with the text/html content-type. That's all well and good for validating against that doctype, but you technically have two different versions of the same page here. You need to validate against the XHTML 1.1 version as well. So, with the inclusion of a quick query parameter check I'm allowing for the ability to give the W3C validator a Url such as default.aspx?xhtml=1 which will force the page to be in XHTML 1.1 mode. This is a little easier than always having to tell the W3C validator the XHTML 1.1 override (I never much liked the warning it gives you anyways when you do it their way anyhow). As I mentioned previously, you could use a similar technique for for it into the "less-strict" mode. One idea would be to set default.aspx?xhtml=0 to kick in "less-strict" mode.