正文转自:http://www.cnblogs.com/scy251147/p/3566638.html

前言

习俗的Asmx服务,由于本SOAP协议,所以回来内容为xml方式组织。并且客户端需要添加服务端引用才会利用(虽然看网及曾提供了即上面的Dynamic
Proxy,但是没有这种办法方便),所以叫开发暨配置带来了无小的分神。并且当服务了多的时候,生成的援文件会大怪,之前项目的一个引用文件才引用代码都来5000差不多实施,全部在一个近乎吃。确实不便民维护。

冲以上几乎沾,就专门研究了转根据Restful的服务支出,当时境遇有少栽框架,一个凡是WCF
Restful Service,另一个凡是Asp.net Web
API。由于针对WCF比较熟悉一些,所以即使分选了前者。

Restful Service及其相关

说到Restful
Service,不得不干其中的Rest这个重中之重字。它是用以创造分布式超文本媒体的相同种植架构方式,我们可以通过正规的HTTP(GET,POST,PUT,DELETE)操作来构建基于面向资源的软件架构方式(Resource-Oriented
Architecture
(ROA)
)。它是单独于任何技术还是平台的,所以人们常常用合这种操作规范之服务称为“RESTful
services”。因为WCF能够构建适合这种专业之劳务,所以我们经常称之为 WCF
Restful Services。

是因为传统的WCF
Service可以运用tcp,net.msmq,http等协商进行数据交换,并且应用了RPC(Remote
Procedure
Call)的工作方法,客户端需要丰富对服务端的援才会形成。但是WCF Restful
Service完全使Http协议来进行,并且无需补充加客户端引用,所以方便多。

劳务端支出同扫

下为图书馆的例子来开具体的认证。

开辟VS2010,新建一个WCF REST Service
Application项目,然后于档次遭到,添加一个BookService.cs用于拍卖逻辑操作,再添加一个BookEntity.cs用于供实体类。

开拓Global.asax,可以看到如下代码:

   1:   void Application_Start(object sender, EventArgs e)

   2:   {

   3:      RegisterRoutes();

   4:   }

   5:   

   6:   private void RegisterRoutes()

   7:   {

   8:      RouteTable.Routes.Add(new ServiceRoute("BookService", new WebServiceHostFactory(), typeof(BookService)));

   9:   }

 

内RegisterRoutes是设定服务启动的入口点的。

接下来是BookEntity实体类的团组织方式:

   1:   public class BookEntity

   2:   {

   3:          public int BookID { get; set; }

   4:   

   5:          public string BookName { get; set; }

   6:   

   7:          public decimal BookPrice { get; set; }

   8:   

   9:          public string BookPublish { get; set; }

  10:   }

这边我哪怕甭多说了,实体类富含图书序号,图书名称,图书价格,出版单位四单特性。

