Services can be exposed using the WebHttpBinding binding using either the WebGet or WebInvoke attributes. Each of these attributes specifies the HTTP verb, message format, and body style needed to expose an operation. We will examine each of these attributes and reasons to use each.
The WebGet attribute exposes operations using the GET verb. The GET has significant advantages over other HTTP verbs. First, the endpoint is directly accessible via a Web browser by typing the URI to the service into the address bar. Parameters can be sent within the URI either as query string parameters or embedded in the URI. Second, clients and other downstream systems such as proxy servers can easily cache resources based on the cache policy for the service. Because of the caching capability, the WebGet attribute should be used only for retrieval.
using System;using System.ServiceModel;using System.ServiceModel.Web;
namespace EssentialWCF{ [ServiceContract] public class CustomerService { [OperationContract] [WebGet(UriTemplate = "/customer/{id}")] public Customer GetCustomer(int id) { Customer customer = null;
// Get customer from database
return customer; }
[OperationContract] [WebInvoke(Method = "PUT", UriTemplate = "/customer/{id}")] public void PutCustomer(int id, Customer customer) { // Put customer in database } [OperationContract] [WebInvoke(Method = "DELETE", UriTemplate = "/customer/{id}")] public void DeleteCustomer(int id) { // Put customer in database } }}