More servicesWindows Live
HomeHotmailSpacesOneCare
 
MSN
Sign in
 
 
Spaces home  DevelopmentalProfileFriendsBlogMore Tools Explore the Spaces community

Blog

August 29

DEVSTA - The programming competition for star developers!

Although it hasn't been officially announced by Microsoft yet, the information was revealed on the OzSilverLight mailing list today that there will be a programming competition for Australian developers starting in September.

Here is the URL: http://devsta.microsoft.com.au/

What's really interesting about this particular competition is that it is offering some seriously cool prizes! For instance, first place is an all expenses paid trip to Mix09 in Vegas next year, an XBOX Elite plus games, and a MSDN subscription.

The "theme" of the competition will be announced on 29th September, and then it will be open slather. Developers will have exactly 200 hours and 8 minutes to complete their submission for the competition.

So start stocking that jolt cola and be ready on 29th Sept to compete to become the next DEVSTA!

August 20

System.IConvertible - Its not a Ferrari

You might be familiar with the System.Convert class, which has a number of methods for converting from/to different types. For example:

short s = Convert.ToInt16(int.MaxValue);

We live in a world of casting and you might find this code somewhat pointless. After all, it will still raise StackOverflowException and lose precision with floating poing conversions, so what's the point?

The convert class is capable of converting an instance of ANY type into one of the common base types (sting, int, ulong, char, etc). If a type implements the System.IConvertible interface, it can define how conversions of that type will occur.

Lets consider a (not so practical) example. Say we have a product class with the following properties:

public int ProductId { get; set; }
public DateTime DateAvailable { get; set; }
public string Name { get; set; }

Our Product class also has a parameterless constructor. In this example, we also make the Product class implement the IConvertible interface. This forces us to implement 16 methods and 1 property. Those methods all relate directly to conversion. For example, here's the implementation of one of those methods:

public ulong ToUInt64(IFormatProvider provider) {
      return (UInt64)ProductId;
}

What we are saying here is that if someone tries to convert our Product instance to a ulong then it will simply assign the ProductId. Our test code looks like this:

Product p = new Product { Name="Car", DateAvailable=DateTime.Now, ProductId=7 };
UInt64 i = Convert.ToUInt64(p);
Console.WriteLine(i);

The output is the number "7" because that is how our implementation of IConvertible tells the Convert class to handle conversions to UInt64.

Simple eh?

 

July 01

Rolling back a changeset in TFS

In TFS 2005 and 2008 there is no easy way of rolling back a changeset. Sure, you can do a "get specific version" of each file from before the "bad" checkin occurred, and then check the older version back in. But this really sucks if you have a lot of files in your changeset.

Luckily the Team Foundation Power Tools can help you here. One of the command line options is as follows:

tfpt rollback /changeset:xxxx

Here, you simply replace xxxx with your changeset number, and the power tools will take care of the rest.

First, it will "get latest" of the workspace. Next it will perform an operation for each file that is required to roll it back. For example, if you added a file, it performs a delete. If you deleted a file, it performs an undelete. If you simply edited a file, it will get the code from the changeset previous for that file. The result on the command line indicates which changeset was used for each file it rolled back.

It then pops up a window requesting confirmation for all the files. After this, it checks out all those files it will roll back and you can then check them in (or only check some in if you so wish).

One caveat with this process is that when you are executing the command line, you might get an error: "Unable to determine workspace". This simply means you are not currently in a folder that is part of a workspace. Change directory into a folder that is stored in the workspace you want to use (for those that use multiple workspaces with different folders).

Good news is that Rosario will have rollback features incorporated. Check out the help for rollback for more information:

tfpt rollback /?

Technorati Tags: ,,,,
June 13

Destroy Work Item : Is it really gone?

This week we had a situation where we needed to remove a work item from the TFS database completely. You can delete a work item in Team Explorer however this just marks the row in the database as deleted and as such your database will always grow, never shrink.

The Visual Studio Team System 2008 Team Foundation Power Tools give us the ability to destroy a work item with the 'destroyWI' call. Here's the command line syntax:

tfpt destroywi /server:<SERVER> /workitemid:<ID>

The 'intention' is to delete all instances of the ID from the database, and all related data, and in truth it comes pretty close. However there is one table it misses: 'WorkItemsWere'

If you look at your TFS database instance you will notice there are actually a few databases. One of them is 'WorkItemTracking' and this database contains all the tables related to work items. The astute might also notice that each of the databases in their instance seems to match each of the web services provided by TFS. Coincidence?

In the 'WorkItemTracking' database you will see a number of tables that all contain work item related data. In particular, notice the two tables: 'WorkItemsAre' and 'WorkItemsWere'. The 'Are' table contains the current state of a work item while the 'Were' table contains its history.

