I have decided to move blogs and have a change of direction in my posts and more regular updates.
You can find me at www.beingnew.net, please come visit and see what you think.

I have decided to move blogs and have a change of direction in my posts and more regular updates.
You can find me at www.beingnew.net, please come visit and see what you think.

A common idea in software engineering is a defining principle at the root of object orientated development that seeks to encapsulate parts of an application into distinct areas of concern/responsibility – the Separation Of Concerns principle.
The primary goal is to create functional parts that, when created or modified, do not affect other areas of the system.
The new MVC framework for ASP.NET seeks to address some of the inherent problems in the webforms arena (where concerns are all piled together in pages and controls) by offering an alternative that attempts to separate a request’s concern areas into the MVC pattern to provide encapsulation for each area of responsibility.
Separating presentation from content is at the heart of MVC and the ASP.NET version looks to provide a robust and flexible way of achieving this whilst providing an interfact-driven unit-testable platform to work with.
I’m working with the Preview 2 release of the MVC framework and will be posting about MVC in the near future as we get to grips with our new production applications we are building.
We are looking forward to developing with it and a large number of respected bloggers and developers seem to be doing the same – the ASP.NET tide seems to be turning to custom development on lightweight but sound foundations and is much better for it.

Recently we had a really strange error – one of those hard to pin-point, drive you crazy kind of problems that has you running around in circles.
A test website that had a few pages and web services running in the 2.0 framework was working very well until we upgraded it with some new functionality – a HttpHandler that runs in a few other sites.
When we called a url with a specific extension (an RSS feed) with something like “/feed/rss.xml” we suddenly got the dreaded “Server Application Unavailable” error:
“The web application you are attempting to access on this web server is currently unavailable. Please hit the “Refresh” button in your web browser to retry your request.
Administrator Note: An error message detailing the cause of this specific request failure can be found in the application event log of the web server. Please review this log entry to discover what caused this error to occur. “
How can we get such a disasterous error message for just one url?
The event log was no help as the framework did not report any issues – it just bombed out.
We checked the <HttpHandler> section of the web.config that relates all “*rss/xml” extensions to a custom handler – this was correct. We checked the settings on the server and again everything seemed fine and IIS had the “.xml” extension configured to point to the aspnet_isapi.dll.
After running around in decreasing circle after circle and just when we thought we might go mad we hit our event horizon and finally found that it was this aspnet_isapi.dll association that was the issue.
This test site was running as a virtual directory under a test web site; in fact a number of sites operate in this way for quick development testing and the IIS configuration for the “.xml” was set at the higher test web site level (so all virtual directories inherit this).
Here lay our problem – the IIS extension configuration pointed to the 1.1 framework aspnet_isapi.dll and not the 2.0 framework that the site was currently running under. D’oh!
Obvious in hindsight, but sometimes don’t these bugbears just eat up your afternoon?

In a previous post I discussed the cause of an error: A process serving application pool terminated unexpectedly.
I realised that I had a need to catch unhandled 2.0 exceptions and started looking at the AppDomain class.
An Application Domain serves as a boundary so that the runtime can isolate applications from one another and using AppDomain.CurrentDomain, you can retrieve the AppDomain instance that a thread is running under.
One of the events of the AppDomain class is the UnhandledException event and it is this that I could utilise to catch any unhandled exception no matter what thread it runs under.
Therefore if I create a new HttpModule I can provide an event handler for this event and can record these exceptions when that is called.
Pooled HttpApplications
One mistake I didn’t want to make though was one that others had made – simply hooking up an event handler in the Init() method of the HttpModule as usual without taking into account multiple application instances…
The ASP.NET pipeline keeps a pool of HttpApplication objects ready to serve requests and each one of these objects has a collection of HttpModules (and thus each HttpModule provides event handlers for its own HttpApplication).
Therefore because for any one AppDomain you have a number of HttpApplication/HttpModule instances I need to ensure that I only hook up to AppDomain events once.
I can achieve this by making the HttpModule thread/multiple instance aware using static members:
public class UnhandledModule : IHttpModule
{
#region static members – note, must be thread safe
static int unhandledExceptionCount = 0;
static object lockObject = new object();
static bool initialized = false;
#endregion
public void Init(HttpApplication context)
{
// We only want one instance of a HttpModule to handle event.
if( !initialized)
{
// create a lock so no other instance can affect the static variable
lock (lockObject)
{
if( !initialized)
{
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
initialized = true;
}
} // now lock is released and the static variable is true..
}}}
So with the above code I have ensured that only one HttpModule instance will provide an event handler for the unhandled exception event.
I can be confident that this will not happen multiple times and bloat the memory of the application.
However I have concerns…
The idea for this was obtained from an MSDN article describing the 2.0 unhandled exceptions behaviour change.
What I am concerned about is that because multiple HttpApplication instances are kept in a pool and managed – if any are killed off by the runtime because they are deemed unnecessary (the requests are less frequent for instance), how do I know that the single instance that provided event handler is still alive?
We’ve tested and installed the “UnhandledModule” as described above, yet our application still errors without catching the exception. My current guess is that it is caused by no event handler existing anymore.
I’ve posed this question on the ASP.NET forums and am still figuring out an answer – do the HttpApplication instance all stay alive?
And if not, should I have code in the Dispose() method of the one instance that has the event handler, so it can release the static initialized variable?
Hopefully if I can’t find it, someone out there knows the answer.

