Using NAnt 0.85 with .net 4.0

Posted by Monty on December 19th, 2009

Im guessing because the .net 4.0 framework is still in the beta stages, that’s why NAnt (and TeamCity) arnt supporting it, but from what I can tell, mstcthe method to build it hasnt changed since .net 2.0′s msbuild way of compiling a solution.  Basically, all you need to do is call msbuild from the command line, give it the path of the solution, and off it goes to build it. You can specify additional information such as the build target etc, but its not essential.

Whereas normally, within NAnt, you would run the following:


<solution configuration="release" solutionfile="test.sln" />

This aparently dosent work with the .net 4.0 framework, it seems to not detect it. From what I can tell, the quickest way around this is to do the following:


<exec program="c:\Windows\Microsoft.NET\Framework64\v4.0.21006\MSBuild.exe" basedir="C:\SVN\CycleMania\"  verbose="true" >
 <arg value="C:\SVN\CycleMania\CycleMania.sln" />
 </exec>

Obviously, change the path in the program attribute to suit where Windows is located, and change the Framework64 to simply Framework if you are not running on a 64bit platform. One quirk that I fonud is that you have to have the BaseDir as where the solution is based, otherwise it throws errors about it cannot build properly.

And that should be it! If you are still struggling to compile on the .net 4.0 framework from within Nant, give me a shout.

CycleMania’s Continuous Integration

Posted by Monty on December 18th, 2009

Its now public knowledge that I have been working on getting a decent CI integration for the CycleMania project working on a publicly accessable server.

For the record, we are using the following tech for the CI integration. Yes, I WILL write a very detailed how-to on how I set things up, but as I am on my lunchbreak here at work, heres a very quick overview of what we are using:

  • TeamCity 5.0 Professional, a free download from JetBrains (Limit of 20 Projects and 20 Users)
  • .Net framework 4.0 Beta 2 (obviously)
  • Nant 0.85, not as a proper build script, but purely as a glorified as a batch script
  • APPCMD command to control IIS7′s App recycling (From inside Nant)
  • .Net 4.0′s MSBuild

All this running ontop of Windows 2008 R2 Web Edition, and SQL Server 2008 Express.

Things I have learned is:

  • TeamCity 5.0 isnt geared up for .net 4.0 YET, which is fair enough, they said support will be included in 5.1
  • TeamCity also seems to be simply geared for building projects and running unit tests, less so for copying files over to another directory with specific paths etc.
  • Nant’s contrib project is crap. Fair enough last time it was updated was 3 years ago, but it dosent work.
  • You cannot simply recycle an app pool to kill a user instance of SQL Server 2008, to update the .mdf file.
  • You really shouldnt run a site from the same directory as SVN, if you plan to update it (re above)
  • Nant is all good and well, but also dosent support .net 4.0.

I promise I will expand on most of these points when I have time!

LAB: Image Detection, Part 3

Posted by Monty on May 8th, 2009

After writing some comparison code, we have the following output, thanks to log4net (I love log4net):

output-3

Source


Target

.

X:79 Y:156 W:17 H:23 X:73 Y:149 W:17 H:23

.

X:337 Y:176 W:27 H:50 X:331 Y:169 W:27 H:49

.

X:158 Y:249 W:32 H:31 X:152 Y:242 W:32 H:32

.

X:446 Y:286 W:28 H:32 X:440 Y:279 W:27 H:32

.

X:398 Y:339 W:18 H:26 X:392 Y:334 W:16 H:24

.

X:244 Y:349 W:47 H:52 X:238 Y:342 W:47 H:52

.

X:38 Y:374 W:16 H:15 X:31 Y:366 W:17 H:16

.

X:34 Y:388 W:16 H:17 X:148 Y:423 W:27 H:24

.

X:154 Y:430 W:27 H:24 X:459 Y:435 W:24 H:22

.

X:465 Y:442 W:25 H:22 X:119 Y:450 W:30 H:46

.

X:126 Y:457 W:29 H:46 X:221 Y:465 W:17 H:19

.

X:227 Y:472 W:17 H:19 X:250 Y:594 W:17 H:18

.

X:256 Y:601 W:17 H:18