接下来便是咱们的核心内容:

   1:      [ServiceContract]

   2:      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

   3:      [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

   4:      public class BookService

   5:      {

   6:          public BookService()

   7:          {

   8:              bookList = new List<BookEntity>();

   9:              BookEntity book = new BookEntity();

  10:              book.BookID = 1;

  11:              book.BookName = "大话设计模式";

  12:              book.BookPrice = (decimal)45.2;

  13:              book.BookPublish = "中国邮电出版社";

  14:              bookList.Add(book);

  15:   

  16:              BookEntity book1 = new BookEntity();

  17:              book1.BookID = 2;

  18:              book1.BookName = "测试用例";

  19:              book1.BookPrice = (decimal)21.0;

  20:              book1.BookPublish = "清华大学出版社";

  21:              bookList.Add(book1);

  22:   

  23:              BookEntity book2 = new BookEntity();

  24:              book2.BookID = 3;

  25:              book2.BookName = "Rework";

  26:              book2.BookPrice = (decimal)15.4;

  27:              book2.BookPublish = "Wrox pulishment";

  28:              bookList.Add(book2);

  29:          }

  30:   

  31:          private static List<BookEntity> bookList;

  32:   

  33:          [WebInvoke(Method = "GET"

  34:              , ResponseFormat = WebMessageFormat.Json

  35:              , BodyStyle = WebMessageBodyStyle.Bare   //不需要任何修饰,否则生成的json无法解析

  36:              , UriTemplate = "/?bookID={bookID}")]  //只接收string类型,如果是其他类型,需要按照 /?para={parameter}的方式来组织。

  37:          public BookEntity Get(int bookID)

  38:          {

  39:              return bookList.Where(p => p.BookID == bookID).FirstOrDefault();

  40:          }

  41:   

  42:          [WebInvoke(Method = "GET"

  43:              , ResponseFormat = WebMessageFormat.Json

  44:              , BodyStyle = WebMessageBodyStyle.Bare

  45:              , UriTemplate = "/")]

  46:          public List<BookEntity> GetALL()

  47:          {

  48:              return bookList;

  49:          }

  50:   

  51:            [WebInvoke(Method = "POST"

  52:              , ResponseFormat = WebMessageFormat.Json

  53:              , BodyStyle = WebMessageBodyStyle.Bare

  54:              , UriTemplate = "/")]

  55:          public bool Update(BookEntity book)

  56:          {

  57:              BookEntity query = (from p in bookList where p.BookID == book.BookID select p).FirstOrDefault();

  58:              bookList.Remove(query);

  59:              bookList.Add(book);

  60:              return true;

  61:            }

  62:   

  63:           [WebInvoke(Method = "PUT"

  64:              , ResponseFormat = WebMessageFormat.Json

  65:              , BodyStyle = WebMessageBodyStyle.Bare

  66:              , UriTemplate = "/")]

  67:          public bool Add(BookEntity book)

  68:          {

  69:              bookList.Add(book);

  70:              return true;

  71:          }

  72:   

  73:          [WebInvoke(Method = "DELETE"

  74:              , ResponseFormat = WebMessageFormat.Json

  75:              , BodyStyle = WebMessageBodyStyle.Bare

  76:              , UriTemplate = "/")]

  77:          public bool Delete(BookEntity book)

  78:          {

  79:              BookEntity bookCurrent = (from p in bookList where p.BookID == book.BookID select p).FirstOrDefault();

  80:              return bookList.Remove(bookCurrent);

  81:          }

  82:      }

内,Method
方法要是标志可以领客户端的乞求类型,这里有四种:GET,POST,PUT,DELETE,其中GET为请数据,POST为创新数据,PUT为新增多少,DELETE代表正在抹数据。

接下来ResponseFormat
则象征正归的数据组织,如果是Json则表明客户端会接收至Json数据,如果是XML则表明客户端会接收及XML组织的多少。BodyStyle
代表回到数据的包裹对象,如果是Bare则表明数据无外包装,原生数据返回;如果是Wrapped则表明数据会在极端外层包装一个当下函数名称加上Result的依样画葫芦。比如对于Delete对象,则会回去
DeleteResult:{******},会促成DataContractJsonSerializer无法进行反序列化。

UriTemplate
主要用来指定操作的URI路径,只要用户输入了合法途径并应用了不易的乞求方式,就见面硌发该函数。

终极说及的便是URI后面和的参数的题材,由于函数只能承受string类型的,所以一旦传入参数是string类型,则足以行使UriTemplate
= “{bookID}”的路线,反之,则要丰富/?param1={paramname}的不二法门,比如我代码中运用的凡:UriTemplate
= “/?bookID={bookID}”。

当尽都将好下,让咱们运行一下,访问如下路径,就好得到结果:

http://localhost:45345/BookService/

得到的结果如下:

[{"BookID":1,"BookName":"大话设计模式","BookPrice":45.2,"BookPublish":"中国邮电出版社"},{"BookID":2,"BookName":"测试用例","BookPrice":21,"BookPublish":"清华大学出版社"},{"BookID":3,"BookName":"Rework","BookPrice":15.4,"BookPublish":"Wrox pulishment"}]

 如果看http://localhost:45345/BookService/?bookID=1,则会得到如下的结果:

{"BookID":1,"BookName":"大话设计模式","BookPrice":45.2,"BookPublish":"中国邮电出版社"}

客户端支出同扫

开始测试成功后,让咱们来拓展一下两全的测试。

先是,在品种中,我们新建一个Asp.net WebForm
Application,用于做测试工作。

接下来,在Default.aspx.cs中,针对GET操作,我们抬高如下代码:

   1:   private void GetBookByID(string id)

   2:          {

   3:              WebClient proxy = new WebClient();

   4:              string serviceURL = string.Empty;

   5:              DataContractJsonSerializer obj ;

   6:              if (string.IsNullOrEmpty(id))

   7:              {

   8:                  serviceURL = string.Format("http://localhost:45345/BookService/");

   9:                  obj = new DataContractJsonSerializer(typeof(List<BookEntity>));

  10:              }

  11:              else

  12:              {

  13:                  serviceURL = string.Format("http://localhost:45345/BookService/?bookID=" + id);

  14:                  obj = new DataContractJsonSerializer(typeof(BookEntity));

  15:              }

  16:              byte[] data = proxy.DownloadData(serviceURL);

  17:              Stream stream = new MemoryStream(data);

  18:              var result = obj.ReadObject(stream);

  19:              List<BookEntity> list=new List<BookEntity>();

  20:              if (result is BookEntity)

  21:                  list.Add(result as BookEntity);

  22:              else if (result is List<BookEntity>)

  23:                  list = result as List<BookEntity>;

  24:              GridView1.DataSource = list;

  25:              GridView1.DataBind();

  26:          }

于上述代码中,DataContractJsonSerializer
是WCF提供的一个行列化类,用于将目标序列化或者反序列化。

形容好之后,我们点击界面按钮,出现了以下的结果:

图片 1图片 2

对PUT操作,也便是加上操作,我们添加如下代码:

   1:  BookEntity bookEntity = new BookEntity();

   2:              bookEntity.BookID = Int32.Parse(txtBookID.Text);

   3:              bookEntity.BookName = txtBookName.Text;

   4:              bookEntity.BookPrice = decimal.Parse(txtBookPrice.Text);

   5:              bookEntity.BookPublish = txtBookPublish.Text;

   6:   

   7:              DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(BookEntity));

   8:              MemoryStream ms = new MemoryStream();

   9:              obj.WriteObject(ms, bookEntity);

  10:              byte[] byteSend = ms.ToArray();

  11:              ms.Close();

  12:   

  13:              string serviceURL = string.Format("http://localhost:45345/BookService");

  14:   

  15:              WebClient test = new WebClient();

  16:              test.Headers.Add("Content-Type", "application/json");

  17:              test.Headers.Add("ContentLength", byteSend.Length.ToString());

  18:              

  19:   

  20:              byte[] responseData = test.UploadData(serviceURL, "PUT", byteSend);

  21:   

  22:              string result = Encoding.GetEncoding("UTF-8").GetString(responseData);

  23:              lblLog.Text = result;

