Archive for July, 2009

Getting the ASP.NET Development Web Server to use a root path

Friday, July 10th, 2009

ASP.NET 2.0 comes with a test webserver which can be run by simply pressing F5 in Visual Studio from a website project which is located on your PC.  The only problem is that for reasons best known to Microsoft, it launches with the site configured in a subfolder.  This isn’t always convenient, as you may have paths relative to the site root which prevent this being practical for testing.

In order to get around this problem, you need to take the following steps: –

First of all, configure Visual Studio so that you can launch the test server manually as follows: –

Under the Tools menu, select External Tools.
Add a new entry
Call it something like ASP.NET Development Server
Command is C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.EXE (you may need to alter the path for your local machine)
Arguments are /port:80 /path:$(ProjectDir) (note that you will need to leave a space on the end of this for it to work properly.  Also, you can change the port number if you wish)

Press OK.  You can now launch the development server from the new entry on your Tools menu.  This will show in your system tray.  You should probably remember to close it when you are done.

The next step is to configure your project to use the server.  Right click on your project and click Property Pages.  If an empty dialogue comes up, press cancel and repeat the process – it should work second time.  Under Start Options, select Use custom server and leave the Base URL blank.  You may wish to change the start action as well.

Once you are done, you can press F5 to start debugging.  Don’t forget that next time you open the project you will need to start the server from the tools menu before you start debugging again, otherwise it won’t work.

Also, bear in mind that if this is a copy of a remote site, things like database connection strings may need changing.  Don’t forget to be careful not to overwrite any settings when you copy back if that is the case!

If you do want to develop on a copy of the site, the Website menu has a useful option to Copy website.

One and One Dedicated Hosting Review

Friday, July 10th, 2009

For the last few years, I have been using a Fasthosts dedicated Windows 2003 x86 server to run a number of different projects on, but I have always found that their proprietary Matrix control panel is very buggy and light on features to the point of being completely unusable for reselling space.

I recently found that 1&1 are doing some very nice Windows 2008 x64 Servers with 100MB connections, no explicit bandwidth cap and they come with Parallels Plesk Panel, which is pretty much the industry standard control panel for Windows.  I’m still in the process of setting this up at the moment, and I have had a couple of annoyances from Windows 2008 x64, but that isn’t really 1&1‘s fault.  They have been quite helpful and seem pretty eager to help, unlike Fasthosts who pretty much just said it was my fault whenever I reported a problem.

The main disadvantage of using 1&1 that I can see is that their datacentres are in Germany.  The connection speed is perfectly good, but from the point of view of Google Local Search, this can be a little annoying as Google can mis-identify the country of a website.  However, as far as I understand, Google checks the domain extension first (so if it is .uk it should be ok), and then if it can’t identify it from that, you can still override the IP check using Google Webmaster Tools.

Anyway, although I haven’t started using it properly, and I have been getting a bit annoyed with Windows 2008 x64, it does seem like a pretty good deal and I’m very happy with what I’ve seen so far.  Oh yeah… they also have an affiliate program which is quite good 😀

.NET for Linux or Other Platforms – Novell Mono

Friday, July 10th, 2009

Whilst most of my work is done in VB.NET for Windows client and server PCs, I do also have a Linux server which I mostly am stuck coding in php on.  It would be nice if it were possible to code in .NET on it, and this is actually partially possible.

Novell have been working on a project called Mono over the last few years to create an alternative implementation of .NET for other platforms, including Linux.  They have implemented most of the major features and classes, but there are still a few areas which are patchy.  I usually find that the Handles VB.NET keyword doesn’t work and causes a compiler error, but it is quite possible to make a decent site that operates on Mono.  I’ve even compiled a DLL file in Visual Studio and used it in a Mono project’s Bin folder.  Of course if you do that, you have to be careful only to reference classes which are available in Mono.

FrontPage Server Extensions in Windows 2008

Friday, July 10th, 2009

As I mentioned in a previous post, I am currently in the process of switching to a One and One dedicated Windows 2008 x64 Server.   I was trying to work out how to set up FrontPage Server Extensions, which I’m not a big fan of, but I do find them very useful for editing remote sites directly through Visual Studio and my life would be a lot more difficult without them.  The problem is, I couldn’t find any reference to FrontPage Server Extensions anywhere in IIS7.

After a bit of research, I found that Microsoft no longer support FrontPage Server Extensions in Windows Server 2008 as they have replaced it with WebDAV, which seems technically much better.  The problem is that WebDAV isn’t supported directly by Visual Studio (please sort that out Microsoft!), so the only way of using that is to use WebDAV Redirector so that you can map a network drive for the folder and then access the network drive from Visual Studio.  This is not really a practical solution for me as I would have dozens of sites to keep track of and I don’t have the patience to remap them when I need them or keep track of so many different drive letters!

