A Brief Introduction to using the Web Programming Model of WCF to develop a rest-style Web Service

Source: Internet
Author: User

The Web Programming Model in WCF provides a rest-style Web service design function, which is different from the Web Service Based on soap or WS-* standards in the past, it is centered on Uri and HTTP. Each resource operated on has a unique identifier, and uses different HTTP actions (such as get, post, put, delete) to perform corresponding operations on these resources. At the same time, the model also provides a URI template, which is used to define a parameterized Uri, so that some parts of the URI are used as parameters in the service. This explanation may be vague. Here is a small example to illustrate this web programming model.

In this example, we use the northwind database,. NET Framework 3.5, And the development tool is vs2005.

First, we implement a simple requirement that we can obtain all the MERs. In this way, we define a customer's service contract, including the operation contract named getallcustomers.

  [ServiceContract]  public interface ICustomerService  {      [OperationContract]      [WebGet(UriTemplate = "")]      List<Customer> GetAllCustomers();  }

As you can see, this is the definition of a standard service contract in WCF. The webget attribute may not have been seen before. It indicates that the Service uses the get action to obtain data, uritemplate is an empty string, indicating that it does not match any parameters.

Next, we implement this service to read all data from the customer table in the database. Here, the customer is a data contract.

[Servicebehavior (instancecontextmode = instancecontextmode. single)] public class customerservice: icustomerservice {public list <customer> getallcustomers () {list <customer> customerlist = new list <customer> (); //......... obtain all the customers from the database
         return customerList;     } }

In this way, the service has been defined and implemented, and we should use the get method to access the service. Finally, the service needs to be released for external access.

 ICustomerService customerService = new CustomerService(); WebServiceHost customerServiceHost     = new WebServiceHost(customerService, new Uri("http://localhost:3333/")); customerServiceHost.AddServiceEndpoint(typeof (ICustomerService), new WebHttpBinding(), "customers"); customerServiceHost.Open();

Input http: // localhost: 3333/Mers MERs in the browser and return all customers described in XML format.

If you want to query a customer based on customerid, you need to add an operation contract in service contract.

[OperationContract][WebGet(UriTemplate = "{customerID}")]Customer GetCustomer(string customerID);

Customerid is a parameter. If the accessed URI is http: // localhost: 3333/customers/alfki, customerid matches alfki.

The operation contract is implemented. When we input http: // localhost: 3333/Mers MERs/alfki in the browser, the returned information of the customer alfki in XML format is returned.

 

This article describes how to use the Web Programming Model of WCF to develop a rest-style Web service. In the following article, we will introduce more relevant content, for example, complex uritemplate and post/Put/delete HTTP actions. For more information about rest, see this article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.