WCF series Restful WCF and wcf series restful

Source: Internet
Author: User

WCF series Restful WCF and wcf series restful

As the project requires, it is necessary to complete the interaction between the mobile terminal and the server in json format. Therefore, the Restful WCF-related content is studied to implement the ios terminal, android and browser can interact with background services.

First, let's take a look at what is a Restful WCF Service. Based on Restful, You can regard each url as a resource. You can call a url to obtain resource data and adjust the conditions for obtaining data through different parameters in the url. Therefore, restful services are more intuitive and easier to use.

Implementation of restful wcf

[ServiceContract]public interface IRESTFulWCF{        [OperationContract]        string GetList(QueryModel qm);}public class RESTFulWCF : IRESTFulWCF    {

/// Method Get, Post, Put, Delete
/// UriTemplate URI template. The caller builds a query url Based on the template.
/// RequestFormat/ResponseFormat Data Type of the request and response, xml or json
/// Whether the BodyStyle request and response data are at the end of the packet

[WebInvoke (Method = "POST", UriTemplate = "GetList", RequestFormat = WebMessageFormat. Json, ResponseFormat = WebMessageFormat. Json, BodyStyle = WebMessageBodyStyle. WrappedRequest)]

public string GetList(QueryModel qm)        {              return "{'state':'1'}";        }}        

After the service is published through the host, it can be called on the mobile end. The publishing address is http: // localhost: 18001/APPData.

Here I will list the methods for calling the restful service in ios, as follows:

NSString * json = @ "{\" qm \ ": {\" pageIndex \ ": 0, \" rowCount \ ": 10000 }}"; // This is the query condition for post to the server. The json format is [HttpHandler HttpPostWithUrlStr: @ "http: // localhost: 18001/APPData/GetList" Paramters: json FinishCallbackBlock: ^ (NSString * result) {NSMutableDictionary * di = [invalid dictionaryWithDictionary: [NSJSONSerialization JSONObjectWithData: [result dataUsingEncoding: Success] options: Unknown error: nil]; if (di) {NSLog (@ "% @", [di objectForKey: @ "state"]) ;}}];

According to our expectation, the ios client logs the returned values to the server. This method of directly calling the service through url is indeed convenient.

 

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.