Android accesses the WCF Service (Part 1)-server development

Source: Internet
Author: User

 

(To) http://www.cnblogs.com/VinC/archive/2011/02/24/1964049.html

 

This chapter aims to use Wcf to establish a data service that can be accessed on Android. The data transmission format adopts the Json format suitable for mobile Internet transmission.

The Service development process is described according to the Service contract, Service implementation, object Model, and Service release process.

As I have a relatively low understanding of Http request links and cannot clearly explain some issues, I will post the code to Android to access the WCF article, so that I can describe the points of attention, no further research is required.

1. Contract)

[ServiceContract] <br/> public interface IAccountJsonService <br/> {<br/> [OperationContract (Name = "GetAccountDataJson")] <br/> [WebGet (RequestFormat = WebMessageFormat. json, ResponseFormat = WebMessageFormat. json, UriTemplate = "GetAccountData", BodyStyle = WebMessageBodyStyle. bare)] <br/> List <Account> GetAccountData (); </p> <p> [OperationContract (Name = "SendMessageJson")] <br/> [WebInvoke (Method = "GET", ResponseFormat = WebMessageFormat. json, UriTemplate = "SendMessage/{Message}", BodyStyle = WebMessageBodyStyle. bare)] <br/> string SendMessage (string Message); <br/>}< br/>

 

This contract defines two methods, GetAccountData (get the Account data list without parameters), SendMessage, get the data transmitted from the client, and return;

1. here, pay attention to the Attribute WebInvoke (SendMessage Method), which represents the Http access Method. We GET data from the server and request data, so we use GET, this can also be replaced by another Attribute-WebGet (GetAccountData method );

2. To return Json data to the client, you only need to specify the ResponseFormat in WebInvoke or WebGet Attribute. The returned data format can be determined by the name.

3. Pay attention to the UriTemplate attribute. This is the method path for specifying the request. The following is an example.

Ii. Service implementation)

Public class AccountService: IAccountJsonService <br/>{< br/> public List <Account> GetAccountData () <br/>{< br/> return MockAccount. accountList; <br/>}< br/> public string SendMessage (string Message) <br/>{< br/> return "Message:" + Message; <br/>}< br/>

 

Only the IAccountJsonService interface is implemented here.

3. Object Model & Simulation DataObject Class Definition:

[DataContract] <br/> public class Account <br/> {<br/> [DataMember] <br/> public string Name {get; set ;} <br/> [DataMember] <br/> public int Age {get; set ;}< br/> [DataMember] <br/> public string Address {get; set ;} <br/> [DataMember] <br/> public DateTime Birthday {get; set ;}< br/>}< br/>

Analog data:

Public class MockAccount <br/>{< br/> public static List <Account> AccountList <br/>{< br/> get <br/>{< br/> var list = new List <Account> (); <br/> list. add (new Account {Name = "Bill Gates", Address = "YouYi East Road", Age = 56, Birthday = DateTime. now}); <br/> list. add (new Account {Name = "Steve Paul Jobs", Address = "YouYi West Road", Age = 57, Birthday = DateTime. now}); <br/> list. add (new Account {Name = "John D. rockefeller ", Address =" YouYi North Road ", Age = 65, Birthday = DateTime. now}); <br/> return list; <br/>}< br/>

 

The simulated data returns an Account list, which contains three simulated data items. DateTime. Now is used for Birthday, but you can check whether the data is newly generated at any time.

 

4. Service Release

In this example, our service is published on the Console. If IIS is used for publishing, you only need to refer to the service configuration information of WCF and configure it in the IIS environment.

WCF configuration information

<System. serviceModel> <br/> <behaviors> <br/> <serviceBehaviors> <br/> <behavior name = ""> <br/> <serviceMetadata httpGetUrl = "mex" httpGetEnabled = "true"/> <br/> <serviceDebug httpHelpPageEnabled = "true" includeExceptionDetailInFaults = "true"/> <br/> </behavior> <br/> </serviceBehaviors> <br/> <endpointBehaviors> <br/> <behavior name = "WebHttpBindingBehavior"> <br/> <webHttp/> <br/> </behavior> <br/> </ EndpointBehaviors> <br/> </behaviors> </p> <services> <br/> <service name = "Hosting. accountService "> <br/> <endpoint address =" xml "binding =" webHttpBinding "contract =" Hosting. IAccountXmlService "behaviorConfiguration =" WebHttpBindingBehavior "/> <br/> <! -- <Endpoint address = "json" binding = "webHttpBinding" contract = "Hosting. IAccountJsonService "behaviorConfiguration =" WebHttpBindingBehavior "/> --> <br/> <post> <br/> <baseAddresses> <br/> <add baseAddress =" http: // 127.0.0.1: 82/AccountService "/> <br/> </baseAddresses> <br/> </post> <br/> </service> <br/> </services> <br /> </system. serviceModel> </p> <p>

 

Console for service hosting and Publishing

 

Class Program <br/>{< br/> static void Main (string [] args) <br/>{< br/> using (ServiceHost host = new ServiceHost (typeof (AccountService) <br/>{< br/> host. open (); <br/> Console. writeLine ("AccountService Address:"); <br/> foreach (var endpoint in host. description. endpoints) <br/>{< br/> Console. writeLine (endpoint. address. toString (); <br/>}< br/> Console. writeLine ("AccountService Started, Press any key to stop service... "); <br/> Console. readKey (); <br/> host. close (); <br/>}< br/>

Next, we will introduce how Android can access our services.

 

 

 

Related Article

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.