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 lets check out how it’s done!
First, create a new WCF Service Library project and call it WcfZeroConfig:
Now, and this is 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 artictle 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:
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:
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: WcfZeroConfig.zip