Basic HTTP Service
This sample demonstrates how to implement an HTTP-based, RPC-based service - popularly referred to as "POX" (Plain Old XML) service – using the Windows Communication Foundation (WCF) REST Programming model. This sample consists of two components: a self-hosted WCF HTTP service (Service.cs) and a console application (Program.cs) that creates the service and makes calls to it.
The WCF service exposes 2 operations,
EchoWithPost, which returns the string that was passed as input.
EchoWithGet operation is annotated with WebGetAttribute, which indicates that the operation processes HTTP
GET requests. Because the WebGetAttribute does not explicitly specify a UriTemplate, the operation expects the input string to be passed in using a query string parameter with name
s. Note that the format of the URI that the service expects can be customized using the UriTemplate property.
EchoWithPost operation is annotated with WebInvokeAttribute, which indicates it is not a
GET operation (it has side effects). Because the WebInvokeAttribute does not explicitly specify a
Method, the operation processes HTTP
POST requests that have the string in the request body (in the XML format, for instance). Note that the HTTP method and the format of the URI for the request can be customized using the Method and UriTemplate properties respectively.
The App.config file configures the WCF service with a default WebHttpEndpoint that has the HelpEnabled property set to
true. As a result, the WCF infrastructure creates an automatic HTML based help page at
http://localhost:8000/Customers/help that provides information about how to construct HTTP requests to the service and how to consume the service’s HTTP response.
Program.cs demonstrates how a WCF channel factory can be used to make calls to the service and process responses. Note that this is just one way to access a WCF service. It is also possible to access the service using other .NET Framework classes like HttpWebRequest and WebClient. Other samples in the SDK (such as the Automatic Format Selection sample and Basic Resource Service sample) show how to use these classes to communicate with a WCF service.
The sample consists a self-hosted service and a client that both run 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 use this sample
Open the solution for the Basic Http Service Sample. When launching Visual Studio 2012, you must run as an administrator for the sample to execute successfully. Do this by right-clicking the Visual Studio 2012 icon and selecting 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 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. At any point in time you can view the HTML help page by typing the URI of the help page in a browser. As the sample runs, the client writes the status of the current activity.
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.