WCF

Web Services Software Factory (1 of 5)

Note: All links current as of the time of this blog post

This is the first in a series of five tutorial posts on the Web Services Software Factory. The Web Services Software Factory (WSSF) is a product that has come out of the Microsoft Patterns and Practices Group and its goal is to aid you in the mundane tasks that accompany setting up a robust WCF or ASMX service. The outline of the tutorials will be as follows:

Post 1: Download, Install, and Setup our Demo Project
Post 2: Create Data Contracts and Implement Business Entities and Translators
Post 3: Create Service Contracts and Implement the Methods
Post 4: Create Host Contract, Deploy and Test WSDL
Post 5: Generate Proxy, Create Simple Client, Call Service to Test

The original WSSF version had a lot of XML configuration to create contracts. With the version that came out in February (called the Modeling Edition), Microsoft Patterns and Practices added a lot of visual designers and even more code generation to the product. However, that is enough of the prelude, lets get into the example.

First you have to do some downloading and installing. You must first download and install GAX (Guidance Automation Extensions) 1.4. It can be found here. Installation is very simple and you just have to run the installer and accept the defaults to go on.

Next, you have to download the WSSF itself. Go here and download the product. If you are on Vista, you have to run the installer from an elevated command prompt. To do that you merely have to get the command prompt to show up in your start menu (either by searching or because you’ve saved it there as I have) and right click on it. Run As Administrator will be one of your options as shown below. After you have the prompt, execute the installer.
Get Elevated Command Prompt

You will then get the installation wizard and again you can accept the defaults and move on.
Install WSSF

Once you have installed the WSSF, start Visual Studio 2008 and click File–> New –> Project. Then select Guidance Packages, Service Factory: Modeling Edition, and then Model Project. Chose a directory and call the project MyCryptographyService and click OK. Note: we are going to do WCF, so make sure that you target Framework 3.0 at a minimum. I’m going to choose 3.5 just to run the “latest and greatest” 🙂
Create Modeling Edition Project

Rename the MyCryptographyService model project file to MyCryptographyService.model (so we can add a project of that same name later).
Rename Default Model

Right click on the solution and choose Add–> WCF Implementation Projects.
Add WCF Implementation Project Menu

This brings up the Add New Project dialog yet again. Name it MyCryptographyService (this is why we renamed earlier) and click OK.
Adding the new WCF Project

Visual Studio will grind away for a second and when it is done, it will have produced a boatload of projects for you in your solution. You will also notice that the projects that should reference each other already do, helping you along in making sure that you separate your code properly. Build your project and everything should build nicely. It doesn’t do much now, but more will come next time. If you have errors, please make sure that you followed the steps correctly.
Solution Explorer Populated with Projects

Next time we will dig into the designers and begin to implement the entities in our project.

Rant

Y Kant Developers Read?

Bookshelf image from flickr.com/photos/ianturton/2341264331/
I guess there are two major reasons that developers don’t read many books. This is kind of a timely subject for me right now on both fronts.

First, I began a new job at the beginning of last month. My new employer was running a Visual Fox Pro environment and wants to move to a full-blown .Net SOA Architecture. I was brought in to architect that. The team that I have (save one programmer that I recommended and was hired) are all Visual Fox Pro 6 developers who don’t know a stitch of .Net. With the exception of one guy (who I *really* appreciate), none of them seem to be very interested in reading books to find the answers to problems or to learn the gist of C# and ASP.Net.

This was also the case at my last job. With the exception of the guy who I recommended at my new place, no one would read books. They would take them at our insistence and then never read them. Someone once told me that developers read about one technical book a year on average. I couldn’t believe it when I heard it, but I’m starting to wonder if that number isn’t smaller. The first major reason just seems to be laziness and/or apathy.

The second major reason seems to be the driving force behind Jeff Atwood and Joel Spolsky‘s new venture, StackOverflow. According to Joel, “Programmers seem to have stopped reading books. The market for books on programming topics is minuscule compared to the number of working programmers. Instead, they happily program away, using trial-and-error. When they can’t figure something out, they type a question into Google.”

I believe that to certainly be true, and for people who are using new technologies or are on the bleeding edge, trial-and-error is the only way to go. However, if we are talking about anything that is approaching a year old, books certainly exist. My problem with trial-and-error and Google is that you never really *learn* anything. You are kind of limited to your own cleverness.

Advanced books not only teach you new features, they teach you new techniques. If you just trial-and-error, you will only try to figure out the Python way to do your C# code, or the Ruby way to do what you’ve always done in Java. You won’t learn what there is to learn about the new languages and the cultural mindset that comes with them. You could skip reading books and read lots and lots of source code, but I don’t believe a majority of developers are doing that, either.

