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
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:
Add a console application to the solution called Chapter7.WCFWebServiceHost.
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.
Now add a project reference to the Chapter7.WCFWebService project and a reference to the System.ServiceModel and System.ServiceModel.Web assemblies.
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();
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.
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>
3.147.195.136