Archive for the 'ASP.NET' Category

Deploying Web Apps with Powershell

During my recent forays into the world of Rails I have discovered, as do most people working with Rails, the deployment tool Capistrano. For those of you unfamiliar with it, in a nutshell you end up with a simple command that deploys your application either afresh or as an update including all the database stuff etc. Very nifty (or at least it looks like it is as, being a bit of a newcomer to the leafy green world of Linux, I had a few issues but that was me not Capistrano).

In use, you check your source into Subversion and then Capistrano instructs your server to check it out onto the server and organise it all. So the code only gets onto the server when the server ASKS for it rather than when someone puts it there. This is good… Capistrano communicates via SSH and so this is all the server has to listen for. All is well…

Of course, deploying an ASP.NET app is different in that it isn’t necessarily the source that you want to deploy but the binaries. What I hadn’t considered before is to put the builds into source control as well as the source. This then allows you a central repository for the latest build that your servers can draw down from giving you the Capistrano like experience (to a point). What is missing is the SSH-esque bit.

Enter Windows PowerShell. I won’t go on as you can get a good idea of the possibilities by watching Scott Hanselman discuss Corillian’s deployment set up which is extremely impressive I might add.

While I’m about it Scott mentioned that he was after a good PowerShell editor and we had discussed the possibility of a bundle for e-texteditor although I think maybe this fell out of favour a bit as e-texteditor was quite flakey at the time. It is still in beta but much more stable now so that might be something for a budding PowerShell programmer to look into. Also, I just noticed that the new beta of PrimalScript has PowerShell support although I haven’t tried it.

Advertisements

Run Watir Tests from within Visual Studio

I have been having a bit of a play with the Watir Ruby web UI testing framework and after a bit of jiggery pokery I’ve managed to get it all going inside of Visual Studio. Here’s how…

You will need to install:

I had a bit of trouble getting Watir to install on Windows XP using the Windows installer but I think that was due to having installed Ruby using InstantRails. Once I installed it using the Ruby Windows installer it worked OK. On Vista however the installer wasn’t playing nicely at all. To install on Vista required the use of RubyGems which basically involves typing:

    gem install watir

… at the command line. Simple as that. One thing to note is that if you install Watir using the ‘gem’ approach you need to add the line:

    require ‘rubygems’

at the top of your test ruby files before adding:

    require ‘watir’

Thanks to my Ruby (tor)mentor Dave Verwer for that gem. (oh dear…)

Ruby in Steel (worst name ever competition winner 2007) will allow you to debug your Watir tests and edit them with all of the Visual Studio goodness in place.

Once you have all that in order you need a bit of code to hook your Ruby scripts into the usual NUnit/TestDriven way of things. You have two options (writing it yourself being a third). Either go ‘the whole hog’ and try and get this article on Code Project running or use the code below, adapted from Scott Hanselman’s efforts to do the same thing. (I say ‘adapted’. I mean added the ‘directoryPath’ argument as Scott’s version seemed to need the .rb files to be put in the bin, so to speak)

public class WatirAssert 
{ 
    public static void TestPassed(string rubyFileName, 
    string directoryPath) 
    { 
        string output = String.Empty; 
        using (Process p = new Process()) 
        { 
            p.StartInfo.UseShellExecute = false; 
            p.StartInfo.RedirectStandardOutput = true; 
            p.StartInfo.FileName = "ruby.exe"; 
            p.StartInfo.Arguments = rubyFileName; 
            p.StartInfo.WorkingDirectory = directoryPath; 
            p.Start(); 
            output = p.StandardOutput.ReadToEnd(); 
            p.WaitForExit(); 
        } 
        Console.Write(output); 
        Trace.Write(output); 
        Regex reg = new Regex(@"(?<tests>\d+)\stests,\s(?<assertions>\d+)\sassertions,\s(?<failures>\d+)\sfailures,\s(?<errors>\d+)\serror\s", RegexOptions.Compiled); 
        Match m = reg.Match(output); 
        try 
        { 
            int tests = int.Parse(m.Groups["tests"].Value); 
            int assertions = int.Parse(m.Groups["assertions"].Value); 
            int failures = int.Parse(m.Groups["failures"].Value); 
            int errors = int.Parse(m.Groups["errors"].Value); 
            if (tests > 0 && failures > 0) 
            { 
                Assert.Fail(String.Format("WatirAssert: Failures {0}", failures)); 
            } 
            else if (errors > 0) 
            { 
                Assert.Fail(String.Format("WatirAssert: Errors {0}", errors)); 
            } 
        } 
        catch (Exception e) 
        { 
            Assert.Fail("WatirAssert EXCEPTION: " + e.ToString()); 
        } 
    } 
}

