Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
10 Mar 2011 1  
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:

new-project

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:

Console-project

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:

servicemodel-ref

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

Add-reference

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

C#
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"));

            host.Open();

            Console.WriteLine
		("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...");
            Console.ReadKey();
        }
    }
}

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.

wcfhost


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:


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:

C#
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();
            Console.WriteLine();
            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...");
            Console.ReadKey();
        }

        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
            ((IClientChannel)wcfClient1).Close();

            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.

WcfClient-result

The full source code is available here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here