The wcf rest service is used for Android and ISO call 1.

Source: Internet
Author: User

The wcf rest service is used for Android and ISO call 1.

A service needs to be compiled in the recent project for Android and iOS to call. Now record the process. (the first time I wrote a blog, I 've been diving for a long time ......)

I will not talk much about the specific content of wcf. the above steps and code ....

Create a new wcf project and create an object class. The Code is as follows:

  

  [DataContract]    public class Users    {        [DataMember]        public string Id { get; set; }        [DataMember]        public string Name { get; set; }        [DataMember]        public int Gender { get; set; }    }

Then write some get and post methods in the interface.

[OperationContract] [WebGet (UriTemplate = "GetUsers", ResponseFormat = WebMessageFormat. json)] [System. componentModel. description ("Get all Users")] ICollection <Users> GetUsers (); [OperationContract] [WebGet (UriTemplate = "GetUserById/{id}", ResponseFormat = WebMessageFormat. json)] [System. componentModel. description ("Get user by id")] Users GetUserById (string id); [OperationContract] [WebInvoke (Method = "POST", UriTemplate = "AddUser", ResponseFormat = WebMessageFormat. json, BodyStyle = WebMessageBodyStyle. bare)] [System. componentModel. description ("Add User")] Users AddUser (Users news );

The code for implementing the interface is as follows:

Private ICollection <Users> _ listCollection = new List <Users> () {new Users () {Id = "1", Gender = 1, Name = "Zhang Wuji "}, new Users () {Id = "2", Gender = 2, Name = "Zhang Sanfeng"}, new Users () {Id = "3", Gender = 1, name = "" },}; public ICollection <Users> GetUsers () {return _ listCollection;} public Users GetUserById (string id) {return _ listCollection. firstOrDefault (v => v. id = id);} public Users AddUser (Users news) {_ listCollection. add (news); return news ;}

Add the following code to the configuration file:

<System. serviceModel> <bindings> <webHttpBinding> <binding name = "webBinding" closeTimeout = "00:40:00" receiveTimeout = "00:40:00" sendTimeout = "00:40:00" maxBufferSize = "2147483647" maxcompute edmessagesize = "2147483647" usedefawebwebproxy = "false"> <security mode = "None"> </security> <readerQuotas maxDepth = "32" maxArrayLength = "2147483647" maxStringContentLength = "2147483647" maxBytesPerRead = "2147483647" /> </B Inding> </webHttpBinding> </bindings> <services> <service name = "WcfServiceTest. service1 "behaviorConfiguration =" wgjServiceBehavior "> <endpoint address =" "behaviorConfiguration =" webBehavior "binding =" webHttpBinding "bindingConfiguration =" webBinding "contract =" WcfServiceTest. IService1 "> </endpoint> </service> </services> <behaviors> <endpointBehaviors> <behavior name =" webBehavior "> <! -- Here you must set --> <webHttp helpEnabled = "true"/> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name = "wgjServiceBehavior"> </behavior> </ serviceBehaviors> </behaviors> </system. serviceModel>

Add helpEnabled = "true" and add System. ComponentModel. Description to the interface address, add the/help browsing method Description. The effect is as follows:

 

  

 

Finally, we create a console application to test the interface.

Add the Http Client library to the NuGet package in the project:

Now let's start writing the method:

Private static async void GetUserById (string id) {Console. writeLine ("----------- get user ---------- by id"); HttpClient httpClient = new HttpClient (); var result = await httpClient. getStringAsync (" http://localhost:31572/Service1.svc/getUserById/1 "); Console. writeLine (result);} private static async void GetUsers () {Console. writeLine ("----------- get all users ----------"); HttpClient httpClient = new HttpClient (); var result = await httpClient. getStringAsync (" http://localhost:31572/Service1.svc/GetUsers "); Console. writeLine (result);} public static async void AddUser () {Console. writeLine ("------------ Add User ---------------"); HttpClient httpClient = new HttpClient (); var jobj = new JObject (); jobj ["Id"] = "4 "; jobj ["Name"] = ""; jobj ["Gender"] = 1; string str = jobj. toString (); HttpContent content = new StringContent (str); content. headers. contentType = new MediaTypeHeaderValue ("application/json"); var response = await httpClient. postAsync (" http://localhost:31572/Service1.svc/AddUser ", Content); response. EnsureSuccessStatusCode (); // exception caused by Console. WriteLine (response. Content. ReadAsStringAsync (). Result );}

Finally, I will write it here this time. Next time I will use wcf rest to upload files ....

 

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.