For example, work item 1234 is modified several times. First it is opened as a bug and assigned to 'triage'. Then triage moves it to the developer who fixes the bug and resolves it with comments, where it is assigned back to the user who opened it. The bug isn't fixed, some additional comments are inserted, its assigned back to the developer, and so on. If you are familiar with work items, you will be able to identify the 'History' section where you can view the audit history. This essentially is what is stored in the 'WorkItemsWere' table. Makes sense now right?

Yesterday I did a destroy of a work item and discovered that all the various tables had all instances cleaned up, except the 'WorkItemsWere' table. I contacted the VSTS team who indicated that this is indeed a bug and it is now logged in their own work item system (oh the irony!).

What to do in the mean time? Well they assure me it is safe to delete the orphaned database rows, despite the general consensus on the net not to mess with the database directly. I can confirm however that this is the only table with orphaned data. And I'm sure this will be fixed up in a future release of the power tools. All round I quite like the TFPT and haven't even bothered with the multitude of 3rd party components that put a UI in front of it: real men use command line!

June 01

Advanced Generics

I've recently been reading this book by Jon Skeet about C# 2 and 3, and it got me all inspired to write about generics. Jon's great; he's extremely active in the C# and Java community, and recently responded to an email question I had in less than 10 minutes! I'm finding the book insightful and useful and greatly enjoying my bus rides to work when I get to read it.

This post is going to assume you are familiar with using generic classes like List<T> and Nullable<T>. So lets start with the syntax for declaring our own generic class:

public class Monkey { }
public class Animator<T> {
    private T instance;
    public Animator(T t)
    {
        instance = t;
    }
    public void MoveOnce() { }
}
Animator<Monkey> animator = new Animator<Monkey>(new Monkey());
animator.Move();

This is pretty simple: the Animator class accepts a generic type T and requires you pass a T in its constructor. The MoveOnce() method is empty at the moment, we'll get to that later.

Currently our generic Animator class is somewhat type-safe: we can only pass instances of T to its constructor, but we don't really know much about T. We'd like to be able to move our instances (such as a monkey) so we need to define some things about what kinds of T we are willing to accept. Lets go back to our Monkey class for a moment and define an interface for it:

public interface IMoveable {
    void Move();
    void Stop();
}
public class Monkey : IMoveable {
    public void Move() { }
    public void Stop() { }
}

Great! We've decided that the fact a Monkey can move and stop is pretty general, and is probably something other animals can do, so we've declared an interface to indicate our monkey has those capabilities. We might also define IEater, or IDungFlinger. Now we can revisit our generic type in our Animator class definition as follows:

public class Animator<T> where T : IMoveable
{
    private T instance;
    public Animator(T t)
    {
        instance = t;
    }
    public void MoveOnce() {
        instance.Move();
        instance.Stop();
    }
}

This is where the magic begins. We've placed a constraint on our generic type, stating that it will only ever accept a type that implements our IMoveable interface. If we try to use the Animator class with something else, we will get a compile-time exception. Even better yet is that our development environment knows that since our instance will always implement IMoveable, it therefore must have a Move() and Stop() method, therefore we can call it anywhere in our class; in this case the MoveOnce() method.

You can declare more than one generic type in your class definitions. Imaging a class that facilitates a race for monkeys, whether they be chimps, gorillas, or marmosets. Consider this generic class declaration:

public class Race<T, U> where T : Monkey where U : Monkey {
    public Race(T monkey1, U monkey2) { }
    public void RunRace() { }
}
public class Marmoset : Monkey { }
public class Chimp : Monkey { }

Here we have defined that our class accepts 2 generic types: T and U. We have further restricted them that they both have to inherit from the Monkey class. Here's some example calls using this new class:

Race<Chimp, Marmoset> race1 =
      new Race<Chimp, Marmoset>(new Chimp(), new Marmoset());
Race<Monkey, Chimp> race2 =
      new Race<Monkey, Chimp>(new Monkey(), new Chimp());
Race<Marmoset, Marmoset> race3 =
      new Race<Marmoset, Marmoset>(new Marmoset(), new Marmoset());

As you can see that anything that inherits Monkey is valid. Of interest, the second example shows that even an instance of Monkey is valid. This shows that the where clause in our type restriction means anything that inherits from X or is X.

Finally here are a couple of extra cool things you can do with generic type restrictions as general non-monkey related examples:

public class TestCase1<T> where T : class { }
public class TestCase2<T> where T : struct { }
public class TestCase3<T> where T : IDisposable, IMoveable, IComparable { }
public class TestCase4<T, U> where T : U { }
public class TestCase5<T, U> where T : U, IMoveable { }
public class TestCase6<T, U> where T : class where U : struct{ }
public class TestCase7<T, U> where T : class where U : struct, T { }

TestCase1 demonstrates that we can force the type to be a reference type.
Likewise TestCase2 ensures that the type must be a value type. How is this useful? Well consider a generic class that performs expensive operations such as copying from one list to another. Reference types are just pointers and therefore inexpensive to copy around, thus ensuring only reference types are allowed in our class could be quite useful.

