In the previous example – Self Hosting a WCF Service in a Console App – all the configuration of the service was done in code. Of course it’s much more practical to do this in a configuration file. So let’s move these setting by adding an App.config to our EchoSelfHostConsole project, right click on it and click ‘Edit WCF Configuration’. Add the basicHttpBinding and the servicebehaviour to enabled metatdata via a httpGet request.
The WCF Configuration editor filles the App.config with the settings after saving.
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="BehaviourMetaData"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="WcfServiceLibrary.Echo.EchoService" behaviorConfiguration="BehaviourMetaData"> <endpoint address="http://localhost:8080/EchoService" binding="basicHttpBinding" contract="WcfServiceLibrary.Echo.IEchoService" /> </service> </services> </system.serviceModel> </configuration>
After stripping all the WCF configuration code, we end up with a much cleaner implementation of the EchoService host.
using System; using System.ServiceModel; using WcfServiceLibrary.Echo; namespace EchoSelfHostConsole { internal class Program { private static void Main() { var host = new ServiceHost( typeof (EchoService), new Uri("http://localhost:8080/EchoService") ); try { host.Open(); Console.ReadLine(); host.Close(); } catch (Exception exception) { Console.WriteLine(exception.Message); host.Abort(); } } } }