However, so far we’ve only talked about learning a new programming language or technology. There is no way to effectively trial-and-error the kind of knowledge that you get from books like Code Complete, Pragmatic Programmer, GOF Design Patterns, or Practical Cryptography. How in the world has this style of learning, proven effective for centuries, fallen so far by the wayside? I am not trying to seem terribly elite, but I really read more than an entire technical book per month on average, along with several technical magazines and journals. I directly attribute my success in my field to that fact. Why can’t people see books as an avenue to their greater success?

Fluff

Great Quote From K

“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you’re as clever as you can be when you write it, how will you ever debug it?”
     – Brian Kernighan (the K of the essential K&R C book)

Code Tips

Anonymous Types in C# 3.0

For a long time, the var keyword has been the domain of only wholly dynamically typed languages. In fact, some people from the strongly-typed camp have considered the use of var as “hacky”, even though C#-ians have been able to treat everything as an object all along. Granted, it is not exactly the same and there is the whole boxing and unboxing thing to deal with, but I wanted to include full disclosure.

Now however, dynamic languages are all the rage and people are finding good uses for a generic variable type. To harness that power, one of the things done in C# 3.0 was the creation of Anonymous Types. Examine the code below and its output.

var blogPost = new
{
    Name = "Anonymous Types in C# 3.0",
    Author = "Pete Shearer",
    Category = "Code Tips"
}; 

Console.WriteLine(blogPost.Name);
Console.WriteLine(blogPost.Author);
Console.WriteLine(blogPost.Category);

Anonymous Types Output

By declaring my variable blogPost with the var keyword, I have created a dynamic variable. Now, after the new keyword and inside the curly braces, I declare a few properties and set their values. Behind the scenes, the compiler has declared a new object, given it a temporary name, and set the values in “traditional” fashion. It is even smart enough that if I declared another var with that definition, it wouldn’t redefine the anonymous type.

Within the anonymous type, the object remains strongly typed. Consider the following alteration to the code:

var blogPost = new
{
    Name = "Anonymous Types in C# 3.0",
    Author = "Pete Shearer",
    Category = "Code Tips",
    Date = new DateTime(2008, 3, 27)
}; 

Console.WriteLine(blogPost.Date.GetType());

Anonymous Types Output 2

The compiler inferred the type from the object placed into it. I know that I said new DateTime there, but it would have worked for any type that I put into that property. This new feature certainly affords developers a new level of flexibility. Enjoy.

Fluff

Why Do You Write Code?

Jeff Blankenburg posed an interesting question on his blog the other day. He asked, “Why do you write code?”. I started thinking about that question and decided that I not only wanted to answer that question on my blog, but that the answer to that question itself would provide a good synopsis about me for my readers.

The short answer is that it is really who I am. I’ve been fascinated with computers and technology for as long as I can remember. We got an Intellivision when I was very young and had the computer add-on when I was in kindergarten. I remember being able to pull sprites from the game cartridges and move them around the screen and writing rudimentary BASIC programs. The system had so little RAM that it had to have a modified version of BASIC; PRINT became PRIN to save a few bites.

I remember being enamored with what that computer represented. I could give instructions to a machine and it would do as I had asked. I loved programming on the computer because it shared two major themes with another love of my life, Mathematics. First of all, programming was about solving problems. I am at point A, I need to get to point B, what steps can I employ to make that happen? Secondly, computer programming was exact. Barring weirdness, a computer executed its instructions the same way given the same inputs the same way that 2 + 2 will always equal 4. There is something comforting in that.

After the Intellivision computer, we got a Commodore 64 and then a Commodore 128. Games Magazine used to publish BASIC programs for you to input into your computer. There were a lot of PEEK and POKE commands in the lines of code that I didn’t understand at the time, but I still felt like the world’s biggest hacker to make this code run.

As I grew up, I went through a variety of languages and ultimately ended up where I am now, writing C#.Net code. The platforms and the languages have changed, but what I do is still about taking a challenge and performing steps against it in order to reach my goal. 2 + 2 still always equals 4, though sometimes I really have to pay attention to make sure that the computer is getting 2 and 2 and not -87 and 1,348,849 😉 The computer isn’t very smart and does only exactly what it is told, which I guess sometimes makes me not very smart!

Even with that limitation, I feel extremely fortunate to have a career I love that remains challenging and fulfilling. There is nothing like the feeling when you’ve been given a difficult task and you are able to overcome it. That’s why I write code.