A quick hunt around online for an alternateive gave me Ready to Run Software’s site, which frankly looks suprisingly unprofessional if you believe that they are actually working with Microsoft.  I don’t know whether they are or not, but they do seem to be the sole supplier of a ported version of FrontPage Server Extensions for Windows Server 2008.

All credit to them, their port of it does seem to work, although I did get an error “unable to read configuration for Microsoft Internet Information service” when I tried to install it, but their FAQ deals with that.  I managed to get it working, although it is a real pain in the neck that you have to be logged in as Administrator and not just an administrator!

The problem I had was managing the FrontPage settings for the website itself – I wanted to add another user.  I couldn’t seem to log in to the site admin – it wouldn’t authenticate me whatever username I used or whoever I made the web administrator.   The only solution I found was to use owsadm.exe, which took a bit of hunting around for how to do it.

First of all, I found it in C:\Program Files (x86)\Common Files\microsoft shared\Web Server Extensions\50\bin\

Secondly, I needed to work out how to call it.  It isn’t very well documented, but you need to do something like this…

owsadm -o roleusers -c add -u username -m domainname -web /subweb -name Admin

replace username and domainname as appropriate, and /subweb with the path to the subweb that you want to add the user to.  That can just be / for the root web.  You can replace Admin if you don’t want to use the administrative role for your new user.  I’m not sure what the other role names are.

Linq to SQL Quirks Part 4: Circular References

Friday, July 10th, 2009
This entry is part of a series, Linq to SQL Quirks»

In a project that I am working on at the moment, I have a situation where I have a Person table and a Family table.  The Person table has a FamilyID field, which is a foreign key to the Families ID (primary key) field, and the Family table has a DefaultPersonID, which is another foreign key back to Person to store the default contact for a family.

The problem is that if I create a new Person and Family with the Person being the DefaultContact, I get an InvalidOperationException thrown with a message “A cycle was detected in the set of changes”.  The problem is that Linq to SQL doesn’t know what to store first.  The only way that I have come up with to get around this is to do the following…

            If Person.ID = 0 AndAlso Not Person.Family Is Nothing Then
               
                Dim fam = Person.Family
                Person.Family = Nothing

                DBContext.SubmitChanges()
                Person.Family = fam

            End If

            DBContext.SubmitChanges()

This works by breaking the circular reference, so that there is only a 1 way reference, then submitting to the database, and once submitted, restoring the removed reference and submitting changes again

Installing SQL Server 2008 x64 on Windows 2008

Friday, July 10th, 2009

I’m just in the process of switching my Windows Server from a Windows 2003 Server on Fasthosts to a Windows 2008 x64 one on One and One, who are offering me a much better deal.  Anyway, I’ve been having some interesting problems installing SQL Server 2008 Express on it.  I mean, it didn’t help that I didn’t realise that the server was preinstalled with SQL Server 2005 Express, but it surely shouldn’t be that hard.  I’ll skip to the point where I had uninstalled 2005 and was trying a fresh install of 2008…

First of all, I tried the Microsoft Web Platform Installer, and it died with an error saying “The INSTANCESHAREDWOWDIR command line value was not specified”.  I thought that this would be easily fixable if I just ran the installer myself, so I tried that.  It seemed to be going fine until I got to the page where I needed to specify what features I wanted to install, and there were 2 textboxes to specify folders at the bottom – one for the x64 main folders and one for the x86 WoW components (that’s Windows on Windows – not any other WoW you might have been thinking of).  The problem was that the main one was disabled (greyed out) and was trying to install to the Program Files (x86) folder instead of the Program Files folder.  When I tried to go to the next page, I got the same error.

I found that if I changed the x86 path, it would let me continue, but I didn’t want the main folder being installed wrong, so I followed through the installer to the Ready To Install page and at the bottom of the page there is a configuration file path.  I copied the config file and edited it to change the paths and then used the command line to launch the process using /action=install /configurationfile=configfilepath /q .

This seemed to work, but I had made a mistake with the configuration file (wrong instance name) and couldn’t be bothered going through the whole procedure again, so I called setup with all the parameters on the commandline.  Microsoft have documentation on what they all are.  It took some experimentation, but I think I used something like

/action=install /q /indicateprogress /features=sqlengine /installshareddir=”c:\program files\microsoft sql server” /installsharedwowdir=”c:\program files (x86)\microsoft sql server” /instancename=MSSQLSERVER /securitymode=”sql” /SAPWD=sysadminpassword

