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

Okay, that was a joke.  I like writing code.  I am not trying to get rid of coders.  Okay, trust me?

One of the interesting features of WCF is the ability to do the Address and Binding portion of our 3 legged stool administratively.  In a xml file called app.config or web.config.  This allows some interesting possibilities of changing the service endpoint without changing code.  Being able to take a project from development to QA to production (assuming all is well with it) without a recompile to redirect the address is pretty handy.  And the ability to add an additional endpoint, at a different address, with a different binding, on the same service has even more advantages.

I decided to keep my original project and just add a second server to the solution.  This one is just a mirror copy of the first initially.  I called it WCFConsoleServerCfg. 

If we were to try and run both now, we would get a exception, due to the fact that both would try and grab port 8000.  So we will need change that.

Once again, let's take the easy way first.  The goal is to create a service that does exactly the same thing as our previous one, at a different address, but use the app.config to set up our service. 

First, let's visit the code required...

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

namespace WCFConsoleServerCfg
    class Program
        static void Main(string[] args)
            ServiceHost host = new ServiceHost(typeof(WCFContract.myContractImplementation));


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


Hopefully you noticed that there is less code in the Main.  And the constructor for the ServiceHost uses less parameters.  What is missing is more important in this context than what is there. 

First, there is not specification or create of a EndpointAddress.  No port number, no URI...   Second, we are not telling the ServiceHost what kind of binding to use.  Those settings were put in our app.config file.  Let's wander over there.

Here is what my app.config looks like:

<?xml version="1.0" encoding="utf-8" ?>
      <service behaviorConfiguration="basicService" name="WCFContract.myContractImplementation">
        <endpoint address="ConsoleService" binding="basicHttpBinding" name="ConsoleService" contract="WCFContract.IMyContractInterface"/>
            <add baseAddress="http://localhost:8001/WCFConsoleHostedService"/>
        <behavior name="basicService">


As you can see, we have defined the endpoint and the binding here in this config file.  The name we gave it was WCFContract.myContractImplementation.  Our ServiceHost constructor was fed this name, and it came here to get the info.  We gave the endpoint a address of "ConsoleService", which it combines with the baseaddress of http://localhost:8001/WCFConsoleHostedService.  We told it what interface to use.  We also have added a behavior, called "basicService", but haven't added any behaviors to it yet. That is a place holder.


Now, how do we get our client to talk to this service?  Simply change the address in our Client to reflect 8001 instead of port 8000.  In our next chapter we will retool the client to use it's app.cofig to get to the service.

Technorati Tags: , ,
Posted on Thursday, June 21, 2007 2:16 PM VS.Net and development | Back to top

Comments on this post: Why write code when we can specify our services administratively in XML?

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

Copyright © Steve Loethen | Powered by: