Use the rest wcf Service through standard HTTP actions

Source: Internet
Author: User

 

The previous sections introduce some basic features of the rest wcf Service. This section describes how to use the rest wcf Service Based on HTTP standard actions. The architectural style of RESTful services is based on the HTTP protocol, and its design principles clearly indicate that resources are used through common connector interfaces. For restful services, four of the eight actions are used to use resources, namely GET, POST, PUT, and DELETE.
In RESTful services, the standard operations for GET, POST, PUT, and DELETE are as follows:
GET: GET Resources
POST: Modify Resources
PUT: create a resource
DELETE: deletes a resource.


The main points involved in this Section are as follows:
1. How to define interface specifications so that the client can use rest wcf resources through standard HTTP actions.
2. How does the client use resources through HTTP standard actions?
First, introduce the development environment:
VS2008 SP1. Because it is based on Framework3.5 platform, this section only describes some features of rest wcf 3.5. Ddmo structure:


1. Interface Specification definition:
Anyone who has some knowledge about the REST architecture style knows that in the System. ServiceModel. Web programming set, Microsoft provides us with two features: WebGetAttribute and WebInvokeAttribute.
The previous sections also gave a brief introduction. WebGetAttribute can be known by name that it is used to obtain resources, so the HTTP action it uses is GET. In rest wcf, several other actions are defined in WebInvokeAttribute.
In rest wcf, the usage of resources is defined through the UriTemplate attribute in WebGetAttribute and WebInvokeAttribute. In addition, you can use RequestFormat and ResponseFormat to define the data format used for client requests and the data format returned by the server to the client.
In this example, the interface specification and client usage specification are defined through the service contract definition. As shown in:


Note: In UriTemplate, {name}, similar to Student/{name}, is the placeholder of the required parameters in the defined method.
2. The client uses rest wcf.
2.1 use http get to get resources:

 

 

 

1 string getMsg = client. DownloadString ("http://www.bkjia.com: 6012/Student. svc/Student/zhangRest ");

2 Console. WriteLine (string. Format ("student information obtained by the get method: {0}", getMsg ));

  

 

2.2 Use the http post action to modify resources:

 

 

1 string postMsg = client. UploadString ("http://www.bkjia.com: 6012/Student. svc/Student/lizhi/0105245687", "POST ",

2 string. Empty );

3 Console. WriteLine (string. Format ("Student Information modified by the post method: {0}", postMsg ));

  

 

2.3 use the http delete action to DELETE resources:

 

 

1 string deleteMsg = client. UploadString ("http://www.bkjia.com: 6012/Student. svc/Student/zhangRest", "DELETE", string. Empty );

2 Console. WriteLine (string. Format ("student information deleted by the delete method: {0}", deleteMsg ));

  

 

2.4 use http put action to add resources:

 

 

1 string addMsg = client. UploadString ("http://www.bkjia.com: 6012/Student. svc/Student/zhangsan/0102356897", "PUT", string. Empty );

2 Console. WriteLine (string. Format ("student information added by the put method: {0}", addMsg ));

  

 

The usage of the four actions is shown in the preceding figure.

 

In rest wcf, URI (Uniform Resources Identifier) is defined by UriTemplate in the service contract, rather than using the method name directly in the service under the SOA architecture.

 

Author tyb1222

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.