Wednesday, 17 December 2008

What's the score?

For those of you who have attended a training course in the past, you will be familiar with the tradition of completing an evaluation form (aka happy sheet) prior to leaving on the last day. The forms that we have used for quite some time deal with issues such as courseware, course content, instructor, environment etc. Each item can be given a score from 1 (Unacceptable) to 9 (Excellent or Exceeded Expectations).


There tend to be two approaches to scoring amongst delegates. Those who will happily give straight 9s provided they have had a good course and learned some useful things. And those who rarely if ever give a 9 because in their book a 9 is 'the best course ever' and how do they know if they have just had the best course ever? The second approach often leads to several 7s and hardly a 9. Most trainers tend to prefer the former.

Since I am not on a bonus for high scores, I am happy with either approach provided a couple of 8s slip in (praise indeed !). One 8 amongst several 7s from a delegate can mean more to me than straight 9s from a delegate who has put 9s for everything including environment where the room was in a basement with no air conditioning and a boiler fit to burst!

Don't get me wrong. I would love to get straight 9s on every course. Especially as scores are evaluated  by the company and those with higher scores are seen to be 'high scorers!'. They must count for something surely?

Anyway, the reason for this post was as a result of a discussion on evaluation forms with my group last week and a memory that it triggered. It was of a score that I received that put everything into context.

