Friday, 28 March 2008

HttpHandler for all requests in ASP.Net on IIS

Setting up a HttpHandler for handling ALL incoming requests is straight forward with .Net and IIS. Provided you've created your .Net HttpHandler project to sucessfully handle the requests, you need to do the two following tasks:

1) Ensure that the web.config references the HttpHandler you've created

To the httpHandlers section of your web.config file add a reference to your httpHandler for all HTTP verbs and extensions:

<system.web>
   <httpHandlers>
      <add verb="*" path="*" type="your.assembly.reference"/>
   </httpHandlers>
</system.web>

2) Setup IIS to route all request to your handler

a) IIS 5 (Win XP)
  • Open the website properties in IIS
  • Select the Home tab
  • Click the Configuration button
  • In new window select the Mappings tab
  • Click the Add button
  • In new window's executable field Browse to the ASP.Net ISAPI dll (usually something like C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll)
  • In the Extension field type .*
  • Untick the Check that file exists box and click OK
N.B. There is a bug that if your OK button is not active you need to click the executable field text box, you should see the middle section of the text in the box change from /.../ to the full file path this should now make the OK button active.
b) IIS 6 (Windows Server 2003)
  • Open the website properties in IIS
  • Select the Home tab
  • Click the Configuration button
  • In new window select the Mappings tab
  • Insert an entry in to the Wildcard application maps with the ASP.Net ISAPI dll (usually something like C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll)
  • Untick the check that file exists box and click OK

You should now see all requests being handle via your handler. If you are having issues, a good place to start finding out why is your IIS logs - but where are my IIS logs?. Good luck!

Wednesday, 26 March 2008

XSLT Transform in C# (a piece of cake)

Whilst there are times when I'd rather be rubbing Dorset Naga chillies in my eyes, most days I'm glad I develop in C# on the .Net framework. Take today for example. I needed to look into manipulating XML via XSLT; a powerful transformation language for XML. I thought it would take a while, around 3 hours; but with the powerful .Net library I had built a win forms app that takes XML input, XSLT input and outputs the transformation - all in under 30 mins! Long live .Net!

Below is the XSLT util class I created for anyone interested.

Tuesday, 25 March 2008

I need some ReST

Despite working hard on two newly released services (BlogIt and Voice SMS) for Esendex there is no time to stop and catch my breath; I've begun working on a completely new ReST API.

With ReST (Representational State Transfer), the idea is that the services are comprised of resources (an item of interest). Calls to ReST services return a representation of a resource placing the client application in a state. Typically a resource provides access to other resources via links. The client uses these links when working with resources, placing the client application in another state, hence the name Representational State Transfer.

ReST is an architectural style, rather than a standard (although it works with recognised standards such as HTTP), there is alot more work with regards to the implementational style rather than just following a set of standards.

I'm quite liking this ReST style:

  • It's simple, lightweight and lacks unneccessary bloat
  • It's built on simple and established standards, following the architecture of the web; no special toolkits are required

I'll keep the blog updated with how it's going in the coming weeks. In the mean time, here are a couple of links you may find useful:

Tuesday, 18 March 2008

DateTime format & globalization problems in ASP.Net

Some problems cropped up recently on a project relating to datetime formats and globalisation. At first glance it seemed that it was related to en-US and en-GB datetime format conflicts. However, it was difficult to figure out why with the machine being setup in the English en-GB globalisation settings?! Turns out that an explicit globalization attribute in the apps config is required to ensure the culture setting is en-GB:

<-- Perhaps something worth adding into the frameworks web.config for all apps? -->
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB"/>

This means that dispite the machine being setup in an English en-GB globalisation, .Net was reverting to en-US because nothing was explicitly set.

After a quick Google-woogle, some techie in a forum suggests a way you can force the ASP.Net user to use the system's globalisation settings as follows:

Open up the regional and language settings and navigate to the Advanced tab. Tick the check box which says "Default User Account Settings" and then reboot your server. The culture selected in the regional and language settings should now be picked up by your ASP.Net application.

I haven't tried this yet, but as soon as I get a chance I'll let you know how it goes.

Monday, 17 March 2008

Log4net - problems logging from a web app

There have been a couple of occasions when logging just won't work for me in a web application. This typically means that log4net is getting permission issues. The web application runs as a special user account on the web server called ASPNET (usually). This account has restricted permissions to protect the web server from attacks. By default this account may not have permission to write to the file system. Make sure that the ASPNET account has permission to create and write to files in the directory chosen for logging. If the user has write permissions and logging is still absent, you need to debug log4net; this luckily is alot easier than it sounds.

Firstly, we need to output all internal debugging messages; add to your web.config the following appSettings:

<appSettings>
 <add key="log4net.Internal.Debug" value="true"/>
</appSettings>

Internal debugging messages are written to the console and to the System.Diagnostics.Trace system. An ASP.Net application does not have a console, therefore messages will be will be lost. The Trace system will by default send the message to an attached debugger (where the messages will appear in the output window). If the process does not have a debugger attached then the messages are sent to the system debugger. A utility like DebugView from http://www.sysinternals.com may be used to capture these messages.

As log4net internal debug messages are written to the System.Diagnostics.Trace system it is possible to redirect those messages to a local file (which is my preferred method). You can define a trace listener by adding the following to your application's web.config file (ensuring write permissions are enabled and dir exists):

<system.diagnostics>
<trace autoflush="true">
    <listeners>
 <add 
     name="textWriterTraceListener" 
     type="System.Diagnostics.TextWriterTraceListener" 
     initializeData="C:\tmp\log4net.txt" />
    </listeners>
</trace>
</system.diagnostics>

Saturday, 15 March 2008

SQL - Replace line breaks and other characters

I'm not quite as proficient with SQL as I'd like, so when I stumble across little SQL gems I know I'll use again I like to note them down; what better a place to do that than my blog to share with the rest of the web :)

This is a Transact SQL example, on how to replace a character on a select. The character being replaced is just the ASCII code of the character.

SELECT REPLACE($FIELD, CHAR($ASCII_CODE_OF_CHAR_TO_REPLACE), '$CHAR_TO_REPLACE_WITH') FROM $TABLE

I find this very useful when replacing line breaks with a space on a selected field:

SELECT REPLACE(MyField, CHAR(10), ' ') FROM MyTable

Other frequently used characters you may want to replace:

Tab -> char(9)
Line feed -> char(10)
Carriage return -> char(13)

Wednesday, 12 March 2008

Update your Blog by SMS wherever you are

BlogIt is a new, free service mobile blogging service available to all new and existing Esendex users. Whether you are a personal blogger or professional journalist, BlogIt is the perfect tool for updating your blog when you are on the move - just submit your posts by SMS message.

Validated posts can be online within seconds, ensuring that fast moving, topical events are shared as quickly as possible.

  • Respond to developments, news and comments immediately, your texts are posted straight away
  • Update your blog whilst on the move, or away from your PC, at anytime of day or night
  • Capture thoughts, opinions and ideas straight away 
  • Free to use - you simply pay your standard rate to text in your blog entries

BlogIt Prices

BlogIt is free to register and use, so there's no better excuse to sign up. There are no hidden charges - you simply pay your standard rate to send in your posts.

Registration and set-up takes just a few minutes then you can start mobile blogging straight away. Just text your blog entries to 07786 204 255. Each entry can be up to a maximum of 612 characters.

Sign Up Now!

If you're new to Esendex's services Sign up as a user now and you can start using BlogIt within minutes.

If you are already an existing Esendex customer simply log in from the home page and update your BlogIt details from My Details.