This error message appears on a third party ASP.NET application that we have installed and each time it happens the application seems to be restarting.
What’s more, we have inserted a custom HttpModule into the application that serves as a global event handler… yet this does not catch this particular error.
What is going on?
There are actually two errors we see in the System event log:
A process serving application pool ‘xxxx’ terminated unexpectedly. The process id was ‘yyyy’. The process exit code was ‘0×0′.
A process serving application pool ‘xxxx’ suffered a fatal communication error with the World Wide Web Publishing Service. The process id was ‘yyyy’. The data field contains the error number.
Both these errors refer to unhandled exceptions in .NET 2.0 where an error is generated outside of the asp.net request such as a worker thread.
However, this never used to happen in previous frameworks…
In 1.0 and 1.1 any exception that occurred outside the context of a request would cause that thread to die… and that was it. The global error handler would not catch it, it simply died away.
In some applications this was a very bad thing – any open resources in this thread would cause memory leaks and all manner of other unintended consequences.
As such, in 2.0 the default behaviour is for the application to quit – hence these messages in the event log.
Now this is where we can now understand why our HttpModule doesn’t report the error…
Because the exception is outside the context of an asp.request and therefore not part of a HttpApplication/HttpModule process any custom error handlers running from application events do not catch the error.
So how can we catch this error and log it so we can identify the real issue behind the event log error?
I’ve been experimenting with the AppDomain events and in my next post will discuss how I’ve used it to trap these errors and identify where to start to resolve the problem.

I saw a recent interesting question about HttpHandlers on the ASP.NET forums that I wanted to share:
“What is the purpose of the IsReusable() method on the IHttpHandler interface?”
Well, to answer it we have to understand that a HttpHandler is processed as part of the request life cycle in ASP.NET and for every one of those requests an instance of the appropriate HttpHandler is required.
The IsReusable property enables you to indicate that the same instance can be pooled and used by concurrent requests.
This is useful when you have a lot of initialisation code occurring inside the handler that does not have to be repeated for every single worker thread and request.
For example:
public class MyHandler : IHttpHandler
{
public MyHandler()
{
//constructor logic involves
// a lot of processing and object instantiation
}
#region IHttpHandler Members
public void ProcessRequest(HttpContext context)
{
context.Response.Write(“biscuit”);
}
public bool IsReusable
{
get { return true; }
}
#endregion
}
This illustrates a simple example where the actual processing is writing some text to the context.
So why do we have to worry about whether it is true or false?
We need to make it Thread safe
If you are indicating that the HttpHandler can be re-used then it needs to be programmed with thread safety in mind.
Concurrent requests that can interfere with object state can cause havoc with the results of the handler’s use when it is not thread safe.
Here is an example:
public class MyHandler : IHttpHandler
{
protected string exampleString;
public MyHandler()
{
//constructor logic involves
// a lot of processing and object instantiation
}
#region IHttpHandler Members
public void ProcessRequest(HttpContext context)
{
exampleString = MyUtility.FromUrl(context.Request.Url.ToString() );
context.Response.Write(exampleString);
}
public bool IsReusable
{
get { return true; }
}
#endregion
}
Between the two lines in the ProcessRequest method there is an opportunity for context switching to interfere with the processing of the handler.
If concurrent requests are using this handler, as one request has retrieved and set the exampleString variable based on url parameters a context switch to a second request thread at the same point means that exampleString is changed to a new value.
When processing switches back to the original request thread, it writes out a potentially different value than the one intended.
Ouch.
Such a problem would probably cause “intermittent” failures and a problem that was potentially hard to pin down.
So if in doubt about thread safety, set the IsReusable method to false.
Otherwise, if you are sure it is thread safe, set to true and pool the handler for potential increased performance in high volume environments.

