WCF服务端
Contract
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web;
namespace Contract
{
/// <summary>
/// 服务接口
/// </summary>
/// <remarks>
/// WebInvoke - 指示服务操作在逻辑上就是调用操作,而且可由Web编程模型调用
/// WebGet - 指示服务操作在逻辑上就是检索操作,而且可由Web编程模型调用
/// UriTemplate - 用于服务操作的统一资源标识符(URI)模版。URI模版可以将一个URI或一组URI映射到服务操作
/// Method - 与操作关联的写作方法,默认为 POST
/// ResponseFormat - 指定从服务操作发出的响应的格式。Xml或Json
/// BodyStyle - 指定body里面的封装
/// </remarks>
[ServiceContract]
public interface IWcfService
{
/// <summary>
/// GET - 无参数 - 返回值为字符串数组
/// </summary>
/// <returns></returns>
[OperationContract]
[WebGet(
ResponseFormat = WebMessageFormat.Json)]
List<string> GetStudentNames();
/// <summary>
/// GET - 无参数 - 返回值为自定义类型的数组
/// </summary>
/// <returns></returns>
[OperationContract]
[WebGet(
ResponseFormat = WebMessageFormat.Json)]
List<Student> GetStudents();
/// <summary>
/// GET - 一个参数 - 返回值为int
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[OperationContract]
[WebGet(
UriTemplate = "GetStudentAge/{name}",
//UriTemplate = "GetStudentAge?name={name}", //默认形式
ResponseFormat = WebMessageFormat.Json)]
int GetStudentAge(string name);
/// <summary>
/// GET - 多个参数 - 返回值为自定义格式
/// </summary>
/// <param name="name"></param>
/// <param name="birth"></param>
/// <param name="age"></param>
/// <returns></returns>
[OperationContract]
[WebGet(
UriTemplate = "CreateStudent/{name}/{birth}/{age}", //UriTemplate中只允许字符串类型的变量
//UriTemplate = "CreateStudent?name={name}&birth={birth}&age={age}", //默认形式
ResponseFormat = WebMessageFormat.Json)]
Student CreateStudent(string name, string birth, string age);
/// <summary>
/// POST - 一个参数 - 返回值为bool
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[OperationContract]
[WebInvoke(
UriTemplate = "DeleteStudent",
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
bool DeleteStudent(string name);
/// <summary>
/// POST - 多个参数 - 返回值为自定义格式
/// </summary>
/// <param name="name"></param>
/// <param name="birth"></param>
/// <param name="age"></param>
/// <returns></returns>
[OperationContract]
[WebInvoke(
UriTemplate = "AddStudent",
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
Student AddStudent(string name, string birth, int age);
/// <summary>
/// POST - 自定义格式参数 - 返回值为string
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
[OperationContract]
[WebInvoke(
UriTemplate = "UpdateStudent",
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string UpdateStudent(Student student);
}
[DataContract]
public class Student
{
private string name;
[DataMember(Order = 0, Name = "name")]
public string Name
{
get { return name; }
set
{
name = HttpUtility.UrlDecode(value, Encoding.UTF8);
}
}