Category Archives: Uncategorized

ld: library not found for -lfl on Mac using Flex and Bison

I was trying the first example of the Flex & Bison O’Reilly book and got an error. After some searching I found the solution.

The command line was:

cc lex.yy.c -flf

Error I got:

ld: Library not found for -lfl
clang: error: linker command failed with exit code 1

Apparently OS X doesn’t include libfl.a. libl.a offers the same functionality. So changing the command line to

cc lex.yy.c -ll

does the trick.

iis7

IIS Rewrite module – redirect all Http calls to Https – breaks debugging in Visual Studio

First you’ll need to install the URL rewrite module.  See link for detailed information on URL rewrite.

1
2
3
4
5
6
7
8
9
10
11
12
<rewrite>
    <rules>
	<rule name="Redirect to https" enabled="true">
	    <match url="(.*)" />
	    <conditions>
	        <add input="{SERVER_PORT}" pattern="443" negate="true" />
	    </conditions>
	    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" 
                    appendQueryString="false" />
	</rule>
    </rules>
</rewrite>

This rule will redirect all Http calls to Https, except calls to Https (otherwise you would end up in an endless loop). Some good info on Url Rewrite Module can be found here.

I you haven’t installed the Url Rewrite Module, your site will crash on the rewrite section.

Another thing we noticed is that debugging in VS2010/2008 with rewrite enabled (enabled =”true”) doesn’t work. Seems to be a bug. More info here and here.

Nested TransactionScopes in .NET

In next example, the persistence to the database will not be executed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
IBinaryAssetStructureRepository rep = new BinaryAssetStructureRepository();
var userDto = new UserDto { id = 3345 };
var dto = new BinaryAssetBranchNodeDto("name", userDto, userDto);
using (var scope1 = new TransactionScope())
{
using (var scope2 = new TransactionScope())
{
//Persist to database
rep.CreateRoot(dto, 1, false);
scope2.Complete();
}
scope1.Dispose();
}
dto = rep.GetByKey(dto.id, -1, false);

scope1.Dispose(); is not necessary in this example, but I added it for clearance.

Getting an embedded resource out of the assembly + convert to bytearray

My file is located under /images/donkey.jpg, so in that case, don’t forget to add .images.

1
2
3
4
5
6
var assemblyName = typeof(BinaryAssetFileDataProviderTest).Assembly.GetName().Name;
var str = typeof (BinaryAssetFileDataProviderTest).Assembly.GetManifestResourceStream(assemblyName + ".images." + "donkey.jpg");
var blob = new byte[str.Length];
str.Read(blob, 0, (int)str.Length);
var size = (int)str.Length;
str.Close();

Saving a connectionString at runtime to .NET app.config

1
2
3
4
5
6
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringSettings =
new ConnectionStringSettings("ConnectionString", _arguments["connectionString"], "System.Data.SqlClient");
config.ConnectionStrings.ConnectionStrings.Add(connectionStringSettings);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");

Web Application Testing Using WatiN

Yestarday I was looking for a way to test a web application having hundred of links that have a certain structure. After a question on StackOverflow I got the same answer a colleague of mine already had given, WatiN. WatiN enables you to open a certain web page in Internet Explorer or Firefox and interacting with the web page though code (like clicking, entering text in controls, …).

In my Unit Test Project (VS2008) I used Internet Explorer as this development is a little bit ahead of the Firefox development. To create cross browser WatiN tests, check this link. I’ll try to port my project to cross browser testing later.

There’s also a WatiN Test Recorder. The next Release of this looks very promising!

Here’s an example how you get started by opening an Internet Explorer Window and go to a certain web page.

1
IE ie = new IE("www.google.be");

Check if the page contains a certain text:

1
Assert.IsTrue(ie.ContainsText("..."));

To find a link and click on it:

1
ie.Link(Find.ByText("...")).Click();

Search for a certain TableCell:

1
ie.TableCell(Find.ByText("..."));

Click on a TableCell:

1
tableCell.Click();

Get all the TableCells of the current page:

1
ie.TableCells;

Search for a certain Frame:

1
Frame frame = ie.Frame(Find.BySrc(new Regex(".*main.aspx")));

Does a certain Element exists:

1
Assert.IsTrue(frame.Element("app").Exists);

Creating a Guid in C#

What is a GUID

For those of you who don’t know, a GUID (pronounced goo’id – Globally unique identifier) is a 128-bit integer that can be used to uniquely identify something. You may store users or products in your database and you want somehow uniquely identify each row in the database. A common approach is to create a autoincrementing integer, another way would be to create a GUID for your products.

How to create a GUID in C#

The GUID method can be found in the System namespace. The GUID method System.Guid.NewGuid() initializes a new instance of the GUID class.

There are also a few overloads available for those of you who want the GUID formatted in a particular fashion.

Example:

1
System.Guid.NewGuid().ToString()

There are some overloaded methods. See http://msdn.microsoft.com/en-us/library/system.guid.aspx for more information.

AS3 – C# String Replace with Regular Expressions and Callback function

In AS3:

[AS]

var regExpDigitString:String = “\\d+”;
modified = original.replace(new RegExp(regExpDigitString, “gi”), findSuffix);

private function findSuffix():String {
var result:String = arguments[0];
var index:int = arguments[1];
var input:String = arguments[2];

return result;
}
[/AS]

In C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
String regExpDigitString = "\\d+";	   	
modified = Regex.Replace(original, regExpDigitString, new MatchEvaluator(findSuffix), RegexOptions.IgnoreCase);
...
 
private String findSuffix(Match m) {
	String result = m.ToString();
	int index = m.Index;
	String input = original; //apparently you can't get the original string out of the Mach Object, so you'll need a reference to the original Object in your class.		
 
       ...		
 
	return result;
}