Comparison
S[X: 79, Y: 156, Width: 17, Height: 23] matched to T[X: 73, Y: 149, Width: 17, Height: 23]
S[X: 158, Y: 249, Width: 32, Height: 31] matched to T[X: 152, Y: 242, Width: 32, Height: 32]
S[X: 398, Y: 339, Width: 18, Height: 26] matched to T[X: 392, Y: 334, Width: 16, Height: 24]
S[X: 38, Y: 374, Width: 16, Height: 15] matched to T[X: 31, Y: 366, Width: 17, Height: 16]
S[X: 154, Y: 430, Width: 27, Height: 24] matched to T[X: 392, Y: 334, Width: 16, Height: 24]
S[X: 126, Y: 457, Width: 29, Height: 46] matched to T[X: 31, Y: 366, Width: 17, Height: 16]
S[X: 256, Y: 601, Width: 17, Height: 18] matched to T[X: 31, Y: 366, Width: 17, Height: 16]
Non matched
S[X: 34, Y: 388, Width: 16, Height: 17]
S[X: 154, Y: 430, Width: 27, Height: 24]
S[X: 465, Y: 442, Width: 25, Height: 22]
S[X: 126, Y: 457, Width: 29, Height: 46]
S[X: 227, Y: 472, Width: 17, Height: 19]
S[X: 256, Y: 601, Width: 17, Height: 18]
T[X: 148, Y: 423, Width: 27, Height: 24]
T[X: 459, Y: 435, Width: 24, Height: 22]
T[X: 119, Y: 450, Width: 30, Height: 46]
T[X: 221, Y: 465, Width: 17, Height: 19]
T[X: 250, Y: 594, Width: 17, Height: 18]

I know its not perfect, I know its only matching about 50% of the blobs, but im working on it. I have a plan up my sleeve for this :)

Code Snippets – Text on images

Posted by Monty on May 4th, 2009

Two code snippets for you:

private void drawTextWithBackground (string Text, Font font, Graphics grpaphics, Brush backgroundBrush, Brush foregroundBrush, int x, int y)

{

SizeF size = grpaphics.MeasureString(Text, font);

grpaphics.FillRectangle(backgroundBrush, x,y,size.Width, size.Height);

grpaphics.DrawString(Text,font,foregroundBrush,x+1,y+1);

}

private void drawTextAtBottom (String Text, Font font, Graphics graphics, Image sourceImage, Brush backgroundBrush, Brush foregroundBrush)

{

SizeF size = graphics.MeasureString(Text, font);

int y = (int) (sourceImage.Height – size.Height);

drawTextWithBackground(Text,font,graphics,backgroundBrush,foregroundBrush,1,y);

}

Does exactly what it says on the tin!

LAB: Image Detection, Part 1

Posted by Monty on May 4th, 2009

Every now and then, I get a crazy idea, to try something rather hard in .net – maybe hard isnt the right word for it, but something that hasn’t really been attempted before, or if it has, nothing public about it. So the idea I have is for some form of image detection, to say that Image X is x % similar to Image Y. I prefer to use the terms Source and Target, but it doesn’t really matter.

The source and the target images are here. That’s a photo I took a few weeks ago, if you do decide to steal it, please put a message on there that points back to me ;) Anyway, the target image is 14×15 pixels smaller – that’s a whopping 210 pixels different! It shouldn’t be too hard to match up the two images, or should it?

Histograms

