Saturday, July 16, 2011

Trial and Error–WCF Services Part 2

As promised in my last WCF post, in this  post I’ll create the client that connects to the WCF service host that was created earlier.  Since the host is pretty simple at this point, so will the client and test be.

First things first, I need to create a project that’ll use the client – figured it would be easiest to do that in the same solution as the host.  So, I added a new project called ExpressRecipeServiceClient (technically this is poorly named as the client/proxy will be generated elsewhere).

Ok, so far so good – now to add the WCF reference, which in turn will generate the client/proxy code.  So, at the client project, I right click, go down to Add Service Reference.  Easy enough so far and is mostly the same as adding a project reference.  The Add Service Reference dialog comes up and I click Discover – thinking VS would see I have a WCF host.  Nope – nothing happens.  Ok, so I go back to the book and it says I need to manually start the host project without using debug…ok, try literally exactly that, start the host solution using Run, not Debug but I run into the problem of not being able to add any new references to any project that way.  I finally realize it means run it at the command line…so, I navigate over to the bin directory of the project and start the host there…host appears happy so I go back to VS and try to add the service through discover…still nothing.  So, I try making sure the service host is running by accessing the WSDL through IE – yep, works as it did.  I noticed on t e WSDL description that it says to use this URL to create a proxy – in my case, http://localhost:8000/UserService/?wsdl.  I put that in where it asks for address, then press go.  And, guess what, it appears to have worked.


image

Great…now, set the namepace something useful (ExpressRecipeService in my case) and then click ok (ignoring the advanced options – for now).  A new reference will appear under Service Reference with the namespace that was provided earlier.

Now for the test.  I still have the WCF host running from the command line.  In the newly created client project, I make a call similar to this

ExpressRecipeClient.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ExpressRecipeServiceClient
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             ExpressRecipeService.ExpressRecipeUserContractClient client = new ExpressRecipeService.ExpressRecipeUserContractClient();
  13.             client.AddUser("test1", "test2", "test3", "test4");
  14.         }
  15.     }
  16. }

Basically, create a new client for the host and call the only user-generated procedure, AddUser, with fake test parameters.  My expectation is that those will be printed out on the host console screen.  Guess it’s time to run, so F5.

Oops…”HTTP could not register URL http://+:8000/UserService/. Another application has already registered this URL with HTTP.SYS.”  Guess we should kill the console running host and try again.  Basically the error means something else is using that transport at that same port.

Kill the console host, then F5 again and here’s what I see from the host screen…well, nothing new – client never comes up but host does.  Gotta make the solution start multiple projects so set the host to start first, then set the client to start.  Run again and I get:

image

Ok, that wasn’t goo bad…going to need to do a bit more complicated things in the host but so far so good.

No comments:

Post a Comment