Basic Web Programming Model Sample

Download sample

This sample demonstrates the basic elements of the Windows Communication Foundation (WCF) Web programming model extensions by implementing a service that can be called using HTTP GET and POST. This sample is implemented as a single process that hosts the service and implements a client. The client calls the service using HTTP.

Note

This sample requires that .NET Framework version 3.5 is installed to build and run. Visual Studio 2008 is required to open the project and solution files.

Assemblies and Namespaces

The WCF Web programming model is implemented in the System.ServiceModel.Web assembly included in this SDK. This assembly is derived from System.ServiceModel, which is installed as part of the .NET Framework 3.0.

The Program.cs file, (which contains both the service and the client), references the following important namespaces:

Service

The sample service exposes a contract with two operations: EchoWithGet and EchoWithPost, as shown in the following code.

[ServiceContract]
public interface IService
{
   [OperationContract]
   [WebGet]
   string EchoWithGet(string s);

   [OperationContract]
   [WebInvoke]
   string EchoWithPost(string s);
}

As with other WCF services, the ServiceContractAttribute attribute is applied to the IService contract and OperationContractAttribute is applied to its methods. This service contract makes use of two new Web programming model attributes: WebGetAttribute and WebInvokeAttribute. These attributes indicate how the contract’s operations should be made available over HTTP.

The WebGetAttribute attribute is applied to the EchoWithGet method, which indicates that the method should be made available at a URI that responds to the HTTP GET method (for example, https://localhost:8000/EchoWithGet?s=Hello, world!). WCF clients as well as Web browsers can use this URI. The WebInvokeAttribute is applied to the EchoWithPost method, which indicates that the operation should be made available at a URI that responds to the HTTP POST method. Parameters to the method are encoded as XML in the body of the POST request. This URI can be used by WCF clients as well as Web clients that can create HTTP POST requests with XML bodies (for example, the browser XmlHttpRequest object). For more information about the Web programming model, see the Advanced Web Programming sample in this SDK.

The following endpoint for the service is explicitly defined.

using (WebServiceHost host = new WebServiceHost(typeof(Service), baseAddress))
{
    host.Open();

    ...
}

The service is hosted using the WebServiceHost type, which is a new derivative of ServiceHost that simplifies hosting services that use the Web programming model extensions. WebServiceHost automatically creates a default endpoint at the base address using WebHttpBinding and WebHttpBehavior, so there is no need to define the endpoint explicitly. This type is a new standard binding for communicating directly over HTTP without SOAP. This binding supports XML messages as well as other types of non-XML content (such as plain text, images, or other byte streams).

Client

This sample also illustrates how you can use the Web programming model on the client to send HTTP requests.

The following client channel is created using a WebChannelFactory.

using (WebChannelFactory<IService> cf = new WebChannelFactory<IService>(baseAddress))
{
    IService channel = cf.CreateChannel(); 
    ...
}

Similar to WebServiceHost, WebChannelFactory creates a default endpoint at the base address using WebHttpBinding and WebHttpBehavior.

After WebChannelFactory has been created, the client application creates a channel to the service and calls EchoWithGet followed by EchoWithPost.

When the interaction is complete, the program waits for a key press. During this period, the service is still actively listening. When a key is pressed, both the client and the server shut down and the program terminates.

When you run the service, the following output is displayed.

Calling EchoWithGet via HTTP GET:

Output: You said Hello, world

The same output can be generated by navigating to "https://localhost:8000/EchoWithGet?s=Hello, world!" in a Web browser while this sample is running.

Calling EchoWithPost via HTTP POST:

Output: You said Hello, world

Press any key to terminate

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

See Also

Other Resources

Advanced Web Programming

© 2007 Microsoft Corporation. All rights reserved.