Geeks With Blogs
Steve Loethen When I grow up I want to code in c#

First, let me correct my service implementation.  I must have missed my low caffeine light when I wrote the implementation.

The code should read

str = str.ToUpper();

and

str = str.ToLower();

to work.  Amazing how returning the string you sent offers little value.  Not that this particular service is of great value, but at least now it does what I intended.

What we have done so far...

So far we have a simple, 3 method web service, using basichttpbindings, sending strings in and out.  This should be very interopable, with a range like the standard asmx web services.  No security, http transport.  Pretty simple.  What we need to do next is to host it, then consume it.  First, let's talk about hosting.

Hosting

Now that we have our service contract and it's implementation, it is time to build it a house.  There are a number of hosting methods.  As a windows service, IIS, self hosted, Windows Activation Services (WAS) if you are running IIS 7.0. 

We are going to start simple and host via a console app.  This minimizes the need for a complex UI, gives us a good measure of control, and is easy.  I like easy.  Easy is my friend.

 Hosting is pretty simple. 

1) Add a console application project to our solution.  This gives us the core reference only, so we need to include our WCF reference.  Add System.ServiceModel as a reference.

2) Open the program.cs file of this new console app.  Go ahead and include the using statement for our System.ServiceModel reference.

3) Now, all the plumbing is in place, lets actually host the service.  I have my contract in a seperate assembly.  We could have made it part of this project, but I chose not to.  I feel it gives me some more options for hosting later.  Include the WCFContract assembly in our project.

4) Now, we create a instance of ServiceHost, tell it to construct itself as our WCFContract, and then add the endpoint.  Once the endpoint is added to the host, open it. 

5)  Serve up our Service. 

The code will look something like this.

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Linq;
using System.Text;

namespace WCFConsoleServer
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(WCFContract.myContractImplementation), new Uri("http://localhost:8000/WCFConsoleHostedService")))
            {
                host.AddServiceEndpoint(typeof(WCFContract.IMyContractInterface), new BasicHttpBinding(), "ConsoleService");

                host.Open();

                Console.WriteLine("Press <ENTER> to terminate the service host");
                Console.ReadLine();
            }
        }
    }
}

 

(btw, thanks for the tips on adding plug-ins to livewriter)

You notice we open but do not close the host.  I could put a

host.Close();

right after the Console.ReadLine(); to explicitly close the host, but we don't need to.  It will get called by the system as the program terminates.  But I am tempted to do so anyway.  I am a big fan of explicitness.  I guess it comes from a former life where it was harder to forget resources if you made sure you always did the disposal yourself.  Anyone out there want to comment on the pro's and con's of implicit vs. explicit in this case?  Until someone argues me out of it, my code will look this this:

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Linq;
using System.Text;

namespace WCFConsoleServer
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(WCFContract.myContractImplementation), new Uri("http://localhost:8000/WCFConsoleHostedService")))
            {
                host.AddServiceEndpoint(typeof(WCFContract.IMyContractInterface), new BasicHttpBinding(), "ConsoleService");

                host.Open();

                Console.WriteLine("Press <ENTER> to terminate the service host");
                Console.ReadLine();

                host.Close();
            }
        }
    }
}
I will be back later to build a client to talk to our service...
Posted on Wednesday, June 20, 2007 9:12 AM | Back to top


Comments on this post: Let's Host our service and build a client

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Steve Loethen | Powered by: GeeksWithBlogs.net