接着,WCF服务端首先是契约定义:
[OperationContract] [WebInvoke(UriTemplate = "GetPersonDetail" , Method = "POST" , BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] Stream GetPersonDetail(Stream req); |
WCF方法的实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public Stream GetPersonDetail(Stream stream) { StreamReader sr = new StreamReader(stream); string s = sr.ReadToEnd(); sr.Dispose(); JObject jo = JObject.Parse(s); string Name = jo[ "Name" ].ToString(); string Age = jo[ "Age" ].ToString(); var result = new Person() { IsSucceed = true , Name = "Service Back:" + Name, Age = "Service Back:" + Age }; return new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(result))); } |
至此,“JS+HTML+WebService+套娃”框架基本完成,后面就是添砖加瓦的工作的,只需要写JS页面操作及WCF数据访问即可。
好了今天就写到这,如果这篇文章对你有所帮助,请推荐一下!欢迎转截,请注明出处!