Dennis van der Stelt

The most votes generally drown out the best votes

Community

News

  • Meet me at PDC08

Email Notifications

I read...

I Use...

Tags

Recent Posts

Archives

April 2007 - Posts

Viva Las Vegas

So I arrived... Yesterday the plane landed in Las Vegas and the first thing that I saw were... slotmachines! This is Vegas of course.

Las Vegas Airport The Venetian - view from my window

I'm staying in The Venetian and the hotel is gorgeous. After publishing this, I'll start taking some pictures and publish them on Flickr. You can already see some pictures from my trip to Las Vegas.

This morning I attended an EMEA meeting where they gave away some info that wasn't really useful. They announced that the Expression sweet is officially released tomorrow, but that's not really unexpected, as every attendee gets its own copy. I'll probably be visiting some brainstorm session later today. I think I'll head to bed early, as I'm still suffering from not sleeping for over 24 hours and of course the jetlag.

For all Dutch readers, you can also see a report of my trip to MIX07 and Las Vegas at my personal weblog.

Technorati tags: ,
MIX07 Blogzone

Alice in wonderland, that's how I'm going to feel like in Vegas! Speaking about wonderland, via Alex I got a link to this post about the MIX07 Blogzone. In our hotel, The Venetian, on the third floor, there's going to be a room for bloggers with the following features:

  • Live streaming of all Mix content to jumbo widescreen plasma TVs
  • Free food
  • Free drinks
  • Free wireless
  • Gamezone – area dedicated to XBox 360s to relieve the stress of the 24 hour a day conference lifestyle
  • Video interviews
  • Opens at 8:00 AM.  Closes at ?? 

This will be an excellent place that's hopefully not too crowded and where we bloggers can meet up! So if you're going, I'll be there with a large TechEd bag behind Windows Live Writer blogging about MIX'07 ;-)

Technorati tags: , , ,
WCF Simple Example

On my WCF Introduction post I received a trackback to an example that should be really simple to start WCF with. I'm not here to judge the post (although I could ;-), but it got me thinking. Although I created some small posts on how WCF works, together it might still be too much for people that just want to see the simplest example. So this post is about that example.

There are two ways to present this. In this example I'll use as little code as possible in as few locations as possible. The other way is using the WCF Service Library project that comes with the VS2005 WCF Extension but requires a lot more code, text, etc. More about the in another post.

