7.6. WCF Starter Kit Integration

Two great features previously seen in the WCF REST starter kit (www.asp.net/downloads/starter-kits/wcf-rest) have been brought into WCF4:

  • Help pages

  • HTTP caching

7.6.1. Help Pages

Help pages are automatically generated HTML pages that describe how to call your service and the type of response it will return, and can be very helpful for working out how to call the darn thing. Help pages are created automatically when you use the WebServiceHost class (although at the time of writing this doesn't seem to be the case) and when using the HelpEnabled property on WebHttpBehavior.

Let's take a look at this now with a contrived example:

  1. Create a new WCF service library project called Chapter7.WCFWebService.

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

  3. By default in .NET 4.0, some project types reference the .NET Client Profile framework, which is a smaller subsection of the main framework aimed at reducing your applications size. To demonstrate help pages, we need to use functionality not contained in the Client Profile framework, so right-click the Chapter7.WCFWebServiceHost project, select Properties on the context menu, and change the target framework to .NET Framework 4.0.

  4. Now add a project reference to the Chapter7.WCFWebService project and a reference to the System.ServiceModel and System.ServiceModel.Web assemblies.

  5. Enter the following code in Program.cs (Main() method):

    using System.ServiceModel.Web;
    
    ...
    
    WebServiceHost MyServiceHost =
     new WebServiceHost(typeof(Chapter7.WCFWebService.Service1),
               new Uri("http://localhost:8888/Test"));
    MyServiceHost.Open();
    Console.WriteLine("Service running...");
    Console.ReadLine();
    MyServiceHost.Close();

  6. Now open a browser and go to http://localhost:8888/Test/help, and you should see something similar to Figures 7-3 and 7-4.

Figure 7.3. Service help page

Figure 7.4. Service help page

7.6.2. HTTP Caching

One of the biggest advantages to using RESTful services is that you can take advantage of HTTP caching features to improve performance and reduce load on the service. Caching wasn't too easy to implement prior to WCF4; however, it is very easy in WCF4 with the simple addition of the AspNetCache profile attribute to your methods:

[AspNetCacheProfile("MyCachingProfile")]

You then need to create a caching profile using the following configuration:

<outputCacheSettings>
 <outputCacheProfiles>
  <add name=" MyCachingProfile " duration="30" varyByParam="format"
     varyByHeader="Accept" />
 </outputCacheProfiles>
</outputCacheSettings>

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

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