Where you stick this is entirely up to you but you need to be able to reference it from your unit tests so you could put it in its own assembly or just add the class to your unit test project.

Once you have all that sorted out you can now add a .NET unit test such as the following:


[TestFixture] 
public class UITest 
{ 
    [Test] 
    public void LoginTests() 
    { 
       WatirAssert.TestPassed("login_tests.rb", @"C:MyAppWatirTestDirectoryUI Tests\"); 
    } 
}

In this example “login_tests.rb” is the name of the Ruby script file containing the Watir test (or tests) and the directoryPath argument is the directory containing said Ruby file.

Now, using TestDriven.NET we can right-click on the above code in VS and run the test. We can add a break point to the C# code and right-click to debug through the test. As we have Ruby in Steel installed we can also use Visual Studio to edit the Ruby file and whilst doing so we can add breakpoints in the Ruby and step into the executing Ruby with the debugger.

Oh joy…

Whilst I am on the topic I have to say that I think Ruby is rubbish. It is supposed to be the language of the Gods and it can’t even tell that when I add a semi-colon at the end of EVERY SODDING LINE, I didn’t mean to do it. Couldn’t it just ignore them for me if it is supposed to be so clever…

SQL Server Deployment Goodies…

I just spent most of the afternoon on a pointless keyboard puncher. I only wanted to move a SQL Server Express database from one place to another which is one of those things that should be straightforward but for one reason or another (which I won’t bore you with) it wasn’t and took me a few hours of messing about to get it to work.

So, it was with a mixture of delight and that feeling you get when you find something in the sales for half the price you paid for it last week that I read Scott Guthrie’s post about deploying Sql Server 2005 databases.

Microsoft are on the way to releasing a Database Publishing Wizard (RC1 is available for download now) which scripts both the schema and data of your SQL Server database.

They also provide an aspx page that you can use to install your db when you don’t have full access to the server.

That would have saved me a fair bit of bother this avo…

Generic Methods… Now there’s a thing

Like the rest of the .NET world I have been soaking up the classes in the System.Collections.Generic namespace which have saved me a shed load of lines of custom collection code.

The other day I was writing some CodeSmith templates and needed a class to represent a property that could be hydrated from an XmlNode. And thus I began…

private XmlNode _propNode; 

public string Name 

{ 

    get 

    { 

        string attName = "name"; 

        //check the attribute exists 

        if (_propNode.Attributes[attName] != null) 

            //it does so return it 

            return _propNode.Attributes[attName].Value; 

        //it doesn't so return an empty string 

        return string.Empty; 

    } 

}

After the first few properties I realised I needed to refactor the XmlNode reading out as I was going to have about 20 properties that did the same thing. The problem was that the Value property of the Attribute returns a string so I was going to have to do some type conversion and so would need a method for each type string, int and bool that I needed. I seemed to recall reading about Generic methods and so I investigated… As it turns out I ended up wrapping this method with a method for each type anyway but I discovered Generic methods along the way which I’d not taken advantage of before.

public void GetAttributeValue<T>(ref T attValue, 

    XmlNode nodeToParse, string attName) 

{ 

    //check the attribute exists 

    if(nodeToParse.Attributes[attName] != null) 

    { 

        //get the string value 

        string strVal = nodeToParse.Attributes[attName].Value; 

        //set the attributeValue that has  

        //been passed in by reference, casting it 

        //from an object using the Generic parameter 

        //and using its own type to perform the conversion 

        attValue = (T)Convert.ChangeType(strVal, 

            typeof(attValue)); 

        attValue = (T)Convert.ChangeType(strVal, 

            typeof(T)); 

    } 

}

You can’t specify a Generic return type so you get the value out by passing in an argument by reference. So, you call this method like so:

public bool IsPrimaryKey 

{ 

    get 

    { 

        //set your default value here 

        //that is returned if the attribute 

        //doesn't exist 

        bool returnVal = false; 

        //call the method specifying the type 

        GetAttributeValue<bool>(ref returnVal, 

            _propNode, "isPrimaryKey"); 

        return returnVal; 

    } 

}

This allowed me to use the same method for all of the types of parameter I wanted to fill from the XmlNode’s attributes.

In the end this didn’t save me a great deal of code from doing it without using Generics and I still ended up casting the return value from an object so there would be no performance benefit either (not that performance was an issue here) but it is definitely an interesting language feature that I am sure I will find more uses for…

Visual Studio on Vista

This all looks a bit alarming… The gist being that none of Microsoft’s dev tools for the .Net framework are fully compatible with Vista and only VS2005 is ever going to be. This leaves mine and everyone else’s 1.1 projects out in the cold as far as Vista is concerned.

There are lots of efforts around to get VS 2005 building against the .Net framework 1.1 but surely, especially in light of the fact that the Windows folk claim it is a piece of piddle to get your applications to run on Vista, we should be able to run 2003.

Baffling…

UPDATE:

Jonathan Allen links to a bit of Microsoft opinion on this issue… The official line lies between “great progress w/Vista on the security front” and “We are just as (financially)constrained as everyone else”. In some ways I can buy the first one. Security is a trade off and generally, to get the benefits, you have to give up something. Whilst the second one may be true in that the dev tools folk no doubt have a budget and a finite amount of resources I don’t think it is a good choice of argument to pacify the VS using public. As it happens, it also turns out that most of VS2003 will work if you are running as admin so hopefully it isn’t going to be too much of an issue.

VBUG Manchester Meet…

Continuing my hectic week of nerd community events (two is hectic by my standards) I attended a VBUG doo in Manchester yesterday evening. The presentation was an overview of the of the newly named, and with characteristic verbosity of course, ASP.NET 2.0 AJAX Extensions and was ably delivered by Gary Rowntree.

The presentation only briefly touched on the other elements of the ‘Atlas’ framework but did cover the server side of things in a fair bit of depth, particularly the update panel and the process it utlises to weave its magic.

I have used all kinds of terribly hackish methods for doing away with obvious page postbacks in the past, which generally involved stashing a hidden page in an iframe or sneaky window, but I haven’t really scratched the surface of the frameworks that are available now. So I’m no expert but what I would say is that we should all give it a little bit of thought before using asynch callbacks for everything. Alex Bosworth has an interesting list of AJAX no nos on his blog. The ease of the update panel might make AJAX monsters of us all…

I stumbled today on a new podcast that I hadn’t listened to before and funnily enough the current episode (show, cast, release… what do you call them?) is about the client library side of the Atlas set up which is the bit that was largely absent from Gary’s talk. The podcast is by Wally McClure and can be found here and includes a demo video.

If you fancy going to any of the Manchester VBUG meetings should keep an eye on the listing or get in touch with VBUG.

Array Serialization in Flex

I have been doing a lot of Flex work over the last couple of weeks. I have to say, it is like a dream come true after having written my fair share of code in the Macromedia Flash IDE which is something akin to eating beans with a toothpick, you get there in the end but it is a long and arduous journey. FlexBuilder, built on Eclipse, is a fully featured IDE that relieves a lot of the frustration.

Anyway, back to the plot… Being a .NET developer at heart my first foray into Flex has been a front end that sits on top of some .NET web services. There are a few options as to what you put behind your Flex apps but .NET was the best fit for the job and my skills…

So off I went and all was going swimmingly until I tried to send an array of integers from the flex app to my .NET web service. The darn thing wouldn’t work. So I opened up (the excellent, probably worth another post) Service Capture and had a look at what was being submitted to the web service.

Flex, it appears, was serializing my int array like so:

<int>1,2,3,4,5</int>

My web service was expecting:

<int>1</int>
<int>2</int>
<int>3</int>
<int>4</int>
<int>5</int>

So what did I do? I spent a while looking around the FlexCoders Yahoo group. Could’t find exactly what I was after so I posted the problem…

The upshot is there seems to be a couple of serialization bugs that Adobe are planning to sort out in the imminent service update. Whether they are responsible for my problem I didn’t find out but here’s hoping.

In the meantime my options are to send literal XML to my web service rather than allow Flex to serialize it for me. This, I imagine will require some adjustments to the back end too although  I haven’t tried it yet and don’t really want to if I can help it, simply because I shouldn’t have to in this day and age.

There are various solutions I could implement on the server such as some sort of custom deserialization but this is all very complicated just so as I can pass an array of integers and  besides which, if this is due to a bug I’ll have to undo it all when it is fixed.

Another alternative suggested on the group was to use JSON. It turns out that there is a Flex JSON implementation and a .NET one, neither of which I had heard of before. Sam Shrefler explains… Thanks Sam that looks just the ticket.