I'm not judging, but the other post by Ralf Sudelbücher has only one project containing both service, host and client. I'm using two projects, because WCF is all about getting clients to communicate with a service. Let's start.

  1. First, install Visual Studio 2005, .NET Framework 3.0 and the .NET Framework 3.0 WCF/WPF extensions.
    You don't need the SDK. And don't worry about the November 2006 CTP status, this is the latest version.
  2. Open Visual Studio 2005 and create a new console application project, called "Host". 
  3. Add a reference to the project for the System.ServiceModel assembly. It's the core assembly used by WCF.
    If you're using Visual Studio 2008, check out this article if you have problems with adding the service reference.

    add_reference
  4. Add a using statement at the top of your class for System.ServiceModel.
    using System.ServiceModel;
  5. Create a service contract and implement it in the Program.cs file, directly under your Program class. In this example I'm expecting a name and concatenate this to "Hello " and return this immediately.
    [ServiceContract]
    class HelloService
    {
      [OperationContract]
      string HelloWorld(string name)
      {
        return string.Format("Hello {0}", name);
      }
    }
  6. In your Program class, inside the static Main method we need code to host our service. We'll add two endpoints, one for our service and one for extracting the metadata.
        1 static void Main(string[] args)
        2 {
        3   // We did not separate contract from implementation.
        4   // Therefor service and contract are the same in this example.
        5   Type serviceType = typeof(HelloService);
        6 
        7   ServiceHost host = new ServiceHost(serviceType, new Uri[] { new Uri("http://localhost:8080/") } );
        8 
        9   // Add behavior for our MEX endpoint
       10   ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
       11   behavior.HttpGetEnabled = true;
       12   host.Description.Behaviors.Add(behavior);
       13 
       14   // Create basicHttpBinding endpoint at http://localhost:8080/HelloService/
       15   host.AddServiceEndpoint(serviceType, new BasicHttpBinding(), "HelloService");
       16   // Add MEX endpoint at http://localhost:8080/MEX/
       17   host.AddServiceEndpoint(typeof(IMetadataExchange), new BasicHttpBinding(), "MEX");
       18 
       19   host.Open();
       20 
       21   Console.WriteLine("Service is ready, press any key to terminate.");
       22   Console.ReadKey();
       23 }

    At line 7 we're instantiating our ServiceHost object, specifying a base address at localhost on port 8080.
    At line 15 we're adding our endpoint, using basicHttp, for our service.
    At line 17 we're adding our metadata endpoint. For this we also need to specifiy the MetadataExchange behavior, which we do at line 10, adding it to our ServiceHost at line 12. At line 11 we're enabling HttpGet, which means we can view the metadata using a browser.

    At line 19 we just open the host and everything should work fine. Lines 21 and 22 are waiting on confirmation of a user to close the console application, automatically closing our ServiceHost and thus our service.

    We only need to ad a using statement for the System.ServiceModel.Description because adding a MEX endpoint using code requires this namespace.
    using System.ServiceModel.Description;
  7. Now this is done, let's try and run our service. If all goes well, we'll see the message that the service is ready. You can check it by opening up a browser and visiting http://localhost:8080/
  8. Now we need to create our client. Add a new Windows Forms project called "Client" to our solution.
  9. Right-click the "Host" project and choose "Debug" and then "Start New Instance". Then right-click the "Client" project and select "Add Service Reference". In the dialog that appears type in the address http://localhost:8080/ and press "OK". Now you should have a new folder in your "Client" project with the reference to your service. Stop the service that's still running in the background.
    The System.ServiceModel assembly should be referenced now, the app.config has been added and modified as well as a proxy class under your "localhost" service reference.
  10. On your form in the "Client" project, add a textbox and a button. Double-click the button.
  11. Add three lines to your button-click event.
        1 private void button1_Click(object sender, EventArgs e)
        2 {
        3   localhost.HelloServiceClient proxy = new Client.localhost.HelloServiceClient();
        4   string result = proxy.HelloWorld(textBox1.Text);
        5 
        6   MessageBox.Show(result);
        7 }
  12. Again, right-click the "Host" project and select to start a new instance and do the same for your Windows application. Then fill in your name and press the button.

Congratulations, the most easy and fastest way to create a WCF service. Source-code can be downloaded here.
UPDATE : The zip file was broken, but is now fixed again.

MIX'07

I'll be attending MIX'07 in Las Vegas this year as well as the parallel MDEC conference for Mobile development. One of my goals for this year is to do more with ASP.NET so Alex isn't the only one with all the knowledge ;-)

Some fun things are:

  • Vegas! Need I say more?!
  • A lot on ASP.NET, Ajax and WPF/E. I haven't looked into Ajax and WPF/e much yet, so I should learn a lot here.
  • I'm particularly interested in the success stories behind really large websites.
  • Rumors that new stuff will be presented at MIX'07, for example still-secret "Technology X"
  • I'll stay at the Venetian, a great hotel with over 4000 rooms, 70 shops and multiple other facilities. Oh, and a casino as well ;-)
  • Windows Vista Ultimate and Expression Studio free for all registrants
  • ASP.NET and PHP head-to-head.
    I'll be attending this conversation as I've never looked at PHP and I'm interested on where the discussion will lead to. I'm hoping for both a religious discussion (for the fun) as a serious discussion on pros and cons of both.
  • Pussycat dolls at Pure? I'm might take a look, but I'm not sure I'm really into that...

I'll keep you informed right here. For all Dutch readers, I'll try and post some stories at my personal weblog.

Technorati tags: , ,