This sample demonstrates how to extend the Windows Communication Foundation (WCF) REST programming model to support new outgoing response formats. In addition, the sample uses a T4 Template to return the response as an XHTML page, demonstrating how a view-style programming model can be implemented.
The sample consists of a simple service along with client code that makes requests to the service. The service supports a single [WebGet] operation, which has the following method signature:
Message EchoListWithGet(string list);
When the client makes a request to the service, it provides a comma-separated list of items from the
list query-string parameter and the service returns that same list in one of the following formats: XML, JSON, Atom, XHTML, or jpeg.
The response format returned by the service is determined first by a
format query-string parameter and second by an HTTP Accept header supplied with the request. If the value of
format query-string parameter is one of the preceding formats, then the response is returned in that format. If the
format query-string is not present, then the service iterates through the Accept header elements from the request and returns the format of the first content-type that the service supports.
The return type of the operation is worth noting. The WCF REST programming model only natively supports XML and JSON response formats when an operation returns a type other than Message. However, when using Message as the return type, the developer has complete control over how the content of the message should be formatted.
The sample uses the CreateXmlResponse, CreateJsonResponse and CreateAtom10Response methods to serialize the list of strings into XML, JSON, and ATOM messages respectively. For the jpeg response format, the CreateStreamResponse method is used and the image is saved to the stream. For the XHTML response, the CreateTextResponse is used along with a preprocessed T4 template, which consists of a .tt file and an auto-generated .cs file. The .tt file allows a developer to write a response in a template form that contains variables and control structures. For more information about T4, see Generating Artifacts By Using Text Templates.
The sample consists of a self-hosted service and a client that runs within a console application. As the console application runs, the client makes requests to the service and writes the pertinent information from the responses to the console window.
To run this sample
Open the solution for the Advanced Format Selection Sample. When launching Visual Studio 2012, you should run as an administrator for the sample to execute successfully. Do this by right-clicking the Visual Studio 2012 icon and choosing Run as Administrator from the context menu.
Press CTRL+SHIFT+B to build the solution and then press Ctrl-F5 to run the console application AdvancedFormatSelection project without debugging. The console window appears and provides the URI of the running service and the URI of the HTML help page for the running service.
As the sample runs, the client sends requests to the service and writes the responses to the console window. Notice the different formats of the responses: XML, JSON, Atom, and XHTML.
You are then prompted to browse to a URI in which you can request the response in a .jpeg format. Open a browser and browse to the given URI.
Press any key to terminate the sample.
The samples may already be installed on your machine. Check for the following (default) directory before continuing.
If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.