Archive for the ‘ASP.NET’ Category

h1

being new – my new blog

October 23, 2008

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.

h1

Separation Of Concerns and MVC

May 4, 2008

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.

h1

Our Server Application Unavailable?

February 20, 2008

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?

h1

AppDomain Events and HttpModules

November 9, 2007

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.

h1

A process serving application pool terminated unexpectedly

November 8, 2007

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.

h1

IsReusable on the IHttpHandler interface

November 1, 2007

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.

h1

Ensuring UI implementation with interfaces

October 16, 2007

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.

h1

Custom web.config settings

October 11, 2007

Most application settings inside a Web.config file can be created inside the <appsettings> section – a collection of name/value pair settings can serve many purposes such as mailserver settings, email settings and on/off flags for certain application functions.

Sometimes more complicated setting requirements involve the need for more than simple name and value pairs and here I will illustrate how to create custom settings in the web.config file.

I’m going to use a simplified version of a recent real example to illustrate this – a syndication feed setting that was required in a recent application we built.
The requirements were for a number of custom settings to identify:

  • a distinctive url pattern that corresponds to a particular feed
    (e.g. /news/ )
  • what class to use to process this feed at run-time
    (utilising dynamic instantiation using reflection)
  • a name to describe the feed

In order to fulfill the requirement are going to do the following:

  1. Create a custom configuration section entry in the web.config file
  2. Input the section declaration itself and the actual settings
  3. Create a section handler class to parse the settings
  4. Create a class that will represent each individual setting
  5. Encapsulate the read of the config section

1. Creating the custom section

A custom section can be created inside the <configSections> node of the web.config file and we declare the name and type which represents the class that handles the section.
In our example, the web.config custom section looks like this:

<configSections>  

<section name="SyndicationFeeds" _
    type="Biscuit.SyndicationFeedsSectionHandler, Biscuit" />  

</configSections>

Here we are creating a section called SyndicationFeeds and we are instructing that the section handler to use is the SyndicationFeedsSectionHandler class in the Biscuit assembly.

2. The section declaration

The next step is the declaration of the section itself and because this is all about customisation, you can describe this section as you see fit.
In our example the actual section declaration looks like this:

<SyndicationFeeds>  

 <Feed Name="Test" Pattern="*/test/*" _
    Class="Biscuit.Feeds.TestFeed" Assembly="Biscuit"/>  

 <Feed Name="Article" Pattern="*/news/*" _
    Class="Biscuit.Feeds.NewsFeed" Assembly="Biscuit"/>  

 <Feed Name="Comment" Pattern="*/comments/*" _
    Class="Biscuit.Feeds.CommentFeed" Assembly="Biscuit"/>  

</SyndicationFeeds>

Here we are declaring three settings and each have the following attributes:

  • Name
  • Pattern
  • Class
  • Assembly

3. The section handler class

The class used to handle the custom config section needs to implement the IConfigurationSectionHandler interface which requires a Create method that is called when the section is read:

public object Create(object parent, object configContext, _
       System.Xml.XmlNode section)

Using the Xml object passed into the method, our custom code can parse each node of section and read the attributes and create objects based on them:

public object Create(object parent, object configContext, _
     System.Xml.XmlNode section)
{
    foreach(XmlNode feed in section.ChildNodes)
    {
       //do something
    }
    return xxxxx;
}

4. The setting class

We now want to create a class that can represent the values of the setting and we can do so with properties on the class for each attribute of the setting we declared in step 2:


public class SyndicationFeedSetting
{
   private string _name;
   public string Name
   {
      get { return _name; }
   }

   private string _pattern;
   public string Pattern
   {
      get { return _pattern;}
   }

   private string _class;
   public string Class
   {
      get { return _class; }
   }

   private string _assembly;
   public string Assembly
   {
      get { return _assembly; }
   }

   // Constructor
   public SyndicationFeedSetting(string name, _
   string pattern, string className, string assembly)
   {
      _name = name;
      _pattern = pattern;
      _class = className;
      _assembly = assembly;
   }
}

We can now fill in the blanks in our section handler so that as the nodes are parsed we create a list of these setting classes:

public object Create(object parent, object configContext, _
       System.Xml.XmlNode section)
{
   ArrayList feedList = new ArrayList();  

  foreach(XmlNode feed in section.ChildNodes)
    {
       feedList.Add(new SyndicationFeedSetting( _
              feed.Attributes["Name"].Value, _
              feed.Attributes["Pattern"].Value, _
              feed.Attributes["Class"].Value, _
              feed.Attributes["Assembly"].Value) );
    }  

    return feedList;
}

(Note: this is 1.1 code that we were using. I have not demonstrated here, but in 2.0 you could return a generic List<T> with type of SyndicationFeedSetting).

5. Encapsulate the read of the config section

It is my suggestion that you also encapsulate the reading of the section from the web.config file. This is especially relevant if you are using in multiple areas in your code as typing the following in multiple places is a bit messy:

System.Configuration.ConfigurationSettings.GetConfig(“SyndicationFeeds”);

Having this declaration in multiple areas complicates the process of future improvements or changes to the code.
By wrapping the retrieval inside another class you can have one point of change.


public class SyndicationConfigurationManager
{
    private SyndicationConfigurationManager()
   {
      //no construction allowed.
    }  

    public static ArrayList GetFeeds()
   {
      return (ArrayList)System.Configuration._
       ConfigurationSettings.GetConfig("SyndicationFeeds");
    }
}

Using the code and further Improvements

To use the above, in your code you now retrieve the settings (as an ArrayList currently, but see below for improvements) with a simple call to:


ArrayList feeds = SyndicationConfigurationManager.GetFeeds();

The above samples of code can be further improved in certain areas and has been written here to be purposefully compact.
For example, there is no error checking of the parameters in the handler class and more importantly the SyndicationFeedSetting class itself (on constructor parameters).

You can also change the SyndicationFeedManager class so that it returns an array of SyndicationFeedSettings as opposed to an ArrayList making it more type-safe at compile time.
As mentioned earlier, in 2.0 you can achieve this with a generic List being returned in both cases.

h1

Dynamic instantiation using reflection

October 1, 2007

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:

  • In the Init() method subscribes to the HttpApplication.Error event
  • Once the error is caught, logs the exception details (in database, event log, xml file depending on setting)
  • Looks to transfer the request to a “landing page”- obtained from web.config setting

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

  • How can we instead have two different parameters for this transfer mechanism?
  • How can the exception monitor/module be sure of the logic of the decision in which “half” of that site it needs to transfer to? It definitely should not sit in this shared utility.
  • If we make it database driven, how do we introduce business logic based on the site location?
  • Have rules without hardcoding them in mother-of-all class with logic based on which site it is in? (e.g. a switch statement where each “case x:” is the site)

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.

h1

Welcome to the foreachbiscuit blog!

October 1, 2007

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:

  • SQL Deadlocks
  • Customised web.config settings
  • Template driven controls with data placeholders

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.