当召开这步的早晚,需要留意,test.Headers.Add(“Content-Type”, “application/json”)
和test.Headers.Add(“ContentLength”,
byteSend.Length.ToString())需要丰富,否则会招致Http 400
返回的荒谬。并且,向服务端传递实体的下,可以透过采用UploadData的主意来拓展,如果数据量过非常,可以设想下异步方式传送。

接下的POST和DELETE方法以及方类似,我还贴一下:

POST方法:

   1:   BookEntity bookEntity = new BookEntity();

   2:              bookEntity.BookID = Int32.Parse(txtBookID.Text);

   3:              bookEntity.BookName = txtBookName.Text;

   4:              bookEntity.BookPrice = decimal.Parse(txtBookPrice.Text);

   5:              bookEntity.BookPublish = txtBookPublish.Text;

   6:   

   7:              DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(BookEntity));

   8:              MemoryStream ms = new MemoryStream();

   9:              obj.WriteObject(ms, bookEntity);

  10:              byte[] byteSend = ms.ToArray();

  11:              ms.Close();

  12:   

  13:              string serviceURL = string.Format("http://localhost:45345/BookService");

  14:   

  15:              WebClient test = new WebClient();

  16:              test.Headers.Add("Content-Type", "application/json");

  17:              test.Headers.Add("ContentLength", byteSend.Length.ToString());

  18:              

  19:              byte[] responseData = test.UploadData(serviceURL, "POST", byteSend);

  20:   

  21:              string result = Encoding.GetEncoding("UTF-8").GetString(responseData);

  22:              lblLog.Text = result;

DELETE方法:

   1:   BookEntity bookEntity = new BookEntity();

   2:              bookEntity.BookID = Int32.Parse(txtBookID.Text);

   3:   

   4:              DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(BookEntity));

   5:              MemoryStream ms = new MemoryStream();

   6:              obj.WriteObject(ms, bookEntity);

   7:              byte[] byteSend = ms.ToArray();

   8:              ms.Close();

   9:   

  10:              string serviceURL = string.Format("http://localhost:45345/BookService");

  11:   

  12:              WebClient test = new WebClient();

  13:              test.Headers.Add("Content-Type", "application/json");

  14:              test.Headers.Add("ContentLength", byteSend.Length.ToString());

  15:              

  16:   

  17:              byte[] responseData = test.UploadData(serviceURL, "DELETE", byteSend);

  18:   

  19:              string result = Encoding.GetEncoding("UTF-8").GetString(responseData);

  20:              lblLog.Text = result;

终极得到的功能图如下:

图片 3

(新增记录)

图片 4

(更新记录)

图片 5

(删除记录)

 成文仓促,难免产生误,还请求指出,在此谢过。

源代码下载

点击下载源代码  

 

Edit:基于本方构建的Android服务就当利用中。后续继续跟进各种以信息。

在StackOverFlow问答如下:点击这里翻

 看评论被提到了IContract问题,由于当下是Restful
Service,不是根据RPC模式,所以没有必要采取的。不过用上呢从没错。

 

 

分类: WCF
点滴记录

相关文章

网站地图xml地图