Android访问WCF服务(上篇)服务端开发

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

 

本章目的: 用Wcf建立可以上Android可以访问的数据服务, 数据传输格式采用比较适合于移动互联网传输的Json格式.

服务的开发流程我们按照 服务契约(ServiceContract), 服务实现(Service), 实体对象模型(Model) 及服务发布的流程来介绍.

由于自己对Http请求的链接认识的比较浅,对于有些问题没法做出清楚明了的解释, Android访问WCF这篇文章我会贴出来代码, 让后说明一下关注的地方, 不做深入研究.

一. 服务契约(Contract)

[csharp] view plaincopy
  1. [ServiceContract]  
  2.     public interface IAccountJsonService  
  3.     {  
  4.         [OperationContract(Name = "GetAccountDataJson")]  
  5.         [WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetAccountData", BodyStyle = WebMessageBodyStyle.Bare)]  
  6.         List<Account> GetAccountData();  
  7.   
  8.         [OperationContract(Name = "SendMessageJson")]  
  9.         [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "SendMessage/{Message}", BodyStyle = WebMessageBodyStyle.Bare)]  
  10.         string SendMessage(string Message);  
  11.     }  


 

此契约定义了两个方法, GetAccountData(获取Account数据列表, 方法不带参数), SendMessage, 获取从客户端传过来的数据, 并返回;

1. 这里面注意WebInvoke(SendMessage方法)这个Attribute, Method代表了Http的访问方法, 我们这是从服务器获取数据,是请求数据, 所以用GET, 这个也可以用另外一个Attribute来替代-WebGet(GetAccountData方法);

2. 我们要给客户端返回Json数据,我们只需在WebInvoke or WebGet Attribute中指定ResponseFormat的格式即可, 这个从名字命名就可以看出来是制定返回的数据格式的.

3. 要注意UriTemplate属性, 这个是指定我们请求时的方法路径, 后面给出示例.

二. 服务实现(Service)

[csharp] view plaincopy
  1. public class AccountService : IAccountJsonService  
  2. {  
  3.     public List<Account> GetAccountData()  
  4.     {  
  5.         return MockAccount.AccountList;  
  6.     }  
  7.     public string SendMessage(string Message)  
  8.     {  
  9.         return " Message:" + Message;  
  10.     }  
  11. }  


 

此处只是实现了IAccountJsonService接口.

三. 实体对象模型&模拟数据

实体类定义:
[csharp] view plaincopy
  1. [DataContract]  
  2.     public class Account  
  3.     {  
  4.         [DataMember]  
  5.         public string Name { getset; }  
  6.         [DataMember]  
  7.         public int Age { getset; }  
  8.         [DataMember]  
  9.         public string Address { getset; }  
  10.         [DataMember]  
  11.         public DateTime Birthday { getset; }  
  12.     }  


模拟数据:

[csharp] view plaincopy
  1. public class MockAccount  
  2.    {  
  3.        public static List<Account> AccountList  
  4.        {  
  5.            get  
  6.            {  
  7.                var list = new List<Account>();  
  8.                list.Add(new Account { Name = "Bill Gates", Address = "YouYi East Road", Age = 56, Birthday = DateTime.Now });  
  9.                list.Add(new Account { Name = "Steve Paul Jobs", Address = "YouYi West Road", Age = 57, Birthday = DateTime.Now });  
  10.                list.Add(new Account { Name = "John D. Rockefeller", Address = "YouYi North Road", Age = 65, Birthday = DateTime.Now });  
  11.                return list;  
  12.            }  
  13.        }  
  14.    }  

模拟数据返回一个Account的列表, 含有三条模拟数据, Birthday用DateTime.Now可是随时查看数据是不是最新生成的.

四. 服务发布

在这个例子里面, 我们的服务采用Console的发布形式, 如果采用IIS发布, 只要参考WCF的服务配置信息, 在IIS环境下配置就OK了.

WCF配置信息

  1. <system.serviceModel>  
  2.   <behaviors>  
  3.     <serviceBehaviors>  
  4.       <behavior name="">  
  5.         <serviceMetadata httpGetUrl="mex" httpGetEnabled="true"/>  
  6.         <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>  
  7.       </behavior>  
  8.     </serviceBehaviors>  
  9.     <endpointBehaviors>  
  10.       <behavior name="WebHttpBindingBehavior">  
  11.         <webHttp/>  
  12.       </behavior>  
  13.     </endpointBehaviors>  
  14.   </behaviors>  
  15.   
  16.   <services>  
  17.     <service name="Hosting.AccountService">  
  18.       <endpoint address="xml" binding="webHttpBinding"  contract="Hosting.IAccountXmlService" behaviorConfiguration="WebHttpBindingBehavior"/>  
  19.       <!--<endpoint address="json" binding="webHttpBinding"  contract="Hosting.IAccountJsonService" behaviorConfiguration="WebHttpBindingBehavior"/>-->  
  20.       <host>  
  21.         <baseAddresses>  
  22.           <add baseAddress="http://127.0.0.1:82/AccountService"/>  
  23.         </baseAddresses>  
  24.       </host>  
  25.     </service>  
  26.   </services>  
  27. </system.serviceModel>  

控制台进行服务的托管发布

[csharp] view plaincopy
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            using (ServiceHost host = new ServiceHost(typeof(AccountService)))  
  6.            {  
  7.                host.Open();  
  8.                Console.WriteLine("AccountService Address:");  
  9.                foreach (var endpoint in host.Description.Endpoints)  
  10.                {  
  11.                    Console.WriteLine(endpoint.Address.ToString());  
  12.                }  
  13.                Console.WriteLine("AccountService Started,Press any key to stop service...");  
  14.                Console.ReadKey();  
  15.                host.Close();  
  16.            }  
  17.        }  
  18.    }  

下篇将介绍Android如何访问我们编写的服务.

原文地址:https://www.cnblogs.com/afly/p/2398178.html