xmlns + SelectNodes = empty XmlNodeList

July 22, 2008

Trying to parse web.config files using SelectNodes, I found that I have two kinds of web.config files on my development PC, one with an xmlns declaration and one without.

<configuration>
<configuration
    xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

This should not really bother me, but it turns out that the SelectNodes method does not work as expected when an xmlns declaration is present. As this comment points out, a namespace manager definition is needed, and the namespace needs to be added to the manager. That’s because, the .Net runtime cannot do this for you.

The namespace of the XML document can be retrieved from DocumentElement.NamespaceURI.

As a ridiculous consequence, every XPath query has to include the namespace in every tag!

So, my previously single-line solution to iterate all user control namespaces in the web.config gained some weight, but fortunately it works:

XmlDocument docWC = new XmlDocument();
docWC.Load("web.config");

XmlNamespaceManager mgr =
    new XmlNamespaceManager(docWC.NameTable);
XmlNodeList xnl = null;
if (string.IsNullOrEmpty(docWC.DocumentElement.NamespaceURI))
{
    xnl = docWC.SelectNodes(
        "/configuration/system.web/pages/controls/add", mgr);
}
else
{
    mgr.AddNamespace("gr", docWC.DocumentElement.NamespaceURI);
    xnl = docWC.SelectNodes(
        "/gr:configuration/gr:system.web/gr:pages/gr:controls/"
        +"gr:add", mgr);
}

Extending default validity for self-generated SSL certificates

July 16, 2008

When I installed GForge recently, I had to generate an SSL certificate as part of the installation since GForge runs on https. However, the make-ssl-cert tool does not provide a way to customize the validity of the generated certificate, which is 30 days by default.

As it turns out, this problem is known for 3 years, and it is still not fixed in Ubuntu 8.04.

The easiest workaround is to edit the make-ssl-cert script using

whereis make-ssl-cert
sudo nano [path-to/]make-ssl-cert

and replace the line

openssl req -config $TMPFILE -new -x509 -nodes -out $output
    -keyout $output

with

openssl req -config $TMPFILE -new -x509 -days 365 -nodes
    -out $output -keyout $output

That’s it.


Installing GForge on Ubuntu 8.04 Hardy

July 13, 2008

I was looking for a program that lets me easily provide download and versioning functionality for the software that I create, and I decided to give GForge a try on my Ubuntu server. Installation turned out to be a bit messy, but finally I managed to make it work.

First, I tried the gforge package that came with Ubuntu using apt-get, but it failed to install the database component gforge-db-postgresql with the message

constraint "forum_group_forum_id_fk" for relation "forum" already exists

(see bug report). From this post I figured out that several constraint triggers with the same name were created in the SQL script, which had been removed according to the changelog.

After modifying the SQL script, installation raised another error with a malformed SQL statement containing

WHERE oldvalue=100 AND field_name='category_id'

(sorry I lost the original error message). Unable (and also unwilling) to debug unknown SQL code, I decided to give up.

Next, I tried the gforge package provided by Christian Bayle. I added the repository to my apt sources, and started installation. Unfortunately, the same SQL error in the WHERE clause caused the installation to terminate unsuccessfully.

I uninstalled everything using apt remove, and turned to my last choice:

Manual installation

From the download page, I chose gforge-4.5.19, because it seems to be the most recent stable version. The installation manual can be found on the Documentation page (User Documentation / Gforge Manual (PDF)). Even though the manual was written for 4.5.3, it is still accurate enough. Chapter 2.3 covers installation.

Since the earlier installation attempts already created a PostgreSQL database, I had to drop both the database and the user

sudo su - postgres
DROP DATABASE gforge;
DROP USER gforge;

The tcpip_socket option in postgresql.conf is not recognized anymore (as of version 8.3).

Setting up the Apache configuration was a bit of a mess, because the previous attempts had generated a gforge.conf (/etc/gforge/httpd.conf) file which contained instructions that Apache2 does not recognize. They also added references to modules that do not exist, such as auth_gforge_module.

Anyway, executing a manual installation from scratch should definitely work with fewer problems than I experienced.

Finally, I got the web application to run, but I had not enabled redirection which caused to redirect an HTTP request to HTTPS. Therefore, when I clicked on the link “Register as a site user”, which requires an SSL connection, the browser display the error message:

(site URL) has sent an incorrect or unexpected message
(error code -12263)

There were strong indications that the error was related to SSL. ;)

Fine, I thought, just activate the https redirection, enable the VirtualHost section in the apache config file, create an SSL certificate, and then we’re done. So, I locate the instructions to set up an SSL server, which rely on a script called apache2-ssl-certificate which is not installed on Ubuntu Hardy. Well, actually it is missing from Ubuntu versions for one and a half years now.

Fortunately, the work-around is in the comments:

sudo make-ssl-cert /usr/share/ssl-cert/ssleay.cnf
    /etc/ssl/private/gforge.pem

In the VirtualHost section, add (or uncomment) the lines

SSLEngine On
SSLCertificateFile /etc/ssl/private/gforge.pem

In the end, I could successfully register a new user, added the Admin privilege via PostgreSQL, and now I am the proud and happy owner of a gforge installation ;)

Let’s see what adventures await me there.


Building Visual Studio Solutions from the Command Line

July 1, 2008

Sometimes there are solutions that are technologically so simple that you don’t even think about them.

I develop a couple of projects, some of which are rather complex to build, as they require to generate code from database content.

Build tools can be complex and have a steep learning curve, Powershell might be an overkill since it’s useful if you handle .Net objects and containers, and I found it amazing what you can achieve with a little batch file.

This is a sketch of a project-specific build script:

First, declare log files and required tools. I found tee.pl to both display output on screen and write it to a log file.

set log=C:\path to build\build.log
set tee=perl C:\path to script\tee.pl
set osql=osql -S host -U user -P password -d database -n -w 1000

echo. > %log%

Next, check ASP.Net code for XHTML compliance and other criteria using graspx:

pushd \inetpub\wwwroot\path to web
(call ..\check2) | %tee% %log%
popd

One of my projects uses a metamodel to generate triggers, so generate a T-SQL script to drop and create triggers, and run that script using osql (SQL 2000) or sqlcmd (SQL 2005):

echo generating database stuff from metamodel | %tee% %log%
%osql% -Q "exec Generate_SQL_Code" -o sqlcode.sql
echo run generated code | %tee% %log%
%osql% -i sqlcode.sql | %tee% %log%

Next, generate C# const declarations from database content. (I find it’s a good idea to keep generated in a separate directory):

echo generating Consts.cs | %tee% %log%
%osql% -Q "exec dev_Generate_Consts" -o "C:\path to project\Generated\Consts.cs

The C# source code now matches the constants defined in the database. So we can now initialize the VS environment and build the project:

setlocal
call "C:\path to\Microsoft Visual Studio 8\VC\vcvarsall.bat" x86
pushd c:\temp
msbuild "C:\path to solution\project.sln"
popd
endlocal

If we build a web project, delete the PrecompileWeb directory that msbuild created:

rmdir /s /q "C:\path to\Visual Studio 2005\Projects\project\PrecompiledWeb"

Finally, update the online help wiki to include new aspx pages:

call createwiki.cmd

What have we got now?

  • checked the source code
  • generated database objects
  • generated C# const definitions
  • built VS projects

Next steps are backing up the development database and publishing the application.