TestCase3 shows that we can ensure T implements multiple interfaces.
TestCase4 shows that one generic type can be forced to inherit from another. An example of an implementation might be:

TestCase4<Chimp, Monkey> test = new TestCase4<Chimp, Monkey>();

This is valid because Chimp inherits from Monkey.
TestCase5 shows that a type can be restricted to inherit as well as implement interfaces.
TestCase6 demonstrates that one type must be a reference type while the other can be a value type.

The last TestCase7 is somewhat interesting. Its declaration indicates that T is a reference type, and U is a value type that must inherit from T. How can this be? It essentially states that you must pass a struct that inherits from a class. But C# won't let you declare this.

To be honest at first I didn't know the answer to this. Thus the email to Jon, and his explanation was simple and it made sense: interfaces are reference types. Hopefully you are aware that structs can implement interfaces also (but can't do inheritance), so we might define a new animal as follows:

public struct Mouse : IMoveable {
    public void Move() { }
    public void Stop() { }
}

Then we can legitimately use the TestCase7 syntax:

TestCase7<IMoveable, Mouse> test = new TestCase7<IMoveable, Mouse>();

Then of course, valid values for an IMoveable are a Chimp, Marmoset, or even another Mouse, while of course you can only use a Mouse for the second type.

Hopefully this has given you a better idea of what you can do with generics.

May 29

Contravariance, Covariance, and Generics

The common question is: what the hell is contravariance and covariance?

Ok so you are familiar with casting right? In C# we can do this:

public class Animal { }
public class Monkey : Animal { }
...
Animal a = new Monkey();
((Monkey)a).FlingCrap();

We can also do this:

public void MoveWest(Animal a) { }
...
MoveWest(new Monkey());

Because the 'MoveWest' method accepts the narrowing type 'Animal' it is said to support 'Contravariance'. If the C# compiler forced us to only pass exact types, then it would be considered 'invariant'. Likewise C# methods support covariance, in that we can return a narrower type and cast the resulting instance to its correct form.

There are parts of the .Net framework however that aren't so flexible. In this case, I'm talking about generics.

Consider this for example:
List<Monkey> monkeys = new List<Monkey>();
monkeys .Add(new Monkey());
List<Animal> animals = monkeys;

Unfortunately this does not compile. We get the following warning:

Cannot implicitly convert type 'System.Collections.Generic.List<FeaturesCsharp2.Monkey>' to 'System.Collections.Generic.List<FeaturesCsharp2.Animal>'

Why is this? Monkey inherits Animal, so the list conversion is perfectly safe. Well this is one case of invariance. But here's the kicker: word on the street is that the CLR actually supports contravariance of generic lists; its just the C# compiler that doesn't!

Circle Of Interest

My mate and fellow Readifarian Paul Stovell recently posted his 'Circle Of Interest' and started a meme by nominating 5 others to do the same. I was one of the lucky ones, so here's my circle.

Corethings

I don't doubt this circle will change completely in 6 months. I expect the TFS parts to fade into the middle circle, with more focus on WPF and SilverLight perhaps. Who can tell?

The big red circle indicates things I don't care about, the baby-vomit yellow indicates stuff I am interest in but have little time or dedication to fully pursue. The dog-vomit green is the stuff that I am loving working or am most eager to develop as soon as possible.

Oh, WCF shouldn't be in the middle circle, but I couldn't be bothered changing it.

Now, to continue the meme, I nominate these people:

Matthew Rowan , John McFadyen , Joseph Cooney , Leon Bambrick

Get to it chumps!

May 05

Personal Development Plan

At Readify we are encouraged to do a plan that we can use for guidance in our personal development. I was pretty generic with mine and have decided to give it more focus today (given that its labour day and all). I figure I'd post it here so that I can hold myself accountable to it next year. Lets say, end of financial year 09? Gives me 14 months to see what I can achieve off of this list. Naturally having a baby will affect this in yet to be discovered ways.

