In recent projects, you need to write a service for Android and iOS calls. Now record the process. (First blog, diving for a long time ...)
WCF doesn't say much about specifics. On the steps and code ....
First create a new WCF project and create a new entity 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);
Implement the interface method, the code is as follows:
Private icollection<users> _listcollection = new List<users> () { new Users () { Id = "1", Gender=1,name= "Zhang Mowgli" }, new users () { id = "2", Gender=2,name= "Zhang Sanfeng" }, new users () { id = "3", Gender=1,name= "Ren ying" }, }; 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; }
Finally, add the following code to the configuration file:
<system.serviceModel> <bindings> <webHttpBinding> <binding name= "webbinding" Closetim eout= "00:40:00" receivetimeout= "00:40:00" sendtimeout= "00:40:00" maxbuffersize= "2147483647" maxReceivedMessageSize = "2147483647" usedefaultwebproxy= "false" > <security mode= "None" ></security> <readerqu OTAs maxdepth= "maxarraylength=" 2147483647 "maxstringcontentlength=" 2147483647 "maxbytesperread=" 2147483647 "/ > </binding> </webHttpBinding> </bindings> <services> <service name= " Wcfservicetest.service1 "behaviorconfiguration=" Wgjservicebehavior "> <endpoint address=" "BehaviorConfigurat ion= "Webbehavior" binding= "webhttpbinding" bindingconfiguration= "webbinding" contract= "WcfServiceTest.ISe Rvice1 "> </endpoint> </service> </services> <behaviors> <endpointbeha viors> <behavior name= "WebbehavIOR > <!--here must be set--<webhttp helpenabled= "true"/> </behavior> </ENDP ointbehaviors> <serviceBehaviors> <behavior name= "Wgjservicebehavior" > </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
Plus helpenabled= "true" and adding System.ComponentModel.Description on the interface can be added after the interface address Help Browse method description. Effects such as
Finally we create a new console application to test the interface.
Add the HTTP client library in the project to add the NuGet package:
Now start writing methods:
private static async void Getuserbyid (String id) {Console.WriteLine ("-----------Gets the 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"] = "big bro"; 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 ();//Error throws Exception Console.WriteLine (response. Content.readasstringasync (). Result); }
In the end, this time it's written here. The next time you upload a file using WCF rest ....
WCF rest service for Android and ISO call 1