Home > Articles > Home & Office Computing > Microsoft Windows Vista & Home Server

  • Print
  • + Share This
This chapter is from the book

Creating Operations for the Web

Creating operations for the Web means that we will want to expose services based on URIs, encode messages without the overhead of SOAP, pass parameters using the HTTP protocol, and format data using JSON or POX. WCF provides the WebHttpBinding binding that supports these capabilities. The WebHttpBinding binding is constructed using two binding elements. The first binding element is a new message encoder called WebMessageEncodingBindingElement. This is a new binding element that allows for the encoding of messages using either JSON or POX. The second binding element is a transport binding element based on either the HttpTransportBindingElement or HttpsTransportBindingElement. These binding elements enable communication using the HTTP protocol. The HttpsTransportBindingElement binding element is used to support transport-level security.

Hosting Using WebHttpBinding

To examine how to use the WebHttpBinding binding, we will create a simple Echo Web service. We are going to keep this example simple because we will expand on how to use this binding later on in this chapter. Listing 13.3 shows the IEchoService interface. This interface defines a service contract that has a single operation contract called Echo. Notice that the Echo operation contract is also attributed with the WebGet attribute. This attribute tells the webHttpBinding binding to expose this operation over the HTTP protocol using the GET verb.

Listing 13.3. IEchoService Interface

using System;
using System.ServiceModel;
using System.ServiceModel.Web;

[ServiceContract]
public interface IEchoService
{
    [OperationContract]
    [WebGet]
    string Echo(string echoThis);
}

Listing 13.4 shows the EchoService class that implements the IEchoService interface. This class implements the Echo operation by taking the echoThis parameter and returning it to the client.

Listing 13.4. EchoService Class

using System;
using System.ServiceModel;

public class EchoService : IEchoService
{
    #region IEchoService Members

    public string Echo(string echoThis)
    {

        return string.Format("You sent this '{0}'.", echoThis);
    }

    #endregion
}

The last thing needed is to host the EchoService service within IIS. Listing 13.5 shows the configuration file that allows us to host this service using the WebHttpBinding binding. The webHttpBinding configuration element exposes services using the WebHttpBinding binding. One important point is that the WebHttpBinding binding does not specify the format to expose services. Instead we need to use an endpoint behavior to specify the format returned from services exposed with the WebHttpBinding binding. Two endpoint behaviors can be used: WebHttpBehavior and WebScriptEnablingBehavior. The WebScriptEnablingBehavior behavior will be discussed in the section "Programming the Web with AJAX and JSON" later in this chapter. For now we will discuss the WebHttpBehavior behavior. The WebHttpBehavior endpoint behavior is used with the WebHttpBinding to format messages using either JSON or XML. The default for this behavior is to use XML.

Listing 13.5. EchoService Configuration

<system.serviceModel>
  <services>
    <service name="EchoService">
      <endpoint address=""
                behaviorConfiguration="WebBehavior"
                binding="webHttpBinding" contract="IEchoService"/>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="WebBehavior">
        <webHttp />
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

Figure 13.1 shows the output from the EchoService service when exposed over the WebHttpBinding binding. Because we exposed the service using the WebGet attribute, we can call the service by typing the URI in a browser. The URI that was used is http://localhost/SimpleWebService/EchoService.svc/Echo?echoThis=helloworld.

Figure 13.1

Figure 13.1 Response in browser using WebHttpBinding binding

  • + Share This
  • 🔖 Save To Your Account