Android访问WCF服务
时间:2011-12-27 23:31来源: 作者:admin 点击: 次
服务的开发流程我们按照
服务契约(ServiceContract), 服务实现(Service), 实体对象模型(Model)
及服务发布的流程来介绍.
由于自己对Http请求的链接认识的比较浅,对于有些问题没法做出清楚明了的解释,Android访问WCF这篇文章我会贴出来代码,
让后说明一下关注的地方, 不做深入研究.
一. 服务契约(Contract)view plain
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)view plain
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接口.
三. 实体对象模型&模拟数据实体类定义:view plain
1.
[DataContract]
2.
public class
Account
3.
由于自己对Http请求的链接认识的比较浅,对于有些问题没法做出清楚明了的解释,
一. 服务契约(Contract)view plain
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
此契约定义了两个方法, GetAccountData(获取Account数据列表, 方法不带参数), SendMessage, 获取从客户端传过来的数据, 并返回;
1. 这里面注意WebInvoke(SendMessage方法)这个Attribute, Method代表了Http的访问方法, 我们这是从服务器获取数据,是请求数据, 所以用GET, 这个也可以用另外一个Attribute来替代-WebGet(GetAccountData方法);
2. 我们要给客户端返回Json数据,我们只需在WebInvoke or WebGet Attribute中指定ResponseFormat的格式即可, 这个从名字命名就可以看出来是制定返回的数据格式的.
3. 要注意UriTemplate属性, 这个是指定我们请求时的方法路径, 后面给出示例.
二. 服务实现(Service)view plain
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
此处只是实现了IAccountJsonService接口.
三. 实体对象模型&模拟数据实体类定义:view plain
1.
2.
3.
