7.1. Configless WCF

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:

  1. Create a new WCF Service Library project called Chapter7.ConfiglessService.

  2. Add a console application to the solution called Chapter7.ConfiglessHost.

  3. In Chapter7.ConfiglessHost, add a reference to Chapter7.ConfiglessService and the System.ServiceModel assembly.

  4. In Chapter7.ConfiglessHost Program.cs, add the following using directive:

    using System.ServiceModel;

  5. 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();

  6. 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).

Figure 7.1. Configless service

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.239.254