The list is priorities by importance for high level headings only. Its mostly just a bullet point list, which is interesting because I am unsure how to measure success for some of these things (for example, when will I 'know' WCF? I've done a bit already but still am not across all the security aspects).

Anyway, here's the list. Feel free to critique or post your own list in response.

Certifications (importance: HIGH)
· MCTS: Web Applications
· MCTS: Windows Applications
· MCTS: WCF
· MCT: Certified Trainer
· MCTS: Business Intelligence

Improve blog quality (importance: HIGH)
· Attract more readers
· More diverse topics (as my knowledge increases)
· More frequent postings

Specific Technologies (importance: HIGH)
· LINQ, dLINQ, xLINQ, etc
· SilverLight
· Expression tools
· Windows Presentation Foundation
· MVC Framework
· ADO.NET Data services
· ADO.NET Entity Framework
· Windows Workflow Foundation
· Windows Communication Foundation
· Active Directory, DNS, and networks
· Sharepoint
· Intermediary Language
· Team Foundation Server / Team Build
· Visual Studio Team System
· ASP.NET Ajax
· Business Intelligence
· Reporting Services
· BizTalk
· Integration Services

Attend Special Interest Conferences (importance: HIGH)
· Tech Ed - Australia
· ReMix - Australia
· Mix - United States
· Tech Ed – Europe

Presenter (importance: MEDIUM)
· User groups / SIGs
· Code Camps
· Tech conferences like TechEd

Further Study (importance: LOW)
· Physics/Chemistry/Astro-physics
· MBA

Author (importance: LOW)
· A technical book, unsure of topic at this stage
· Articles to MSDN, code magazines, etc

Wow, looking at this list, I feel like I know absolutely nothing! But to be honest, I have groundings in a lot of the technical stuff (like SilverLight, WCF, Reporting Services, BI, LINQ, etc), but I really want to gain expertise in these areas.

Why do I rank certifications so highly? Well I'm keen on the MCT, which requires an MCTS or two. Also they are useful to an employer for points towards partner status. Finally, its a personal milestone, ie the certification is for me, not you. I want to pass all those tests based on my knowledge (not cheating, examining practice tests, etc).

I don't expect I'll get to Tech Ed Europe or Mix US anytime soon, but ReMix is a possibility for me this year (I've never been) and Tech Ed is a definite (see you there!).

May 02

So You've Ruined Your Life...

You might recognise this quote from the Simpsons as the title of the book that doctor Hibbard gives Homer and Marge after they realise they are 'up the duff'.
 
Since I just got the news that my wife Sonia and I are now also 'up the duff' I figured it would make for a good article title. I don't actually believe it of course... I'm as excited as a little kid with a lollipop! It will however mean that my lifestyle will change somewhat, since I now have new priorities.
 
So that's all this article is about. Its been just the two of us (married for over 6 years) and a sausage dog up until now. I'm excited, curious, and nervous, all rolled into one. I have no idea how it is affecting my work, but no doubt this will change me in ways I have yet to understand.
 
Anyway, I just want to apologise for the lack of blogging in the last few weeks, and hopefully I'll be back at it again soon.
April 06

TeamBuild, PowerShell, and Exit Codes

At my work I am starting to integrate PowerShell scripts into the TeamBuild projects for our nightly build. Including PowerShell is very easy: you just need to ensure that PowerShell is installed on the build server. Installing PowerShell will adjust the PATH environment variable, but you may need to restart the build service in order for the build server to pick up this new path.

Including a PowerShell script in your TeamBuild (or MSBuild) targets is very easy. You might have a target that looks something like this:

<Target Name="UnrestrictedExecutionPolicy">
    <Exec Command="powershell set-executionPolicy unrestricted&quot;" />
</Target>
<Target name="TestScript" DependsOnTargets="UnrestrictedExecutionPolicy">
    <Exec Command="PowerShell ./Scripts/testscript.ps1" />
</Target

As with most environments, we need to change the execution policy so that it will allow scripts to be run. We only need to do this once for our whole build, so best to create it in its own target and place a dependency to it from other targets that will be calling PS scripts.

In the above example, we are calling a script in the Scripts folder called testscript.ps1. Pretty simple really. If an error occurs in that script, then the exit code will indicate an error, and the build will fail. However, we may not want the build to fail. Perhaps only a warning is required. Or perhaps we want the script to do things that might break the build AND raise warnings. How can we handle this?

An easy way is to use exit codes. Most EXEC calls will return an exit code. 0 usually means success, and 1 or higher means failure. In MSBuild (ie. TeamBuild) that means 1 or higher will break the build. So what if we want our PowerShell script to return all sorts of exit codes, and only have certain exit codes break the build, and others raise warnings? This is easy too, but requires some jigging.

First, we call the EXEC task with an attribute that tells it to ignore the exit code that results from the call. This means that even failed calls will not break the build. Second, we capture the exit code of the EXEC task with the Output attribute and assign it to a Property. The target might then look like this:

<Exec Command="PowerShell ./Scripts/testscript.ps1" IgnoreExitCode="true">
     <Output TaskParameter="ExitCode" PropertyName="ScriptExitCode" />
</Exec>

Cool... we now have the exit code in the property 'ScriptExitCode' and can manually do what we like with it. Using the Warning and Error tasks that already exist, we can react in a more custom way:

<Warning Condition="$(ScriptExitCode)==2"
               Text="Warning: The script raised a warning" />
<Error Condition="$(ScriptExitCode)==1"
           Text="Error: The script raised an error: Build stopping." />

In this example, if the script returns exit code 1, the build still fails. However if it returns 2, then only a warning is raised, and the build continues on.

So only one thing remains: how do we tell PowerShell scripts to return a specific exit code? Well that's just as easy. In our PS script, we can use this line:

$host.SetShouldExit(2)

$host is a reserved variable in PowerShell. In the above code, we are telling the script to return exit code 2 when it exits. Note that this line does not tell the script to exit straight away. To do that, call the command 'exit'.

So using this knowledge, we can sprinkle the relative exit codes throughout our scripts and let MSBuild handle it on the outside.

April 03

How to lose an entire week in conferences and presentations

Brisbane: A rich ecosystem of technology, enthusiasm, and deliverance. I often wonder how I manage to stay married when there is so much going on in this town that keeps me distracted. I'm sure its just as rich in other cities, but hell, I don't care about those infestations. Brisbane is great for the developer/enthusiast and here's why: my week since last Wednesday.

Weds 26th March - Heroes Happen
I was sitting in one of the Server 2008 sessions and I realised "I've seen all this before". It was at that point in time I remembered last year playing around with server 2008 beta 3 and TFS 2008. I had had so many problems, I'd reinstalled the server like 5 times, so I was very familiar with the interface. I was most interested in the Server Core and Hyper-V products and particularly interested in the Server 2008 license now sitting on my desk waiting to be installed. To be honest, I didn't learn much from the developer sessions. I've been using VS2008 for quite a while: both in production over the last 3 months, and beta last year with 'toy' projects. Still, Dave Glover showed a few cool things I hadn't seen before, such as the native debugger support for WCF apps, including the WCF test window.

Thurs 27th March - Sql Server User Group
The guys from Soul Solutions showed up to deliver some useful information on Sql Spatial. Unfortunately I couldn't stay from the whole event, but I hung around long enough to find out that John and Bronwen were in fact the 2 people dressed up as Batman and Batgirl at Heroes! Those 2 are crazy...

Mon 1st April - QUT Alumi Presentation
There was a few hours of work left when Joel Pobar came roaming through the pods trying to drum up interest in a presentation at QUT by Gordon Bell. The topic was around data and its management: how do we derive quality information? He covered briefly his history around the starting of the Microsoft Research Centre and the work done there in recent years around data management, in particular a case study into celestial data and the resulting Worldwide Telescope.

Tues 2nd April - Security Interest Group
A new user group that I only stumbled on as I was walking out the door at the end of the day, this group had a great presentation on Microsoft's product offerings and where security fits into the 'now' and 'future'. While MS product based, it was quite well generalised. I felt quite odd though being the only developer in a room full of system admin-type people! I'm very keen to see where this group goes from here (no link because they don't have a web site yet: hit me up if you want the administrators email, sorry I don't have it handy at the time of this post!).

Weds 3rd April - Briztalk User Group
This is the second time I have come to this group's meetings and have enjoyed them both greatly. The first was about RFID, and last night's was about the new 2008 offerings in Server and Visual Studio. The second talk was old news (again) but once again I felt fascinated by the infrastructure aspects of 2008 Server, and it was my questions (single handedly) that caused the group to finish 1 hour later than it should have! (sorry everyone)

So as you can see, Brisbane is a rich fertile ground of development. I've learnt so much over the last week, its ridiculous. Luckily the VSTS user group (was suppose to be Friday morning) was postponed, help give meaning to the weeks ahead! Next week looks rather quiet, which should be good because I have some interesting ideas around F# that I would like to explore.

See you at the next Brisbane community event.

March 30

Introduction to PowerShell XML

Hopefully by now you have heard of PowerShell (PS) and know that it is a scripting shell much like BASH or any other Unix shell. However unlike those other languages, PS uses an object pipeline, rather than strings, and those objects work directly with the .Net Framework. What this gives us is a powerful, flexible, scripting platform where developers don't have to learn a whole lot before they can start leveraging some real scripting power.

There's lots of examples around how to get started with PowerShell so I'd like to skip that part but first I just want to cover off a few key commands. First, everything in PS is an object of some sort. Unless otherwise specified, most commands will deal with objects or arrays of objects. Since all .Net types inherit from System.Object, this is how PS assumes type safety for all command calls.

Creating an instance of a class is relatively easy. This is how you would create an XmlDocument:

$xml = New-Object "System.Xml.XmlDocument"

The dollar $ sign indicates a variable in PS. The New-Object is a command that accepts a string input for the type. We could then continue to load some XML as we would in .Net:

$xml.Load("c:\somefile.xml")
$xml.LoadXml("<a><b>test</b></a>")

However, PS has a nice XML shortcut that you should know about, and that's the [xml] cast:

$xml = [xml] Get-Content "c:\somefile.xml"
$xml = [xml] "<a><b>test</b></a>"

Now lets use something more interesting for our XML:

$xml = [xml] '
    <world>
        <continent name="Africa">
            <country name="Zimbabwe" />
            <country name="South Africa" />
            <country name="Ethiopia" />
        </continent>
        <continent name="Asia">
            <country name="China" />
            <country name="Japan" />
        </continent>
    </world>'

Another cool operation with XML is referring to Xml Nodes specifically rather than searching. The .Net way would be this:

$continents = $xml.SelectNodes("/world/continent")

This would return an array of Xml Nodes that represent all continents. In PS we can shortcut to:

$continents = $xml.world.continent

You can look at the type of any variable with the standard GetType method:

$continents.GetType()

Interestingly you will see that rather than an XmlNodeList or array of XmlElements, this in fact is an array of Objects! However if we look at the contents of that array we will see each item is something more reasonable:

$continents | % { $_.GetType() }

This command tells us that we have 2 objects of type System.Xml.XmlLinkedNode in our array. The syntax is specific to PS. The | symbol is a pipe, and means to take everything from the left, and shove it into the right. The % symbol is a shortcut for "foreach", which also works just as well. Because we have piped in some content, we don't need to specify any condition logic for our loop, and can go straight to the inner contents. When you don't specify what the name of the variable in the range will be, you can use the $_ variable instead. So overall, the above statement is the same as:

foreach ($_ in $continents) { $_.GetType() }

Let's do something a little different. We will attempt to now take our array of Xml nodes representing continents, and put just the continent name into a generic collection of strings. The syntax around creating an instance of a generic class is as follows:

$list = New-Object "System.Collections.ObjectModel.Collection``1[System.String]"
$list.GetType()

The type shows as Collection''1 without the generic part (string) but it is still there, under the covers. Now lets put the name of all continents into that collection with out % syntax:

$continents | % { $list.Add($_.Name) }
$list | % { $_.GetType() }

The second command shows the type of each item in the collection, and we can see they are all strings.

So that wraps up my first PowerShell post. I've been doing a lot of it this past week, and was very green to begin with. All my work has been around Xml, and I'll be doing some more this week. My specific usage is relating to build and release management in TFS, and am enjoying it greatly.

When it comes to using PowerShell you can create your scripts in any text editor. Currently I am using Notepad++ with PowerShell language definitions, and also PowerGui. PowerGui in particular is an excellent free tool.

March 24

My Introduction To F#

During the week the QLD MSDN User Group was treated to a presentation by Leon Bambrick who demonstrated F# in all its glory. The very next day I had the opportunity to meet with Don Syme, inventor of F# (you never know who you meet sitting at the hot desks in Microsoft SDC!).

To me, this was a sign from the powers that be: play with F# or else! First step to get started, is download the latest version MSI from the Microsoft Research site. After you install it, you can open Visual Studio and you will find that you have a new project template:

image

Not a lot of options for project types I'm afraid. But its a start at least, and Leon indicated there will be full commitment from Microsoft in the future (don't make me a liar Leon). When you create a new project, its very empty. So we add a new item, and notice a few options:

image

To be honest, I have no idea what they all do. I'm as new to this as you! The script file has a FSX extension and this makes me wonder what kind of cool script stuff you can do with it. Undecided, Leon's voice came to me as if in a dream... "Use the Source, Luke, use the source".

Sure enough, the F# Source File option has a FS extension which is what I was looking for. It comes with a heap of template code for you see how the language syntax works. I was pretty comfortable with the magic keyword 'let' already from Leon's presentation. I set off instead to get the 'interactive' mode to work, since I found this quite a neat feature.

So it seems that there is a 'FSI.exe' which stands for F# Interactive. It is a command line tool that allows you to pass snippets of F# code followed by two semi-colons ;; to terminate the parameter. It seems to remember your 'state' such that functions are remembered in between calls. This is further implemented as a command window in Visual Studio, which is a nice feature. You have to activate it after you have installed F# MSI, by turning it on in the 'Add In' section:

image

Tick all the boxes, and the F# Interactive window opens. Now you can start typing inline F# code to your heart's content! You can even execute sections of existing code in your .FS files by highlighting a section of code, and pressing ALT-ENTER. This is very similar behaviour to SQL Management Studio - Query window, where you can highlight some T-SQL and press F5 to run just that code. The F# Interactive window will show any errors inline.

For example, when you add a new source file, it comes with a bunch of syntactical examples, and one of them is the "highest common factor" code, as follows:

let rec hcf a b =           // notice: 2 arguments seperated by spaces
    if a=0 then b
    elif a<b then hcf a (b-a)           // notice: 2 arguments seperated by spaces
    else hcf (a-b) b

So 'let' is kind of like 'dim' in VB, or 'var' in C# / Javascript, but, er, not really. 'rec' means that the call is recursive. You can't have a function that calls itself without the 'rec' identifier. Can't say that I am a fan of 'elif' instead of 'elseif'.

I wanted to test to see if this function really works as indicated. Some tests I thought up in my head:

hcf 15 5 // Should return 5
hcf 48 52 // should return 4
hcf 1 1 // should return 1

I could have deleted all the other redundant code, and entered my 3 tests above, then compiled and run it, but I suppose I would also have to print out the results or some such... too much hassle! So I highlighted the function definition and pressed ALT-RETURN to run it in interactive mode. Then in the interactive window, I typed each example, placing ;; at the end. Each time it returned the correct result:

val it : int = 5

Finally I wanted to see the DirectX demo again that Leon showed in his presentation. The MSI also installs these examples, and I ran the DirectX demo in script mode, just to prove to myself Leon isn't a trickster or a witch (burn him!). Very impressive stuff.

Great! I now feel that I can put F# on my resume. I am interested in exploring it some more. I have some thoughts around graphing and other mathematical applications. I am particularly interested in finding examples of complex algorythms made extremely simple through the F# syntax. Finally, since FSI can effectively be called script-like (and there are those mysterious FSX files), what advantages might we be able to see in using it with other scripting tools/languages like PowerShell?

Oh, see you all at the launch on Wednesday.

March 20

Recent Announcements

Some interesting things announced today. First, the official dates for ReMIX Australia:

Sydney: Tuesday 20th May
Melbourne: Thursday 22nd May

To be honest I've never been, but I think I'll see if it's doable this year.

Secondly, the speakers for CodeCampOz are announced! Check out the sessions here.

Of particular interest to me are the following sessions:

  • Interactive video using Silverlight and WCF - Jonas Folleso
  • ORM Smackdown - Adam Cogan

Oh and don't forget the Readify presenters!

Finally, my new Microsoft SDC friend: Joseph Cooney on Silverlight 2.0 and WPF (even though it doesn't say WPF on the CodeCampOz site, trust me, he's doing a WPF / SL comparison).

All in all its going to be a great weekend! Look forward to seeing you all there.

March 16

Visual Studio Built-in Web Server

No doubt you've hit F5 to debug your web application from time to time, and you noticed the little web server that sits in your system tray.

aspnet_devserver1In the old days you needed to have your web site setup as a virtual directory in IIS in order to be able to debug it. Now days it's a lot easier: the first time you debug Visual Studio starts up its own mini-IIS that hosts your web application and lets you debug. This works for web sites, ASMX web services, and WCF services.

By default, it assigns a random port on the local machine. If you already have IIS then no doubt it is configured to use port 80, which is why the VS web server uses another random one. When your application loads up in browser, it will usually have a URL similar to this:

http://localhost:53669/default.aspx

Even when you stop debugging, the web server continues to run! Which means you can manually open a browser and still hit that same location. This is perfect if you don't have a version of XP or Vista that comes with IIS.

However, by default the port number is random. This means tomorrow when you go to open and debug your application, it will run up with a different port number. Sometimes this is no big deal, but with web services and (in my case) WCF services, this hurts. Your WCF endpoints will be configured for debugging and the endpoints will specify the port number, so that you can debug them in VS. Every time the VS web server starts up, it gets a new random port number, which you then have to update in your WCF client endpoint.

Luckily the internal web server is configurable. With your web projects there is a property under the project properties - Web section:

webproject_properties

By default, the "Use Visual Studio Development Server" option is checked, and "Auto-assign Port" is selected. However you can change this to "Specific Port" as I have in the above example. You can also configure the web server further to pretend to use a virtual directory. This is great because it allows you to simulate production environments by specifying the same combination of ports and virtual directories as would exist on the production server.

Of course, you can save yourself the hassle of having multiple web servers running at once by just selecting "Use IIS Web Server" instead, however this isn't ideal in environments where multiple people work on a project and could potentially have different workstation setups.

Further Resources
ASP.NET and IIS Configuration
Build Web Server Solutions with End-To-End Extensibility
Web Servers in Visual Web Developer
Troubleshooting the ASP.NET Development Server

March 07

Notification Services : Debug Tips and Tricks

Sql Server Notification Services can be tricky to debug. You have no code that you can put a breakpoint in (except for your managed api calls of course) and there's a lot of working parts inside. Here's a couple of tips that have helped me today dealing with a local and alpha problem.

Rule Testing
After you upload your instance and the application is created, a view is created for your event, subscription, and notification schemas. This means that the Sql you define in your subscription rule can be run against the table. I tend to remove the "INSERT" part of the rule, and just execute the select to make sure that my event is getting picked up and associated with a subscriber.

Even one step lower, you can just view the contents of your events schema (view) to make sure the event was posted there correctly, and view your subscriptions schema (view) to ensure that the correct subscription information is in there.

Enable Distributor Logging
In your Application XML there is a node for settings called <ApplicationExecutionSettings>. You can use this to turn on 3 layers of debugging in your Notification Services application as follows:

<DistributorLogging>
    <LogBeforeDeliveryAttempts>true</LogBeforeDeliveryAttempts>
    <LogStatusInfo>true</LogStatusInfo>
    <LogNotificationText>true</LogNotificationText>
</DistributorLogging>

Obviously this will add some overhead to your processing, but if you don't have high through-put then you could leave this on all the time. But for initial setups (such as on your local machine, or on production databases) it is invaluable.

As the name suggests, the logging occurs around distributions of notifications. Your event should get stored in the relevant table after it is submitted, and your rule should process the events and post them to the notifications table. At this point, the distributor logging kicks in. When the service 'ticks' and processes your notifications for delivery, information is logged in a view matching your notifications view. For example, if your notifications class name is "BlogUpdates" then there will be a view automatically created called 'NSBlogUpdatesNotificationDistribution'. You can monitor this view for changes while you post an event that should trigger a distribution. Keep refreshing the view and you will see a new entry appear for a desired notification delivery. It details what delivery channel to use, the user, the content being delivered, and a status of the delivery. Eventually this will change to "Delivery Succeeded". If not, you can find information inline to assist in debugging.

Windows Event Log
Some problems are not specific to Notification Services. For example, your distributor account might not have read access to the XSLT needed to format the content for delivery. In these cases, information is posted to the event log.

Email Notifications
A final tip relates specifically to email notifications. I have noticed that if the SMTP server rejects the service sending the distributions (for whatever reason) you will not receive any information to the affect. Nothing appears in the event log, and nothing in the distributor log view. If you suspect this is your issue, then I can only suggest installing an SMTP relay locally and pointing your delivery channel to that server instead. At least you can get more information in your SMTP logs this way.

More Information On Notification Services
My 4 part series for beginners:
Notification Services - Part 1 - Technology Overview
Notification Services - Part 2 - Example Overview and Instance XML
Notification Services - Part 3 - Application XML
Notification Services - Part 4 - Managed API

Notification Services tutorial: http://msdn2.microsoft.com/en-au/library/ms170337.aspx
MSDN help: http://msdn2.microsoft.com/en-au/library/ms172483.aspx
API reference: http://msdn2.microsoft.com/en-au/library/microsoft.sqlserver.notificationservices.aspx

March 06

Particls

Presumably you subscribe to blogs. You might even subscribe to this blog. How many do you subscribe to? What about other feeds, like Engadget? Podcasts? What about sites you read manually, like your local newspaper site?

Let's be honest, there's a lot of information on the web that we would like to be able to digest. 3 years ago, I would simply load up my browser, go to a url of a site, and read the articles. This is how I kept up with the technology industry and the goings-on. Unfortunately it was hard to sort through and get to the quality pieces of information that I really wanted to know about.

Then I started subscribing to blogs. A few at first. People who were at the top of their game and were driving the technologies I was interested in. I tried some other tools (including Outlook) for periods at a time, and eventually came across Google Reader. By now my list of feeds had grown.

Content Is King

I was following the big corporate bloggers as well as the little guy down the street. I was more interested in the content of the post than the poster themselves. In a move that would shock the world, some of the big guys on campus were scratched from my feed list. I spent a fair amount of time customising my Reader with the information I really wanted to receive. I still had a dilemna though: what about those posters who provide quality sometimes, boring crap other times? Well I guess I just had to skip those entries, 'mark as read', although sometimes I didn't know the post was crap and not related until I had spent time reading the first few lines or summary.

Enter Particls

I can't remember how I first came across Particls. But for me it was a smash hit instantly. The essence of Particls is about attention management. From their About page:

The web is just too big. No one has time to keep track of all the sites and topics that interest them.

You see, every person, site, feed has something interesting to say at some point. Why not have a system that can rate and prioritise the information for you so that you only get presented with information you are specifically interested in?

Particls1
For example, with Particls I can specify all those blog feeds I follow in Reader (imported as OPML of course) and all those feeds I previously removed. Then I can prioritise them individually: which ones are more important to me on the whole? Further, I can specify key words that I am interested in, and keywords that I am not interested in.

Particls2Particls will present the inf ormation to me based on relevancy. By spending a certain amount of time up front defining the kinds of information I want to hear about, Particls can continue to deliver information that I specifically want to read.

Furthermore, you can tell Particls to search for information. This means even if you don't have any blogs you subscribe to, you can still specify keywords of interest, and let Particls do the rest.

The information can be delivered to you in a number of ways. If you must, you can get the annoying popups around the system tray area. You can get a news ticker across the top, or a nice sidebar (my personal choice). You can use multiple options if you like: tell your news ticker to display only the most relevant and important information, tell your sidebar to disappear when not in use and show less relevant information. Particls can even provide an RSS feed for you to consume in another reader if you so desire.

desktop

On my wide screen it works great with Vista sidebar. If you get a notification that is inaccurate, you can simply right click and tell it to give less information about this topic. In this way, Particls quickly learns what your priorities are in order to deliver you more relevant information.

Oh, did I mention Particls is free? Enough reason to go and give it a shot.
Even better, it is produced by a local Brisbane (Australia) company called Faraday Media.

Currently the download link on the site is disabled. I believe this is because they might be moving from beta to production (can anyone shed light on this?). I'll let you know as soon as I hear something.

Update: I've tried to contact Chris Saad from Faraday Media about this a few times this week but didn't want to delay the article any longer waiting for a reply.

Technorati Tags: ,