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.