… and that seemed to work.

I’m not sure what Microsoft are playing at with this installer though.  It took me a good few hours to work this out and with a number of different permutations I tried, it just dropped out with no error message!  Also, the error is wrong – it specifies INSTANCESHAREDWOWDIR, which isn’t a parameter.  They actually meant INSTALLSHAREDWOWDIR.

.NET Compilation Part 2: Conditional Compilation

Tuesday, July 7th, 2009
This entry is part of a series, .NET Compilation»

Conditional compilation is being able to tell the compiler to only compile certain areas of code if some conditions are met.  For example, you have probably noticed that Visual Studio sets up 2 different compilation profiles with each project – Debug and Release.  The Debug profile has a constant enabled called DEBUG.  You can find this by going into My Project, Compile tab, and clicking on the “Advanced Compile Options” button.  There is a section there called “Compilation Constants”.  There are a couple of pre-defined ones with checkboxes – DEBUG and TRACE.  You can toggle these on and off.  You can also add your own custom constants.  Another way of defining constants is using #Const in a code file.

These constants are used by the compiler – they aren’t like normal constants.  That means that you can create different modes of compilation.  Eg. if you are producing an application which is available in 2 different editions (“Home” and “Professional” for example), you may wish to define #Const EDITION = “Home”.  You can create separate compiler profiles for this as well, so you can have Home Debug, Home Release, Pro Debug and Pro Release.

Once you’ve got a compiler constant, you can use it to decide whether some code is compiled or not.  This is done using #If.

For example,

#If EDITION=”Home”

‘home code

#Else

‘pro code

#End If

The important thing here is that when you compile in Pro mode, the home code never actually makes it into the compiled assembly at all – it isn’t just that it is never executed.  This means that your assembly is smaller, and there is no way of hacking around it to get the code to work, because it just isn’t there (unless you hack it in).  Also, when you switch Visual Studio to use a different compilation profile or change the constants around, it effectively treats the code that is within the blocks that are not to be compiled for the current profile as though they were just like comments.

Visual Studio pre-defines some useful compiler constants which you can use as well…

CONFIG is the name of the configuration (Debug/Release by default)
DEBUG is whether we are compiling in debug mode or not
PLATFORM is the CPU platform we are targeting (eg. x86, x64)
TARGET is the assembly type (eg. winexe)
TRACE is whether debug tracing is enabled
VBC_VER is the version number of the VB compiler (I assume C# has some equivalent like CS_VER)
_MyType – I’m not sure exactly what this is – I think it is the type of application that you are compiling so that the compiler knows what structure to use for the My. namespace.

Entries in this series:
  1. .NET Compilation Part 1: Profiles
  2. .NET Compilation Part 2: Conditional Compilation
Powered by Hackadelic Sliding Notes 1.6.5

IsNot Operator in VB.NET

Friday, July 3rd, 2009

Somehow I seem to have missed Microsoft adding in the IsNot operator into VB.NET.  I’ve just found it now.  It will make things a lot easier to read and write.  The number of times that I write If Not a Is b Then …  it should save me a lot of annoyance to write If a IsNot b Then …I’ve tried “Is Not” in the past with a space, but that doesn’t work.

CType and DirectCast

Friday, July 3rd, 2009

VB.NET supports 2 different keywords which are related to casting.

DirectCast takes the object which is passed in and assumes it to be of the casted type or an inherited type.  This means that if you try to DirectCast an Integer to a Single, it won’t work.  If the compiler can see that it won’t work because it isn’t an inherited type, you’ll get a compiler error.  If you try it on a variable in eg. an Object at runtime, you’ll get an InvalidCastException.

The alternative is to use CType, which is much cleverer and will try to find a way to convert from one type to another.  This means that you can Ctype(“2.334”, Single) and it will parse the string and give you a Single length floating point number.  It will also check for overloading of the CType operator to see if it can convert using that.  This makes it very powerful, although it does come with the cost of a lot more processor usage.  However, for most modern applications, unless you are doing something heavily time constrained with a large amount of iterations in a  loop, it is unlikely to be an issue.

SQL Management Studio 2008 Import/Export Data (Replacement for DTS)

Wednesday, July 1st, 2009

The old SQL Enterprise Manager used to have something called Data Transfer Services, which allowed you to transfer data between SQL Server and a different database using ODBC.  This was curiously lacking in SQL Server Management Studio 2005 (at least the Express one) unless I missed it.

However, in the 2008 version, you can right click on a database, select All Tasks and there are options for Import Data and Export Data, which open an appropriate wizard to guide you through the process.