Last year I delivered a standard MOC (Microsoft Official Curriculum course) in .NET development. A MS2555 is I recall. On the first morning I am going through the usual welcome and assessing expectations when one delegate (let's call him Aaron) suddenly launches into "I need to write an application when I get back to work next week and was hoping that you could tell me how I should go about writing it." .

As we had already established that he had NO previous real programming experience let alone any .NET, I said that I would rather not discuss anything relating to his project until Thursday because prior to that, he would have little chance of understanding any of my answers.

I suggested that he bring any of his design/code in by Wednesday in order to give me a little background. This he did, on Tuesday. He also explained over lunch that he was in 'real trouble!'. He had blagged his way into a job with a University in the midlands beginning with the letter 'L' convincing them he was an experienced developer and they had taken him on in order to develop an application that generated statistics.

Normally, I wouldn't get drawn into what was obviously consultancy. However he gave me the 'bunny in the headlights' look of someone who was going to go back to work on Monday and get caught out by Wednesday, only to get fired on Friday.

I didn't point out that anyone dumb enough to have employed him in the first place, probably wouldn't have noticed for a long time.

Anyway, I took his design (aka doodles/wishful thinking) away and did nothing with it until Wednesday evening. As I was working away (again) I found myself in a hotel room with his design, several cans of Fosters and a bag of Bombay mix. What else could I do? The alternative was an episode on New Tricks. And I had already seen it. Does anyone else here have a thing for Amanda Redman?

After 1 minute I knew that the guy had absolutely no chance of writing this application. I read on as I wanted to be able to point him in the right direction. After 5 minutes, I concluded that there was no way that I could tell him everything that he wanted to know AND have him understand it.

One can of Fosters later and I figured "why not?" By 2 am I had written it. It wasn't pretty the UI needed plenty of work. I used an XML file for the data store and I didn't waste any time on exception/error handling. After all, that stuff is for wimps!

I stuck it on a CD and went to bed with a righteous glow. Or was that the Fosters?

In the morning I told him that as I didn't think that I could explain everything that he needed to know, I had simply written it for him. The guy broke into tears and gave me a hug. "I had saved his life!". Actually I had probably saved his career but I wasn't about to split hairs.

Next day, he walks in with a card and a box of chocolates and looks a lot like my son looked like this weekend when he received a PS3 for his birthday!

Six hours later it's time for the evaluation forms. As you can imagine, I am feeling pretty confident. Guess what Aaron gave me? That's right, 7s!

Until this point, I saw a 7 as an insult. Since then I have come to appreciate that for some people, a 7 is as good as it gets and 8s and 9s are stuff of the gods!

I do hope that I have not started a trend of everyone giving 7s as my fellow trainers will never forgive me. If I have, then I will take the flak. Just as long as I get a few 8s and 9s thrown in to keep me ahead!

I wonder if Aaron is still 'getting away with it' ?






See you soon

Phil Stirpé
"I don't do average!"

Silverlight in Sydney

I have just arrived in Sydney to deliver a Silverlight for Developers training event for Microsoft to some of their partners in Australia.

This is the first time that I have made this journey and now that I have arrived at my hotel and opened the mini bar, I sit down reflecting on my journey.


Start : Saturday. 4am GMT

Tetley Tea and Tesco Croissant in my kitchen (Burnley)

Latte and Cafe Nero Croissant at Manchester airport

Coffee on the shuttle to Heathrow

Mineral water at Heathrow

Champagne as I boarded QF32

Smoked Salmon over Prague

Beef Wellington over Kiev

Cheeseboard over Kharkov

Wash, toothbrush and into my Qantas pyjamas over the Caspian sea.

Took two 'special' tablets and activated my Q-Branch seat/bed/pod thing.

Awoke to the smell of bacon over the Indian Ocean

Freshen up and change back into my clothes in time to watch an episode of House before a 1 hour stop over at Singapore.

Managed to send a couple of emails before re-embarking to another glass of champagne.

Somewhere over Indonesia (I think) I had eastern pork entree followed by braised beef.

Tried to watch a movie but fell asleep.

Awoke to a prosciutto and 'something' over Alice Springs.

Add to that, a few litres of water and two beers, that just about sums up my journey.

I will NEVER fly Economy class again !

I arrived at my hotel (The North Sydney Harbour View Hotel) 9pm Sunday. And guess what: it really has a harbour view!

Finish: Sunday 9pm Sydney

As my friends and colleagues will attest, I usually have something  to moan about following a trip. But so far (and this was the bit I was concerned about), I have to say that my day spent in the hands of Qantas has exceeded expectations !

The only possible complaint (and actually, it isn't) is that on the two occasions that I plugged in my laptop to try and do some work, I tripped a technical thing in the automated in flight entertainment system and cut all entertainment for half the passengers!

So to those people who had to cope with the best part of 30 hours without any form of entertainment other than a hand me down copy of the Independent newspaper, I am truly sorry !

Here is where I am right now:

I have just taken this from my window.








I picked this off the hotel's website. The room could be mine, but the dress isn't!





So I have a day to sort my body clock out, iron my shirts and do some last minute prep.
Nah ! I might just go walkabout !

Hold it. You'll never guess. Rolf Harris is playing the Sydney Opera House this week! Can you believe it?

Right if off to book !

I will tell you about my wife and son appearing on Animal Hospital another time.




Training?



In the meantime should you require any training in Silverlight, QA offer the following courses:

Extending Microsoft SharePoint Solutions with Silverlight 3


Developing Rich Internet Applications Using Microsoft Silverlight 4


Developing Silverlight Applications with Visual Studio 2010 and Expression Blend 4








See you soon

Phil Stirpé
"I don't do average!"


Monday, 17 November 2008

Deep Zoom Philip Stirpe

If it’s good enough for Barak Obama, it’s good enough for me.

I have been spending a lot of time with Silverlight 2.0 lately and the first run of our new course was a great success. For that first run, I had planned to show the delegates the DeepZoom Obama page in order to demonstrate DeepZoom . Unfortunately the course coincided with the RTW release of Silverlight 2.0.The site had been built with Silverlight 2.0 Beta and was unable to run.

I hoped that it would be updated in time for the RTW but I guess they were a little busy. So I spent an evening building my own.



DeepZoom allows you to create a composition of images using a tool called DeepZoom composer. Images can also be embedded within others. I took inspiration from Donavan West who created the DeepZoom Obama site. He had used a great piece of software from AndreaMosaic that can will create a mosaic from multiple image files based upon a single image. The result is brilliant. Further more, as far as DeepZoom composer is concerned, I only loaded one image into my project.

I wanted to make sure that I had embedded some additional high quality images into my mosaic and so I placed some in each eye, my bow tie and shirt button. Actually my right eye is a bit of a give away as I chose a bright image to embed there.

In fact I wanted to see how far I could push it. There are images within images within images. My 'Zoom is deepest' in my shirt button and reflects the depth of my love of Pink Floyd. Incidentally, should David Gilmour be reading this and would like a DeepZoom Gilmour just let me know. Dave, I'll do yours for free. Well perhaps a personal acoustic Comfortably Numb for my birthday might be a fair trade.

By the way, if you are the sort of person who gets excited when they find an easter egg in a computer game, try this: load up the Hard Rock Memorabilia site and select the Beatles from the side bar. This will rearrange the wall to only show the Beatles memorabilia. Now press V !

One more example of DeepZoom that I particularly like is this one by Vertigo. It has inspired me to create a functioning Site Map for my web site.

If you like a challenge, see if you can find this image. It is a high quality image of Porta Maggiore in Alatri and is nested within another high quality image.



 


Training?



In the meantime should you require any training in Silverlight, QA offer the following courses:


Extending Microsoft SharePoint Solutions with Silverlight 3



Developing Rich Internet Applications Using Microsoft Silverlight 4



Developing Silverlight Applications with Visual Studio 2010 and Expression Blend 4






See you soon

Phil Stirpé
"I don't do average!"


Wednesday, 17 September 2008

Another painting competition entry

On September 25th 2008, Compendium Games of Burnley (epicentre of the universe) held  their 2nd annual painting competition in association with Games Workshop.

Like last year, the Stirpe clan turned up in force.


Unlike last year, Alex won his section with a superb scratch built Chariot of Tzeentch model (on a nice base). Max was a runner up despite his superb diorama of a Skink Teradon rider attacking a skeleton.

Despite my best hopes (and efforts), I was thwarted once again. I had taken on board the judges point about last year's winner's hand painted banner and done the same. I even hand painted the shield.

Furthermore, I really went to town on the base. I cut up almost 30 plastic skeleton warriors to make it.
In the end I lost out again. This time it was suggested that I might have spent more time on the base. "Could do with a little more dry brushing".

Perhaps. Although I think I have figured it out, In both years the winner has used a model from the Warhammer 40000 (40K) rules whereas both of mine have been from Warhammer Fantasy. Perhaps a bias?

Of course, that's just sour grapes!

I have already decided what I am going to do for 2009. It is quite an ambitious diorama but I have 8 months to do it.

 



Baron le Tice "The Red Baron"


By the way, for those of you who know your Warhammer, Yes I know it's the Green Knight model but I had originally planned to use this figure and had already chosen my colour scheme before Games Workshop mail order let me down at the last minute.

I must admit that the competition is great fun. Not only has it encouraged my children to get involved with something other than PCs and console games, it has allowed me to spend a long time on a single piece rather than the usual production line of getting the next unit on the wargames table as soon as possible.

And Finally
I had thought of calling his trusty sword Silverlight, but that would just have been cheesy and cheap !







See you soon

Phil Stirpé
"I don't do average!"









Silverlight Flash Mob

Despite how it might occur in search engines, I am not suggesting any form of attack on Adobe Flash.

Rather, I am proposing a Flash Mob event at next week's ReMix in Brighton.

In my previous Silverlight post, I mentioned the excellent Silverlight Piano application.




I am sure that by now you have all had a go at Ode to Joy and hopefully you have been trying to work out the notes for your personal favourites. In which case you will have realised that not all of the notes are there.

Why else do you think I ended up with Ode to Joy and not Hotel California?

So here's the plan. At 1.30pm during the lunch break on Friday in the main hall, I propose that as many people as possible (with speakers on full) launch into Ode to Joy. It could be like the Last Night of the Proms !

What do you think ?

Whatever you decide, I have worked out a number of tunes for you to try. You might note a bias towards festive tunes. I worked them out to cheer myself up whilst hiding in my hotel room during a trip to eastern Europe last winter. Incidentally if you follow the previous link to the hotel, I assure you that my room looked nothing like any of those featured.

Furthermore, the food shown in the dining section must be 'stock'. I can still taste the Bistecca Boscaiola that I ordered that came topped with ketchup ! I am so NOT over that trip.

Anyway, moving on ...

Ode to Joy:

V V G H H G V D S S D V V D D,
V V G H H G V D S S D V D S S

Brick in the wall:

Z X D X, Z X D X, Z X D X Z X D

Deck the halls:

M N H G C G H C,
G H N G H G C D C,
G H N G,
H N M H,
M M M M M M M

Little donkey:

C V B C, V B N V, C V B C X

Jingle bells:

GGG GGG GNXCG,
BBBB BGGG NNBCX

Good King Wenceslas:

GGGH GGS DSDVGG


God save the queen:

DDVXDV BBHBVD VDXD










Training?

In the meantime should you require any training in Silverlight, QA offer the following courses: 

Extending Microsoft SharePoint Solutions with Silverlight 3


Developing Rich Internet Applications Using Microsoft Silverlight 4


Developing Silverlight Applications with Visual Studio 2010 and Expression Blend 4





See you soon

Phil Stirpé
"I don't do average!"



What is Silverlight?

Silverlight can enhance web applications by providing rich interactive experiences for users. Like Adobe Flash, Silverlight can offer interactivity and advanced animations. Provided that the user has installed the required plug-in, the Silverlight content will run within the browser.

Plugins are available for both the Mac and Windows and come in two versions: 1.0 and 2.0.

When a user visits a web page that has Silverlight content for the first time, they are prompted to install the Silverlight plug-in. The Silverlight application specifies the required version. The process is quick and simple, just a couple of clicks and the plug-in is installed. Once the plug-in is installed, that's it! The user will not be required to install it again.


Regardless of version, Silverlight applications exist within web applications and run, for example in a region of a web page.

The UI is described mainly in XAML (Extensible Application Markup Language), although it can also be generated with code at runtime.

XAML supports complex animations which can be achieved without the need to write any code.

The biggest difference between Silverlight versions 1.0 and 2.0 is the programming model. Silverlight 1.0 applications are programmed in Javascript whereas Silverlight 2.0 applications are written in a .NET compliant language such as C# or VB.NET. Silverlight 2.0 can also be programmed with the new dynamic languages such as Python and Ruby.



Silverlight 1.0

 

Silverlight 1.0 applications are programmed in Javascript and therefore comprise at least one .js file and one or more .XAML files to describe the UI.


These files reside with those of the parent web application. Alternatively they can be hosted for free by the Silverlight Streaming Service to cut down the cost of storage for your web application. I shall examine the Silverlight Streaming Service in a later post.


You will find many examples of Silverlight 1.0 applications here   www.silverlight.net/showcase/.


I think my favourite has to be the Silverlight Piano.






It does what you would imagine it does. You can either click on the keys or figure out what keystrokes press each note.

Look out for the reflection below the piano keys, particularly when you press a note.

Just to get you started, I have worked out the notes for Ode to Joy. Enjoy !

V V G H H G V D S S D V V D D
V V G H H G V D S S D V D S S

There are plenty of good examples of business applications including media players and interactive marketing applications but I will show them to you another time. The piano couldn't wait.

Silverlight 2.0


Silverlight 2.0 applications require another plug-in. This is to accommodate a version of the .NET Common Language Runtime (CLR). Although this plug-in is backwards compatible and can still execute Javascript for version 1.0 applications, it's strength is to execute CLR code such as C#.

As a result, Silverlight 2.0 are far more powerful than 1.0.

Unlike Silverlight 1.0 where .js files are hosted in addition to all .XAML content, Silverlight 2.0 code will initially be written in code files such as .CS for C#. These must then be compiled into a .DLL. The .DLL is then placed into a special ZIP file with an extension of .XAP (pronounced ZAP!) See what they did there?

This XAP file must be hosted with the parent web application (or Silverlight Streaming Service).

You will find many examples of Silverlight 2.0 applications here   www.silverlight.net/showcase/.

This airline booking example is a good example of a business use. Shame it's only an example, it is better than any flight booking system I have ever seen.



DeepZoom

One interesting feature of Silverlight 2.0 is DeepZoom. This allows you to create a 'wall' of images that a user can zoom into and scroll around. For speed, only those graphics in view are download, the remainder is downloaded as and when the user moves around the images.

A good place to see this in action in the Hard Rock cafe Memorabilia page.


Tools

It is important to distinguish between designers and developers at this point. It may be that one person alone is responsible for creating the UI and animations and then writing the code. However, in practice it is more likely to be a team effort.

For the designer whose deliverables are the graphical assets, the UI and animations, Expression Studio is the set of tools for them. Expression Studio comprises tools including Expression Design, Expression Blend and Expression Encoder. In a later post I will show how these tools can be used to create rich and compelling applications.

For the developer, the best tool for the job is Visual Studio 2008 is equipped to deal with Silverlight applications whereas Visual Studio 2005 will require a number of extensions before you can get to work. Once again, I will look at programming Silverlight applications in a later post.

Training?

In the meantime should you require any training in Silverlight, QA offer the following courses: 

Extending Microsoft SharePoint Solutions with Silverlight 3


Developing Rich Internet Applications Using Microsoft Silverlight 4


Developing Silverlight Applications with Visual Studio 2010 and Expression Blend 4



And finally

Xpertise will have a stand at ReMix in Brighton next week. So if you would like more information about any of our courses please stop by for a chat. We could talk about Silverlight, Visual Studio, Expression Studio or if you prefer, wargaming.





See you soon

Phil Stirpé
"I don't do average!"


Work with computers?

Last week my colleague Gary Duffield complained that once acquaintances realise that you “work with computers” they just can’t help asking your advice or worse, asking you to “take a look at ...”

After many years of the same, I have refined my answer.


Scene: Encounter

Phil: Enters kitchen at party.

Person: "Ah, so you're into IT !"

Person: Doesn't pause for breath. Launches straight into...

Person: "I'm having real problems with my dial-up / broadband / printer etc."

Phil: "No I'm not into IT. I'm into toy soldiers. I only work in IT".

Person: Tries to turn it around.

Phil: Takes bottle of Becks from fridge, smiles politely and leaves kitchen.


Incidentally, friends and family know not to ask.






See you soon

Phil Stirpé
"I don't do average!"


Sunday, 17 August 2008

Painting competition entry

On September 25th 2008, Compendium Games of Burnley (epicentre of the universe) will be holding their 2nd annual painting competition in association with Games Workshop.


Last year the Stirpe clan did well. Max (7) won the under 12s category 'The Scouts' with his Chaos Warrior.

Alex (12) didn't get placed in the 12 to 16 category 'Space Marines' with his Troll but this was by far the most populous category and he had a lot of competition. He did learn however, the value of making an effort with the basing.

I came second in the over 16s category 'The Veterans'. I had been confident of a win but the judges voted against me because the other guy had hand painted his figure's battle standard.

Well the gloves are off. I have finally settled on what I am going to paint this year but I can't say anything just yet as I don't want to give the opposition any inside information.

Here are some photographs of my 2007 entry.






 

Now you are probably wondering at this point, "when is he going to get around to drawing a parallel between his painting competition entry and something related to IT such as Silverlight or WCF ?".

Well sorry to disappoint you. I have nothing to say today about IT. It is Sunday you know!

Here's a thought. As I am primarily writing blog entries on behalf of my employer and I have managed to include technical references such as Windows Workflow and Visual Studio 2008 in order for this page to show up in search engines, I think it's only fair that I claim the cost of the wargames figures on expenses.

If that works, expect a posting on the renovation of a Massey Ferguson tractor next week.






See you soon

Phil Stirpé
"I don't do average!"


How easy is it to change the bindings of a WCF service from Http to tcp?

I have written a number of posts looking into the basics of Windows Communication Foundation - WCF. I have provided an overview of the technology (and wargaming), shown you how to create a simple WCF service and client. I have also looked into hosting services. I think it’s about time I covered bindings.




So I have written my flight service for QA Airways and it is called by client applications over the intranet.

In the short term we will only use the service on the intranet and the administrator feels that we would get better performance if we used tcp instead of Http.

Notice that I said administrator. I have moved on to other things. I am currently developing an Eco Cycle Grader © (the name needs a little work). It is a piece of equipment that helps me keep my sand arena free of weeds. Anyway that story is for another day.

Back to the administrator. It is out of the question for me to change the bindings as I am busy. It would also be impractical and probably foolish to permit the administrator to use Visual Studio 2008 to complete the task.

So instead we give them the Windows Service Config Editor (svcconfigeditor.exe). This is the same tool that can be launched from within Visual Studio 2008 and it is a more convenient way to edit config files.


Change the service bindings

The administrator runs svcconfigeditor.exe and opens up the App.config file for the service.


 


They change the binding of the service's endpoint to netTcpBinding.


 


And then change the base address to start with net.tcp rather than http.


 

Finally they save the config file as their work here is done.



Change the client bindings

In WCF it is essential that client bindings match those of the service they are calling. After all there would be no point using http to call into a service that only uses tcp.

So the administrator uses svcconfigeditor.exe to open up the App.config on each client in turn.

In a future post I will look at alternatives to having an administrator actually have to update multiple client config files!

They change the binding of the service's endpoint to netTcpBinding.



 


And then change the address to start with net.tcp rather than http.



Note that the configuration name (WSHttpBinding_IFlightService) was generated when the service's reference was added to the client application originally and was derived from the service's App.config. It would be better to have used a more generic name in the service's App.config file before discovery.

However it is doing no harm here and so we will leave the name alone.


Is that it?

Yep. That's it. Simply change the binding and address in each config file and the applications keep on working.

So, how easy is it to change the bindings for a WCF service?

Very!







Training ?
Should you require training in WCF we currently offer the following course: 

Building Effective Windows Communication Foundation applications using Visual Studio 2010

 




See you soon

Phil Stirpé
"I don't do average!"


How to host a Windows Communication Foundation (WCF) service

If you launch your service from within Visual Studio 2008 it is hosted by WcfSvcHost. However, when you deploy your service you will want something else to do the hosting.

For example, you could use a Windows application, a Windows service or IIS. For simplicity, I am going to host my service using a Console application.


Create the self hosting project

In Visual Studio 2008 I add a new Console Application called FlightServiceHosted to my solution (the FlightService solution from my earlier posting).



 


I then copy the following files from the FlightService project into the FlightServiceHosted project.




As I shall be using a self hosting service, I no longer need the FlightService project. And so I remove it from the solution.




As the files (App.config, IService1.cs and Service1.cs) contain references to a namespace called FlightService that no longer exists, I rename all instances of FlightService in these files to FlightServiceHosted.
Example:





I ensure that the FlightServiceHosted project contains a reference to System.ServiceModel.





In the Main method in Program.cs, I declare a variable of type ServiceHost and instantiate it with the type of my service (FlightServiceHosted.FlightService).




I complete the Main method to call both the Open and Close methods of my ServiceHost. To permit an administrator/user to start and stop the service I added WriteLine and ReadLine prompts.

Note: in a production environment you would be most unlikely to use a Console application. Rather, you might use IIS or a Windows service to host your WCF service.




 
The service is just about ready to go. However, I want to change the endpoint that was auto generated in my previous post when the FlightService was first created.
And so, I update the endpoints in both the App.config file in the FlightServiceHosted project and the App.config file of the WinClient project as follows:
FlightServiceHosted: App.config



WinClient: App.config



Time to test

That's it. My self-hosted service and it's client are good to go.
However, I shan't run them from within Visual Studio 2008 as I don't want the WCFScvHost to launch and I also want to illustrate their independence from Visual Studio 2008.
Firstly, I use Windows Explorer to locate and execute FlightServiceHosted.exe in the FlightServiceHosted/bin/debug folder.


Once the console application launches, I press Enter to start the service.



Now that my self-hosted service is now running, it is time to run the client application.
I use Windows Explorer to locate and execute WinClient.exe in the WinClient/bin/debug folder.



Once the client application launches, I enter two airports and invoke the service which returns the appropriate flight details.



That's it. How simple was that ?
As long as FlightServiceHosted.exe is executing and the service is running, the WinClient will be able to call it's methods.
When I'm done, all I need to do is close the client application and then press Enter once again within the console application in order to terminate the service.

What else ?

In my next post, I will show you how easy it is for you (or better still and administrator) to change a service's bindings.







Training ?
Should you require training in WCF we currently offer the following course: 

Building Effective Windows Communication Foundation applications using Visual Studio 2010

 




See you soon

Phil Stirpé
"I don't do average!"


Friday, 1 August 2008

How to write a simple Windows Communication Foundation - WCF service and client

In an earlier post, I provided an overview of Windows Communication Foundation (WCF) and likened it to wargaming. Well I have finally got around to write a series of posts detailing how to write and configure WCF services. Better late than never!


I needn't worry about how and where my service will be hosted, how clients will connect to it or indeed what protocol will be used. It doesn't matter, because at the moment I only want to think of my service in terms of its interface and implementation. i.e. What does it look like and what does it do?

I plan to write a flight information service that will be used by internal clients here at QA Airways. It is likely that our employees based at remote airports will need to access the service and so Http is likely as a protocol. In time we could share (and charge for) this service with business partners such as travel agencies.

Some form of authentication will no doubt be called for. However, as I have already said, I do not want to get bogged down in protocols and hosts. With WCF it needn't impact on how I write my service.


So I have decided to paint an army, ahem, write a WCF service.


Let's get started

Alright then, I'll simply dive in.

In Visual Studio 2008, there are several templates for WCF. For now, I will simply choose WCF Service library.





The resulting project contains a number of files that hold the key ingredients for my WCF service.

The file IService1.cs contains my interface (or Service Contract) called IService1. I shall be making some changes here so that the service contract defines methods that I require for my service.


 



The file Service1.cs contains the implementation of my service contract called Service1. Once my service contract has been updated, I shall make changes here to properly implement my service.


 



The file IService1.cs contains a class CompositeType which is a Data Contract. I have no need for a custom/complex type yet as I shall be using primitive types to pass data back and forth. So I will remove the entire CompositeType class.


 


The App.config file is perhaps the most important part of the project. This is where I will control what interface(s) will be used, where the service will be deployed and what protocol(s) it will use. However, it is too early for that.



 


The story so far

I have created a WCF Service project.

It comprises the following files:
  • IService1.cs
  • Service1.cs
  • App.config

IService1.cs contains an interface (Service Contract) IService1 and a class (Data Contract) CompositeType.

I deleted CompositeType.

Service1.cs contains a class Service1 that implements IService1.


Define the contract

Obviously, I don't want an interface called IService1 nor a service called Service1. So I use Edit / Find and Replace / Replace in files to rename every instance of IService1 to IFlightService and Service1 to FlightService.

In case you are wondering why I don't use Refactor to do the renaming, it is because Refactor does not 'do' config files and my interface and service class are referred to in App.config. I don't want to miss any!

I remove the GetDataUsingDataContract method from both IFlightService and FlightService.
Finally I replace the GetData method in both the interface and class with GetFlights as follows:


 


As you can see, the GetFlights method simply returns a string of contrived flight numbers. In fact we would probably query a data source at this point.

OK, the service is good to go. All we need to do now is test it before sitting down to write a client application.



Test the service

You'll like this bit!
In Visual Studio 2005, you would have to write a client application in order to test your service which was time consuming and delayed the 'inevitable' runs first time moment. However, in Visual Studio 2008 you are able to use the built in test client. WcfTestClient.exe is launched automatically whenever you run a WCF service.

This is thanks to a /client command line argument:




So in order to test my service I simply press F5.

The test client loads up and shows me that there is only one method and allows me to interactively invoke it with sample/test data.


 
It works!



 

One other point.

When you create web services, you rely on IIS to host your service for you. With WCF you are free to implement your own host. For example you could write a console application host, a Windows application host or possibly even a Windows service host. In fact you could even have IIS do it for you just like with web services.

None of this is necessary for now though because by default, WCF services are hosted by the built in WCF host WcfSvcHost when launched from within Visual Studio 2008.




What about the bindings ?

As I mentioned earlier, once I have written the service I want to leave decisions such as the transport to use up to the administrator. So the more that can be achieved with the App.config file, the better.

Let's look at some key elements.

Firstly there is the service name:




This is the name of the service class. It is declared within a services element.

Note that you can define more than one service in this section.

Each service may have more than one end point. For example, one for tcp clients and another for clients using http. These endpoints might share the same base address. So by default, a baseAddress section is added to the config file for each service class.


 

By default each service is given two end points. One that uses wsHTTP binding for all clients and another that uses Mex binding. The Mex binding endpoint is used by Visual Studio 2008 when adding a reference to a WCF service. The Mex endpoint permits discovery.





One last point of interest regarding config files is/are behaviours. These are named combinations of options/settings that you can assign to one or more services.

The behavior FlightServiceBahavior below is used by the FlightService service.


 



Write a client application

And finally ..

Time to write the client.

I add a new Windows Forms Application project called WinClient to the solutions and create a simple UI:



 

I now need to add code to my client that acts as a proxy to my service. To do that, I select Add Service Reference from the Solution Explorer. When I click Discover, my service is located thanks to the Mex binding endpoint defined in the config file.

All I do now is simply give my proxy class a better namespace e.g. Flights and click OK.



The first thing to do in my Search button is instantiate the proxy class.

 

This class FlightServiceClient represents the proxy for the FlightService class in the WCF service. This proxy will contain all the methods (just GetFlights for now) that exist in the actual service. However, they do not contain the same code. Rather, they contain the code necessary to call the methods remotely.
I now write the code to call my service and receive the result.


 

Finally, I set the StartUp Project to my WinClient project and hit F5 one last time.



I type in two airports, click Search and wait for the FlightService to return me a list of flight numbers.


Turn off discovery

Now that I have written my client application, I no longer need a Mex endpoint enabled. In fact it desirable to remove the Mex endpoint once your client is written so that your service cannot be 'discovered' by unscrupulous developers.





I remove the mex endpoint and change the httpGetEnabled property in my FlightServiceBehavior to False.


In conclusion

So there you have a very simple WCF service and client.

I have written a WCF service and in the process looked at Service Contracts, EndPoints in config files. Mex binding to allow discovery and the creation of a service proxy class.

In a future posting I will show how easy it is to change the bindings for a WCF service.









Training ?
Should you require training in WCF we currently offer the following course: 

Building Effective Windows Communication Foundation applications using Visual Studio 2010

 




See you soon

Phil Stirpé
"I don't do average!"