using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Runtime.Serialization; namespace ExtJSAndWCFChapter1 { [DataContract] publicclass Employee { [DataMember] public Guid EmployeeID { set; get; } [DataMember] publicstring CnName { set; get; } [DataMember] publicbool Sex { set; get; } [DataMember] publicint Age { set; get; } [DataMember] public DateTime Birthday { set; get; } [DataMember] publicstring Email { set; get; } } }
接下来编写EmployeeService.cs的代码,代码如下:
using System; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; using System.Collections.Generic; namespace ExtJSAndWCFChapter1 { [ServiceContract(Namespace ="")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] publicclass EmployeeService { // Add [WebGet] attribute to use HTTP GET [OperationContract] publicvoid DoWork() { // Add your operation implementation here return; } // Add more operations here and mark them with [OperationContract] ///<summary> /// 创建一个实体,实体由客户端传递 ///</summary> ///<param name="emp"></param> ///<returns></returns> [OperationContract] [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate ="/Create")] public Guid Create(Employee emp) { NotNull(emp.CnName, "CnName"); return Guid.NewGuid(); } ///<summary> /// 获取一个实体 ///</summary> ///<param name="id"></param> ///<returns></returns> [OperationContract] [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate ="/Get")] public Employee Get(int id) { if (id !=1) thrownew ArgumentException("Expected 1 for ID"); returnnew Employee() { EmployeeID = Guid.NewGuid(), CnName ="Xiaozhuang", Sex =true, Age =28, Email ="iamxiaozhuang@163.com", Birthday =new DateTime(1979, 02, 02) }; } ///<summary> /// 获取所有实体 ///</summary> ///<returns></returns> [OperationContract] [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate ="/GetAll")] public List<Employee> GetAll() { returnnew List<Employee> { new Employee(){EmployeeID = Guid.NewGuid(),CnName="CnName",Sex=true,Age=28,Email="email@saf.com",Birthday=new DateTime(1979,02,02)}, new Employee(){EmployeeID = Guid.NewGuid(),CnName="CnName1",Sex=false,Age=28,Email="email1@saf.com",Birthday=new DateTime(1979,02,02)} }; } ///<summary> /// 获取num个实体 ///</summary> ///<param name="num"></param> ///<returns></returns> [OperationContract] [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate ="/GetByNum")] public List<Employee> GetByNum(int num) { if (num.ToString() =="") thrownew ArgumentException("参数错误!"); List<Employee> emplist =new List<Employee>(); for (int i =1; i <= num; i++) { Employee emp =new Employee() { EmployeeID = Guid.NewGuid(), CnName = i +"CnName", Sex =true, Age = i *10, Email = i +"email@163.com", Birthday =new DateTime(1979, 02, 02) }; emplist.Add(emp); } return emplist; } privatestaticvoid NotNull<T>(T o, string paramName) where T : class { if (o ==null) thrownew ArgumentNullException(paramName); } } }
现在可以编译并访问那个EmployeeService.svc文件,后面加上 UriTemplate的值:例如http://localhost:1481/EmployeeService.svc/get。会得到“Method not allowed”的提示。如果访问出现错误,请确认修改的Web.Config是否正确。 接下来编写Default.aspx的代码:代码如下