I had recently been reading about the Model View Presenter design pattern and although I found it interesting, it didn’t quite fit the bill for my current client.
One of the main facets of the pattern is that it ensures the UI code is testable, especially in unit tests, but if you are not implementing full test-driven development the pattern seems like it could simply be an unnecessary layer.
Or so I thought until this week.
A situation presented itself this week that made me think that adapting the view model may be very beneficial for us.
We have two custom controls on a web page – one is a news article control that delivers news, picture, by lines (etc) and the other is a social bookmark control. Both are independent of each other so the bookmark control needs to have properties set from information in the article.
This is currently achieved by the page providing a delegate to the article control – when the article data is retrieved and the control populated, the event is raised to advise of the title (and other properties).
The page code behind then sets the bookmark control’s properties and fires a custom creation method for that control.
It all works great and we haven’t had any problems so far.
What suddenly clicked with me this week though is that there was something not right about the property assignment process… the code was fine and is illustrated below, but somehow something was up!
bookmarks.BookmarkTitle = title;
bookmarks.BookmarkUri = new System.Uri(url);
bookmarks.BookmarkGeneratorID = articleID;
bookmarks.BookmarkGeneratorType = GeneratorType.Article;
bookmarks.CreateCustom();
Again, the problem we have is not with the code itself, but with the enforcing of its implementation.
All of these properties have to be set in the bookmark control for it to be valid – an application exception is raised if they are missing or incorrect and again this is fine.
However, if I or another developer goes to implement the bookmark control in another of our websites the only way we know if all the properties are being set as they should be is that a run-time exception will be generated during testing.
Hmmm. This seems very loose – how can we enforce the assignment of these properties through design so that at compile-time we can see the error of our ways?
(note: this control must be declared in the aspx page, so we cannot create a new constructor, the default one is used by .NET in the HttpHandler when it creates the controls.)
Well, we could provide just the one method on the bookmark control that assigns the properties, but this doesn’t seem right either.
This is when I thought back to the model view presenter – if we can define a viewer interface we can force the page (viewer) to implement certain contracted properties and methods and this ensures that each is actioned and implemented.
Of course this can’t stop foolish implementation such as simple”return true;” code in the methods but it does force the developer to do something.
Here is a quick example I’ve rumbled up for this post:
public interface IBookmarkViewer
{
string BookmarkTitle { get; }
System.Uri BookmarkUri { get; }
int BookmarkGeneratorID { get; }
GeneratorType BookmarkGeneratorType { get; }
}
Here we are forcing the viewer to implement these properties and here is an example of what the page might be like in action as a “viewer”:
public class articlepage: BiscuitBasePageClass, IBookmarkViewer
{
public string BookmarkTitle {
get { return _title; }
}
public System.Uri BookmarkUri {
get { return _uri; }
…
etc
The rest of the implementation can be derived from the actual model view presenter pattern – here I am simply sounding out an idea in the post and have not written or tried to implement this.
Has anyone else implemented something similar, like a “slim” version of that pattern as illustrated above?
Note: the author of the pattern, Martin Fowler, has changed the original pattern and split it into two distinct ones- you can view his description of that here.

I’ll first introduce you to a problem we recently had that required the dynamic provision of a class for a particular task and then discuss how we came to solve it using the reflection method Assembly.CreateInstance().
The background
We have many websites – well into double figures – that all utilise a common exception monitoring HttpModule that we wrote. This module is very pluggable and can also be used by third party sites that we either host or assist with developing.
The ExceptionModule, on a basic level does the following:
This landing page reports a branded apology to the end user for browsers and changes the HttpResponse header so that an appropriate Http Header is sent to the client.
Note: this navigation was done using Server.Transfer() so as not to redirect the browser in any way – it receives a branded page and Http Response for the original request (if you need more information on this, let me know and I can blog in detail about it).
The design problem
The original coding of this module was fine and the application worked as intended. However, soon after being rolled out on many sites we came across a design problem when implementing in one particular site caused by how it was set up.
The site is “split” into two distinct halves of functionality – when there is an error in one half, the exception module needs to transfer to one particular branded “landing” page; if an error occurs on the other half, the module needs to transfer to a different page.
In our original design, this wasn’t accounted for and the landing page was obtained from a name/value pair in the web.config file:
<add key=”HttpLandingPage” value=”landing.aspx” />
The challenges
Our solution was to enable the site itself to suggest where the landing page should be and be able to decide on the business logic – by providing the ability to customise this whole process, we can make this dynamic.
So how did we do this?
The web.config setting
We changed the web.config setting so that it now provides the assembly name and the class/type that will provide the landing page information (namespace and assembly below is false!):
<add key=”LandingPageProvider”
value=”Biscuit.Landing.GenericLandingPageProvider, BiscuitClasses” />
The ILandingProvider interface
We then defined a simple Interface that classes could implement to provide the landing information to the module code:
public interface ILandingPageProvider { string GetLandingPage(HttpContext context); }
We also created a generic provider for use in most situations:
public class GenericLandingPageProvider : ILandingPageProvider { public GenericLandingPageProvider() { //no constructor required } public string GetLandingPage(HttpContext context) { return "landing.aspx"; } }
Dynamic Instantiation
The climax of this solution is how we instantiate this string that is in the web.config file and implement this on the fly.
To do this we created a utility class that reads the web.config (using custom ConfigItems not explored here) and creates an instance of the object at run-time:
public class LandingPageUtility { private LandingPageUtility() { // no constructor required - private so cannot instantiate } public static ILandingPageProvider GetLandingPageProvider() { ILandingPageProvider provider = null; string configProvider = ConfigItems.Get("LandingPageProvider"); if(configProvider.IndexOf(",")>0) { // Retrieve the assembly and class string assemblyName = configProvider.Substring( _ configProvider.IndexOf(",")+1).Trim(); string className = configProvider.Substring( _ 0, configProvider.IndexOf(",")).Trim(); // create an instance of the class from the assembly System.Reflection.Assembly assembly = _ System.Reflection.Assembly.Load(assemblyName); object o = assembly.CreateInstance(className); // check if this class implements ILandingPageProvider interface if(o is ILandingPageProvider) provider = (ILandingPageProvider)o; } return provider; } }
The most important parts in the code above are the two lines in the centre of the if statement – we first load the assembly from the web.config setting giving us an instantiated Assembly object and then call the Assembly.CreateInstance() method upon it.
This reflection method uses the System.Activator class to instantiate the type specified by string provided and enables us to achieve our dynamic instantation from the web.config file.
We receive an instantiated object type and then check for the interface implementation before casting.
(Note: this procedure purposefully returns null if the config is set up incorrectly as we know it is specifically consumed by the exception monitor that will already be in the process of handling an exception, so we do not throw another.)
Using the provider
The HttpModule pulls all of this together when it wants to call the Server.Transfer() method.
To retrieve the landing page it does the following:
ILandingPageProvider provider = LandingPageUtility.GetLandingPageProvider(); context.Server.Transfer(provider.GetLandingStaticPage(context) );
In summary
We have enabled the dynamic provision of a landing page and expanded our original design past the limiting one-page provider scenario.
This may (to some) seem like overkill for what is effectively one string, but we have in fact only created one interface, a generic provider class and utility class to instantiate the web.config setting.
What we have done though is enable business logic to be provided on a site-by-site basis – by creating a new class that implements the ILandingPageProvider class we can have simple or complicated code that decides on where the exception module transfer mechanism goes to. We have also future-proofed this for third-party partners who may have more complicated requirements such as our “split” site.
Our solution in the “split” site itself was then to create a custom class that used the HttpContext object that was passed in to retrieve the Request.Url and apply certain rules to decide which page to provide as the landing page.

In this first post of this developer blog I thought I would set what I intend to write about in the future.
I came across a recent post on problogger about professional blogging that has really helped me to think about the subjects I wish to write about over the coming weeks.
In this post, it suggests an editorial calendar to give shape to your writing and to think about particular themes for your posts – if you are new to blogging (as I am) you may want to take a look at this interesting idea.
So what theme is this blog going to take?
Well, it is primarily a developer’s blog – that is, I intend to write about technical issues and problems that I have come across and discuss the solutions we adopted to solve them.
I will also cover some design and architecture and coding solutions that were of particular interest and that other developers may benefit from reading about.
Using the editorial calendar approach I already have a few that I plan to write about such as:
I also intend to write about a particular blog or website article of interest each week and how that has shaped my own thought processes and practices.
A little about biscuit
I am a contractor, working in the UK dealing primarily with ASP.NET, C# and Team Foundation server/ Team System in senior developer and lead architect roles.
As such, my posts may contain content that is limited in the nature of the solution (so as not to impose on client confidentiality) and the client name is always withheld.