Mark Mintoff My superpower is common sense

16Feb/122

C# Timed Execution

A nice and comfortable way to time how long a particular code block has taken to execute.

Usage is easy. Here's a simple example:

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
23Jan/121

ASP.Net File Serving

The best way to go about creating downloadable files in ASP.Net is to control the way in which those files are served to the users. Some of the most apparently obvious advantages are:

  • Controlling the response header, allowing for a forced download
  • Hiding the directory structure of your website
  • Easy way to control file access through rights
  • File download tracking

To do this, we need to create a Generic Handler (.ashx) in our project.I added one and named it "File.ashx".

Working with generic handlers is super easy. All it takes is programming; no design. Most systems utilize a database to track files and I will be working on this assumption.

In the ProcessRequest method, we will first do some querystring handling to get the ID of the file:

Here, we are simply acquiring the file of the ID and ensuring that the application will not crash through querystring manipulation.

I did not want to get into the details of database implementation, so I wrote the following code block as a simulation of fetching from the database:

I am making the assumption here that the MIME type is already stored in the database along with other information about the file. The best time to extract the MIME type from the file would be during the upload to the server. In order to do this, I developed my own (long...224 lines) method to extract the MIME type from an HttpPostedFile object.

Moving on however, after we have the file name and mime type information, it is time to actually serve the file. We can do this by writing the file into the Response's Output Stream, but first we need to read the file into a byte array.

The byte array returned from this method will then be written into the OutputStream, along with some other information. One of the things we should do is control whether or not the file is a download; forcing the browser to download the file onto the computer as opposed to handling the file how it sees fit. For example, many browsers handle images by opening the image in the browser itself which may not be a desired outcome. Alternatively there may be some files that the browser does not know how to handle (docx for example) and will instead opt to do nothing rather than download the file. Hence:

Here, we are writing the file into the Output Stream, specifying the content type (MIME Type) and setting the content-disposition, determining whether or not the file is a download. It is necessary to wrap the file name in quotes because Firefox does not handle file names with spaces otherwise.

Finally, the way to access the file (from the downloadable example) is creating a link as follows:

And here is the obligatory, downloadable example:

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
23Jan/120

Actual Line Count

Often times, we find ourselves designing throwaway code which only fulfills a one-time requirement. The following piece of coding is one such piece of code.

What it is designed to do is generate an actual count of lines of code within an SQL file (it can be modified accordingly to work with other language files).

In order to do this, the code iterates through the lines of text and skips counting on empty lines, as well as comment blocks.

As it turns out, the 177,251 lines of code file I scanned has 154,109 lines of actual code. Handy!

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
20Dec/111

Benchmarking Infinite Loops

cycle

Today, my good friend Andrew Calleja and I set about discussing infinite loops and which technique for infinite loops would be the most efficient. Our choices boiled down to four:

  • for( ; ; )
  • while(true)
  • while(1==1)
  • GOTO loop

I created a small program to perform a benchmark test and the discrepancy between the four techniques was negligible enough to attribute to other operations in the background, so we agreed that we would have to look into the CIL using ILSpy. What was discovered was very, very interesting. First and foremost, the loops are programmed as follows:

After opening the .exe file in ILSpy, I noticed that all of the above were converted into while(true) loops!!

However, when I switched the language from C# to IL:

As can clearly be seen, the GOTO loop gets compiled into the shortest IL. Additionally the GOTO loop does not create a superfluous boolean. Quite interesting!

Please note that this IL was generated when the program was compiled in Debug mode. When generating the IL in Release mode, the loops all generate the same IL:

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
10Sep/110

C# Search Stop Words

VN:F [1.9.22_1171]
Rating: 5.0/5 (2 votes cast)
9Sep/110

Designing a Search Algorithm

Due to a recent requirement, I have had to develop a search algorithm and I wanted to create something which made sense and didn't follow the ranking and age ideology behind Google's search engine. Not that I consider Google's search engine something to scoff at mind you; it's a great search algorithm, but it doesn't suit the purpose of my implementation. My objective has been to create a search engine which searches within the site it's hosted on and I wanted it to be as relevant as possible;

  • Search must be human-like
  • Search must allow for Fuzzy Matching
  • Search must search through Pages

With this in mind, I set about trying to resolve how I would go about achieving this. Since I am searching through Pages and hence HTML, I resolved to first strip out HTML Tags and Special Characters such as   To do this, I made use of Regex and put together the following expression:

Which basically translates to:

  • Remove anything which starts with '&' and ends with ';'
  • Remove anything which starts with '<' and ends with '>'

Quite simple, but effective. Preserving class names, image URLs and what have you, is beyond scope. Now that I'm left with a clean, HTML-Free string, I set about thinking what the most natural and human-like method of searching would be. I put myself in the hypothetical shoes of a poor hypothetical man tasked with searching for a phrase in a book and determined that a human would not search for keywords but would instead search for that phrase. Hence I determined if a user searches for a phrase of a particular length, then the text must be scoured to find those words adjacent to one another. This however presents a problem; human beings do not have infallible memory, so the search might not contain the exact wording. Especially with superfluous words like "the", "and", "or"...etc. Hence the next step would be to strip out the most common words used in the English Language. This technique is known as stop words and a short google search afterwards landed me with the following satisfying list of stop words:


a, about, above, after, again, against, all,am, an, and, any, are, aren't, as,at, be,because, been, before, being, below, between,both, but, by, can't, cannot, could, couldn't,did, didn't, do, does, doesn't, doing, don't,down, during, each, few, for, from, further,had, hadn't, has, hasn't, have, haven't, having,he, he'd, he'll, he's, her, here, here's, hers,herself, him, himself, his, how, how's, i, i'd,i'll, i'm, i've, if, in, into, is, isn't, it,it's, its, itself, let's, me, more, most, mustn't,my, myself, no, nor, not, of, off, on, once,only, or, other, ought, our, ours, ourselves,out, over, own, same, shan't, she, she'd, she'll,she's, should, shouldn't, so, some, such, than,that, that's, the, their, theirs, them, themselves,then, there, there's, these, they, they'd, they'll,they're, they've, this, those, through, to, too,under, until, up, very, was, wasn't, we, we'd,we'll, we're, we've, were, weren't, what, what's,when, when's, where, where's, which, while, who,who's, whom, why, why's, with, won't, would, wouldn't,you, you'd, you'll, you're, you've, your, yours,yourself, yourselves


With that out of the way, I determined that I needed some kind of algorithm which would rank the search results in a non-static fashion. Seeing as I elected to make use of Fuzzy Logic, I knew I would have access to a scoring methodology, hence it would simply be a matter of calculating the average of all the results. All results below that average would not be considered, whereas the rest would be returned, ordered by their score. The testing results are quite accurate so far and given some more testing time, I will be providing a downloadable example.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
11Jul/112

Using Programming for Groceries

This is not something which fits in with the general theme of my blog, but it does involve programming. One of the things which I find irritating, wasteful and unnecessarily time consuming, is grocery shopping. If you want a lean grocery list with little waste, you need to spend time writing a list. If you don't have the time to write a list, you end up impulse buying and then eventually throwing out something when it starts going bad.

I decided that a little bit of programming would resolve this issue. What I resolved to do is type up an XML file with meals and the ingredients required of that meal. For example:

I then used XPath to walk over the XML file, creating a collection of objects. Then X (user-defined) meals are selected at random. The ingredients of each meal are collected together (aggregating the amounts for equivalently named ingredients) and a shopping list is generated. The costs of the ingredients are also aggregated to produce an estimate of the cost of your shopping trip.

My plans for this small application are to also add items which are recurring buys such as coffee, sugar, soap..etc. I am at this stage unsure of how to go about with this. Do I simply tack it onto every shopping list and then check my supplies? I think the best way would be to integrate this with an inventory-style system so that your list would be checked against your stock levels, effectively filtering out the items you already have from your final shopping list. I just wonder on the practicality of keeping an inventory system in a small household.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
17May/111

JQueryUI Hierarchical Sorting

Recently I have taken an interest in adapting JQueryUI's Sortable plugin in order to create a hierarchical (parent->child) sortable model. Unfortunately this proved to be difficult to do and after several adaptations using various HTML elements and JQuery configuration, I was disappointed by how unwieldy it was to move nodes about. There was a persistent jittering which I was unhappy about.

Fortunately, I happened upon Manuele J Sarfatti's nestedSortable plugin which visually presented exactly what I wanted.

My only complaint with Mr Sarfatti's plugin is it's poor serialization technique, which doesn't do a very good job of handling ID's which are in GUID format. For this reason, I created my own serialization methodology to render the hierarchy into an XML structure:

This would then serialize the hierarchy achieved in the following format:

Usage of Mr.Sarfatti's plugin is surprisingly easy:

With the following HTML:

Results in this little downloadable demo:

Let me know if there's anything you get confused about and happy coding!

VN:F [1.9.22_1171]
Rating: 5.0/5 (3 votes cast)
11May/110

ASP.Net Keep Alive and Background Processes

In certain specific cases within our web-based solutions, we would like to have one or several processes running at specific times or specific intervals in order to perform a specific set of tasks. Commonly, this would be used for tasks such as:

  • Sending e-mails on a daily basis at a specific time
  • Sending notifications on an hourly basis

My original solution for this was to have a few threads running, initiated on Application_Start which would execute code. I assume many people have tried this sort of thing and many people have found that this does not work. The reason this does not work is simple.

Web applications are essentially stateless. Working with static variables on the Application level does not mean that the web application will have a state. IIS will unload inactive web applications. Occasionally, especially on shared hosting environments, IIS will intermittently shut down a web application, frequently to clear up memory.

Some online posts on various websites, describe setting up a thread which pings the website every 5 minutes or so, thus keeping the web application alive. I have tested this on a shared hosting environment on GoDaddy, which though admittedly is not the best web hosting environment available, is no different for the purposes of this test case. The result of the automated website pinger is that the website itself stays active for a few hours and then gets randomly unloaded by IIS, hence terminating my threads. In short, this solution appears to work, but it does not work.

Hence, we can move on to an improved solution. This solution would ideally be developed for a dedicated hosting environment where you would have access to the windows machine through Remote Desktop and would be able to deploy a windows service. Alternatively you could setup the windows service to execute on your machine and interact with your website. In either case, the solution is the same.

Windows Service will call a Web Service every 5 minutes. This web service then invokes the Process Code. Within the process code, we can do whatever we like.

Ideally what we would do is keep information within a database, specifying the Next Execution Time. What we would do then, is check on invocation if the date and time are greater or equal to the current date and time. If it is, we execute the process code. On execution, we would then reset the Next Execution Time by our specified delay time.

I will create a prototype eventually, which will be available within the Downloads section.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
15Apr/116

HTML, CSS and JavaScript Testing Ground

Considering that at times HTML, CSS and Javascript development is time consuming, I developed a small testing ground to enable rapid design work. The idea is heavily based on W3C's TryIt Editor, except that I added sections for CSS and Javascript editing as well.

The testing ground supports JQuery as well.

Below is a screenshot of what it looks like:

Download below:

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)