Thursday, 5 February 2009

Watchout, there's a Host about

I was pairing with Remco (start a blog man!) today and we came across an interesting assumption. The code we were looking at created a redirect URL using the following code.


return Redirect("http://" + Request.Url.Host + "/" + url.Trim('/'));

'Host' returns the "host component of this instance". No bad thing, until you start to test on a server using a port other than port 80. Your code may well break because the URL you are constructing doesn't contain the port number that your Web application is running on. To fix this you can use Request.Url.Authority. 'Authority' return the "DNS host name or IP address and the port number for a server".


Tuesday, 3 February 2009

ActiveRecord Configuration

The quick-start documentation that comes with ActiveRecord provides the following example configuration.




<?xml version="1.0" encoding="utf-8" ?>
<activerecord>
<config>
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" />
</config>
</activerecord>

I couldn't get this to work. After a bit of fiddling I ended up with the following.



<?xml version="1.0" encoding="utf-8" ?>
<activerecord>
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Server=.;Initial Catalog=TestDatabase;User Id=UserNameGoesHere;Password=PasswordGoesHere " />
</config>
</activerecord>

The main differences are the 'hibernate' prefixes in the keys and the replacement of Data Source with Server in the connection string. This configuration worked against a SQL Server 2005 installation.



Monday, 2 February 2009

The Microsoft Loopback Adapter

I had a situation once where the IP address of an SMTP server had been hard-coded and I could only run the application successfully if the server was available. This meant that developing off-site was a problem. Then, from somewhere in the back of my mind, I remembered about the Microsoft Loopback Adapter (MSLA). This is a software network adapter i.e. you don't have to physically install a NIC. Installing this network adapter (add new hardware and choose the Microsoft Loopback Adapter from the list) allows me to easily route a given IP address to my local machine. For instance, if I manually assign the IP address of the MSLA to 192.168.0.100, then any requests bound for this IP address from my machine will end up at my machine. It's like saying "route any requests to 192.168.0.100 to 127.0.0.1". I installed a local SMTP server and my application sent emails to this local server via the MSLA. All I had to do was to remember to disable the network adapter when I was back on-site and wanted to use the real SMTP server.