Click here to Skip to main content
12,700,567 members (33,032 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked

WCF Services with Zero Config (on both the Host and Client)

, 10 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
WCF Services with Zero Config (on both the Host and Client)

When dealing with your WCF services, have you ever gotten so confused with all the settings in your App.Config that you just wanted to throw the file away? Well now you can, and I’ll show you how.

With .NET 4.0, WCF allows developers to take the convention over configuration approach to their services. This means that by omitting configuration settings, the .NET Framework will create defaults in their place and make your services fully available and operational.

So let's check out how it’s done!

First, create a new WCF Service Library project and call it WcfZeroConfig:


Now, and this is the good bit, delete the App.Config file. Just right click on the file, select Delete – feels good right?!

The WCF Service Library project template makes two methods available for us by default and for this article, we’ll be using the GetData(int value) method for our example.

As our WCF library is now complete, we are going to add another project to our solution to host the service. In production, you would usually use IIS or a Windows Service to host your WCF Service Library, but for this simple example we are going to use a Console application.

From the Visual Studio main menu, select File > Add > New Project > Windows > Console Application and name the project WcfHost:


As this project will be acting as the host, we’ll need to add references to both the System.ServiceModel assembly and our WCF Service Library project. Right click on the WcfHost Project file, select Add Reference and select System.ServiceModel framework:


Now select Projects (in the top left hand corner) and double click the WcfZeroConfig and press the close button.


To complete the host, copy and paste the following code into the Program.cs file:

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WcfHost
    class Program
        static void Main(string[] args)
            var host = new ServiceHost(typeof(WcfZeroConfig.Service1), 
			new Uri("http://localhost:8732/WcfZeroConfig"));


		("Our service is currently running with the following config:");
            foreach (ServiceEndpoint se in host.Description.Endpoints)
                Console.WriteLine("A: {0}, B: {1}, C: {2}", 
		se.Address, se.Binding.Name, se.Contract.Name);

            Console.WriteLine("Press any key to end the service...");

As you can see, it takes one line of code to create the ServiceHost object, passing in an Endpoint address and a single method call to open the host channel to make the service available.

On startup of the host, under the covers, WCF (in .NET 4.0) will detect that no Binding or Contract has been supplied, either in our App.Config file or from our code, and will provide default values. From the Endpoint address, the framework can see we are using HTTP and will provide the default binding of basicHttpBinding. The contract type of IService1 will also be inferred from the Service1 concrete class.

To run the host, select the WcfHost project and press F5 to build and run the application.


We are now ready to create the client application to consume our WCF Service. From the Visual Studio main menu, select File > Add > New Project > Windows > Console Application and name the project WcfClient:


As before, you’ll need to add the System.ServiceModel framework (right click on the project file > Add Reference…) along with the WcfZeroConfig project, this is so that the WcfClient project is aware of the WCF Service contract type “IService1”.

To complete our client, just paste in the following lines of code into the Program.cs file:

using System;
using System.ServiceModel;
using WcfZeroConfig;

namespace WcfClient
    class Program
        static void Main(string[] args)
            Console.WriteLine("To call the service, enter a number:");
            ConsoleKeyInfo consoleKeyInfo = Console.ReadKey();
            int input = Convert.ToInt16(consoleKeyInfo.KeyChar - 48);
            Console.WriteLine("Calling service with value {0}", input);
            var result = CallService(input); Console.WriteLine("Response from service: {0}", result);
            Console.WriteLine("Press any key to close the application...");

        private static string CallService(int value)
            // setup the client A, B, C (address, binding and contract)
            var address = new EndpointAddress("http://localhost:8732/WcfZeroConfig");
            var binding = new BasicHttpBinding();
            var channelFactory = new ChannelFactory<IService1>(binding, address);
            IService1 wcfClient1 = channelFactory.CreateChannel();

            // call the service
            string result = wcfClient1.GetData(value);
            // close the client channel

            return result;

To complete our example:

  1. You’ll first need to build the solution by right clicking on the solution file and selecting Build Solution.
  2. Run the C:\Projects\WcfZeroConfig\WcfHost\bin\Debug\WcfHost.exe file.
  3. And finally, run the C:\Projects\WcfZeroConfig\WcfClient\bin\Debug\WcfClient.exe file and enter a number.

That number then gets passed to our WCF Service and our client receives the result.


The full source code is available here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United Kingdom United Kingdom
Developer of .Net Enterprise solutions in the City of London

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
JV99999-Mar-11 21:10
memberJV99999-Mar-11 21:10 
GeneralRe: My vote of 2 [modified] Pin
StevenHollidge10-Mar-11 1:18
memberStevenHollidge10-Mar-11 1:18 
GeneralRe: My vote of 2 Pin
JV999922-Mar-11 1:06
memberJV999922-Mar-11 1:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 10 Mar 2011
Article Copyright 2011 by StevenHollidge
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid