One of the most frustrating aspects of WCF for me was the massive amount of configuration needed—it always seemed to be much harder to configure than it should be, especially when compared with the simplicity of creating an ASMX services. With great flexibility and power comes a great big XML configuration file.
WCF4 allows you to create a service with no configuration file at all in just a few lines of code. Let's do this now:
Add a console application to the solution called Chapter7.ConfiglessHost.
In Chapter7.ConfiglessHost, add a reference to Chapter7.ConfiglessService and the System.ServiceModel assembly.
Enter the following code in Program.cs's Main() method to instantiate your service:
ServiceHost MyServiceHost = new ServiceHost(typeof(Chapter7.ConfiglessService.Service1), new Uri("http://localhost:8888/Chapter7")); MyServiceHost.Open(); Console.WriteLine("Service running..."); Console.ReadLine(); MyServiceHost.Close();
Now right-click Chapter7.ConfiglessHost in Solution Explorer and set it as the startup project, and then press F5 to run the application. You should now have a running service without any configuration file.
You can verify this by browsing to http://localhost:8888/Chapter7, where you will find your running service (Figure 7-1).
WCF4 defaults to a number of commonly used settings, saving you from having to configure them yourself. Of course, should you not like the defaults, you have the flexibility to override them or not use them at all. Let's look at this default configuration now.
52.15.239.254