My first attempt was with Histograms. I grabbed some open source (unsafe!) code that generates an array of int[] and lists the histogram values, so I shoved both images through that, and got it to output the Source’s histogram value, the Target’s histogram value, the difference between them both, and the percentage of how similar it is, like so: (The first number is the key# of the int in the array, just because)

image

Right at the bottom, I had an average of all the percentages, to see how “similar” the image is. I was expecting the percentage to be fairly high, since there is only a few hundred pixels difference, and I didn’t change any of the levels or colours when I cut the images out of each other, but it told me there was a 87.7% similarity! That was very shockingly low. If you see something wrong with my maths from the code below, please let me know:

public HistogramCompareResults(int key, int source, int target)

{

this.key = key;

this.source = source;

this.target = target;

difference = source – target;

if (difference < 0)

{

difference = difference*-1;

}

if (source != 0 && target !=0)

{

if (source > target)

{

percentage = (double)target / (double)source;

}

else

{

percentage = (double)source / (double)target;

}

percentage = percentage*100;

}

}

Image Processing

My next port of call was basically image processing, like what I did with my ANPR project that I created – basically filtering stuff out and building a “thumbprint” of the image, that hopefully will withstand being resized and stuff like that. Using image filters, flattening images and looking for large “blobs” of images, sofar I have come up with this:

image

That is the current “thumbprint” for the red channel, on the source image.

An introduction to Pex

Posted by Monty on April 16th, 2009

Pex generates Unit Tests from Parameterized Unit Tests through Automated White box Testing based on Input Generation

Ill be the first to admit, I have never been a big fan of unit testing. To me, it seems like a big waste of time. I’m not sure why, but I just don’t like it – I suppose it has something to-do with spending time writing tests instead of actually doing proper, paid work. Yes, yes, I know that tests “prove” that your code works, as long as you have coded your tests right. But what if you haven’t?

Anyway, I thought id give Pex a test since it generates unit tests for me. I love things that generate things for me, because I like to do things as quickly as possible, and the computer is millions of times faster than me at generating stuff.

For starters – head over to the Pex website, and download it – I downloaded the academic version, since im running VS.NET 2008 pro. Installing it after downloading it also might help matters.

Once you have installed it, you should be able to just open up a project and when right clicking inside a .cs file, you should see a few menu items:

WindowClipping (192)

Once you click on that little baby, Pex should start whizzing into action. Be warned, Pex will only test Public classes and methods – it dosent like testing anything else that isnt public, and I believe it will give you a warning.

In VS.NET’s status bar, you will get a bunch of messages, along the lines of “Pex: listening to monitored process (cold start)” and then “Pex: Finished”. It should also pop up a window, something vaguely like the following:

Pex Exploration Results - stopped - 1 failed, 2 runs

I know this looks confusing at first, but it really isn’t. Its quite simple really. You have various options, but what’s interesting (I think) is the grid view in the middle. The first icon says if the test passed or failed – green = pass, red = ? (Take a guess, gwan, take a guess!) The number denotes the number of the test (its incremental), and the name is the value it has tried If the test fails and throws an exception, that will be listed under the Summary and Error message bits.

If you click on the test, you can see the following to the side :

WindowClipping (194)

The details is the actual code for the unit test that it has executed. I know, it says “this.” instead of the class name, and that’s because Pex creates a copy of the code to run tests on, in a partial class. Under the stack trace subtab, it well, gives you the stack trace (Please tell me you knew what that would do. Please.)

This is where Pex becomes rather … brilliant – it allows you to save all of these generated tests into its own project, simply by selecting the tests you want to save, and hitting the fancy “Save Test…” button – it will go off, and generate its own project (It will ask you for the name of the project, where it should live etc, I will provide a screenshot later) – and it will save the tests to that project! Tests that you can run later, and you don’t have to use vs.net’s test suite – you can decide to plug in different testing libraries such as MBUnit (my current fav) by downloading extensions that are available on CodePlex, or I believe you can create your own.

More on Pex later!

Nesting Repeaters without OnItemDataBound

Posted by Monty on February 10th, 2009

windowclipping-106

windowclipping-107

And the page result is:

abc 12345
def 67890

Microsoft.WebApplication.targets

Posted by Monty on January 28th, 2009

I seem to be getting quite a few hits regarding Microsoft.WebApplication.targets file, and im guessing its because people’s copies are missing, so I have decided to attach the file to this post. Enjoy!

Linky

Extending Nettiers Part 2 – Adding an OnItemCommand

Posted by Monty on November 21st, 2008

A few days ago, I decided to try to extend the Repeater Control, by adding an OnItemCommand, like the System.Web.UI.WebControl. Simple I thought. Shouldnt be that hard I thought.

I couldnt have been more wrong.

I have learned alot with the whole experience. I have learned about event bubbling, the way that events get passed up the control tree till they are handled by their parents. Iv learned about the way that items that are nested will need to be databound for the event to be fired, even if the data is being shown on the page, you still have to explicitly call DataBind(); And I got some help from a random guy (who works for telerik!) on StackOverflow, thanks!

Basically, this is the way it works. When a user hits a button (Like a <asp:button) on the page, it will bubble up (OnBubbleEvent) on the ITemplate (In this case, the actual type of the ItemRepeater). That will do its magic, work out if its a CommandEventArgs, if it is, then bubble it up:


protected override bool OnBubbleEvent(object source, EventArgs e)
{
if (e is CommandEventArgs)
{
RepeaterCommandEventArgs args = new RepeaterCommandEventArgs(this, source, (CommandEventArgs) e);
base.RaiseBubbleEvent(this, args);
return true;
}
return false;
}

Taken from System.Web.UI.WebControls.RepeaterItem.OnBubbleEvent(object source, EventArgs e);

From there, the Repeater will intercept that, then process it:

private static readonly object EventItemCommand = new object();

protected override bool OnBubbleEvent(object sender, EventArgs e)
{
bool flag = false;
if (e is RepeaterCommandEventArgs)
{
this.OnItemCommand((RepeaterCommandEventArgs)e);
flag = true;
}
return flag;
}

protected virtual void OnItemCommand(RepeaterCommandEventArgs e)
{
RepeaterCommandEventHandler handler = (RepeaterCommandEventHandler)base.Events[EventItemCommand];
if (handler != null)
{
handler(this, e);
}
}

public event RepeaterCommandEventHandler ItemCommand
{
add
{
base.Events.AddHandler(EventItemCommand, value);
}
remove
{
base.Events.RemoveHandler(EventItemCommand, value);
}
}

This is where the fun starts. First off, in the RepeaterItem source code, you cannot call:

RepeaterCommandEventArgs args = new RepeaterCommandEventArgs(<strong>this</strong>, source, (CommandEventArgs) e);
base.RaiseBubbleEvent(this, args);

because this refers to a RepeaterItem, which, in my case, it is not because its a Nettiers generated RepeaterItem. So you have to go off and create your own CommandEventArgs. If you create your own EventArgs, the event handler RepeaterCommandEventHandler wont work, because its not of the type RepeaterCommandEventArgs. Which is a pain in the arse!

Once you have created the EventArgs and the event handler though, its fairly simple from there – you just modify the Reflector’ed code (From the RepeaterControl), to get it to handle your custom Repeater Item’s event, and not the RepeateItem default event which you cant even call!

But your not done! I found out that the command wont get called when someone clicky’s on it, because you havnt called DataBind(); on the control, which explains some … oddities I spotted in the code when I was digging around inside the Repeater – I kept finding calls to “EnsureDataBound()” – which is called OnPreRender:

Note: I will provide code samples and Nettiers templates very soon, as soon as I have integrated my changes into Nettiers!

Introduction to CSS Adapters – Label Control

Posted by Monty on November 5th, 2008

CSS Adapters in .net are rarely used, even though they are really powerful and can fix things like xhtml validation errors. This very quick sample will show you how to replace the <span> tags with a <div>

Create a class file, something like “LabelOverride.cs”, and put in the following:


using System.Web.UI;

namespace CSSAdapterTest
{
public class LabelOverride : System.Web.UI.WebControls.Adapters.WebControlAdapter
{
protected override void RenderBeginTag(HtmlTextWriter writer)
{
writer.RenderBeginTag(HtmlTextWriterTag.Div);
}
protected override void RenderEndTag(HtmlTextWriter writer)
{
writer.RenderEndTag();
}
}
}

Create a folder in the web application called “App_Browsers”, and create a .browsers file called “CSSAdapter.browser”, and enter in the following:


<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.Label"
adapterType="CSSAdapterTest.LabelOverride" />
</controlAdapters>
</browser>
</browsers>

This basically tells asp.net to override the controlType’s renderer with the one you have specified.

Once you have done this, and when you build your application, all <span>’s will disappear and be replaced with <div>’s!


Copyright © 2007-2010 Muntedhar Alhakim. All rights reserved.