欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
  jQuery Ajax调调用用WCF服服务务详详细细教教程程
 
  这两天在写基于WCF服务的后台框架,过程中遇到了一些挫折,经过 力全部解决了,在此分享给大家,使用的工具是V isual St
 
  udio 2013 。
 
  该后台需要支持通过j son来传递和接收数据。
 
  首先,说说搭建过程。
 
  第第一一步步::创创建建WCF服服务务应应用用程程序序项项目目WCF。。
 
  第第二二步步,,创创建建服服务务使使用用的的数数据据类类
 
  using System;
 
  using System.ComponentModel.DataAnnotations;
 
  using System.ComponentModel.DataAnnotations.Schema;
 
  using System.Runtime.Serialization;
 
  namespace WCF
 
  {
 
  [DataContract
 
  [Table("TUser")
 
  public class Person
 
  {
 
  [DataMember
 
  public int ID { get; set; }
 
  [DataMember
 
  [StringLength(100)
 
  public string LoginName { get; set; }
 
  [DataMember
 
  [StringLength(100)
 
  public string Password { get; set; }
 
  [DataMember
 
  [DataType(DataType.Date)
 
  public DateTime CreateDate { get; set; }
 
  }
 
  }
 
  这里,由于我使用EF来与数据库交互,所以使用了Table、StringLength、DataType。若你未使用EF,可以不加这些。DataContr
 
  a t是用来标志当前类在序列化时需要参考DataMember属性,若不设DataContra t或仅设置DataMember,则所有共有属性和字
 
  段全部序列化,否则,只对设置有DataMember的序列化。注意,DataContra t和DataMember与反序列化无关,也就是说,当把
 
  一个j son对象字符串传递给WCF服务时,不管该字段上是否有DataMember,都会被反序列化。
 
  第第三三步步::创创建建服服务务契契约约接接口口
 
  如果你的服务仅仅用来提供Aj ax等一些非WCF客户端访问的,那么是不需要接口的,把接口定义中的各种A ttribute直接加在服务
 
  提供的类的定义上即可。但是为了能让程序可以通过服务接口来访问,那么必须使用接口,例如:前端MVC+后台WCF的架构形
 
  式。
 
  using System.Collections.Generic;
 
  using System.ServiceModel;
 
  using System.ServiceModel.Web;
 
  namespace WCF
 
  {
 
  [ServiceContract
 
  public interface IPersonService
 
  {
 
  [OperationContract
 
  [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat =
 
  WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)
 
  Person CreatePerson(string loginName, string password);
 
  //服务功能2
 
  [OperationContract
 
  [WebGet(RequestFormat = WebMessage

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h62644.shtml