我们专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

WCF简单教程(11)REST调用

第十一篇:REST调用

香河网站建设公司创新互联公司,香河网站设计制作,有大型网站制作公司丰富经验。已为香河近千家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的香河做网站的公司定做!

上篇写的是Ajax调用WCF,今天写一篇如何以REST方式调用WCF服务。不知道REST是什么的同学,可以去google一下。对某些类型的应用,REST还是相当不错的方式,所以专门写一篇来说明一下开发方法。

老规矩,上代码,直接在代码注释里讲解。


1、服务端:

服务契约,我们定义CRUD4个方法(增查改删),对应HTTP METHOD分别为PUT/GET/POST/DELETE:

  1. using System; 
  2. using System.ServiceModel; 
  3. using System.ServiceModel.Web; //这个命名空间要求引入System.ServiceModel.Web.dll
  4.  
  5. namespace Server 
  6. { 
  7.     [ServiceContract(Namespace = "WCF.Demo")] 
  8.     public interface IData 
  9.     { 
  10.         //WebInvoke中标明REST的相关属性,以这个方法为例,调用的Url是 ..../Data/key/data,HTTP方法是PUT,响应为Json格式(也可以换成xml) 
  11.         //这样如果客户端用PUT方法访问 ..../Data/1/100,就会映射到CreateData方法上来,并且传入key=1,data=100 
  12.         [OperationContract] 
  13.         [WebInvoke(UriTemplate = "Data/{key}/{data}", Method = "PUT", ResponseFormat = WebMessageFormat.Json)] 
  14.         void CreateData(string key, string data); 
  15.  
  16.         [OperationContract] 
  17.         [WebInvoke(UriTemplate = "Data/{key}", Method = "GET", ResponseFormat = WebMessageFormat.Json)] 
  18.         string RetrieveData(string key); 
  19.  
  20.         [OperationContract] 
  21.         [WebInvoke(UriTemplate = "Data/{key}/{data}", Method = "POST", ResponseFormat = WebMessageFormat.Json)] 
  22.         void UpdateData(string key, string data); 
  23.  
  24.         [OperationContract] 
  25.         [WebInvoke(UriTemplate = "Data/{key}", Method = "DELETE", ResponseFormat = WebMessageFormat.Json)] 
  26.         void DeleteData(string key); 
  27.     } 
  28. } 

然后是实现类,这个简单,没什么可说的。

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ServiceModel; 
  4.  
  5. namespace Server 
  6. { 
  7.     //这个例子中用了Single Instance模式,这样m_DataDict的值才能保留住 
  8.     [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
  9.     public class DataProvider : IData 
  10.     { 
  11.         private Dictionary m_DataDict = new Dictionary(); 
  12.  
  13.         public void CreateData(string key, string data) 
  14.         { 
  15.             m_DataDict[key] = data; 
  16.         } 
  17.  
  18.         public string RetrieveData(string key) 
  19.         { 
  20.             return m_DataDict.ContainsKey(key) ? m_DataDict[key] : "NOT FOUND"; 
  21.         } 
  22.  
  23.         public void UpdateData(string key, string data) 
  24.         { 
  25.             m_DataDict[key] = data; 
  26.         } 
  27.  
  28.         public void DeleteData(string key) 
  29.         { 
  30.             m_DataDict.Remove(key); 
  31.         } 
  32.     } 
  33. } 

配置文件最关键了,注意里面绿色的注释部分:

  1.  
  2.  
  3.      
  4.          
  5.              
  6.           
  7.                  
  8.                  
  9.                      
  10.                          
  11.                      
  12.                  
  13.              
  14.          
  15.  
  16.          
  17.           
  18.              
  19.                  
  20.                      
  21.                  
  22.              
  23.          
  24.      
  25.  

最后发布服务,没什么特殊的,和以前一样:

  1. using System; 
  2. using System.ServiceModel; 
  3.  
  4. namespace Server 
  5. { 
  6.     class Program 
  7.     { 
  8.         static void Main(string[] args) 
  9.         { 
  10.             using(ServiceHost host = new ServiceHost(typeof(Server.DataProvider))) 
  11.             { 
  12.                 host.Open(); 
  13.                 Console.WriteLine("Running ..."); 
  14.                 Console.ReadKey(); 
  15.                 host.Close(); 
  16.             } 
  17.         } 
  18.     } 
  19. } 

这个服务端没有用IIS做HOST,直接用自己的进程做的宿主(当然了,本质还是http.sys在工作)。

 


2、客户端

我们这回要用REST形式访问服务端,所以不是普通意义上的WCF客户端了,再也用不着那么麻烦的写配置文件创建Channel或者代理了。

  1. using System; 
  2. using System.Net; 
  3.  
  4. namespace Client 
  5. { 
  6.     class Program 
  7.     { 
  8.         static void Main(string[] args) 
  9.         { 
  10.          //用一个WebClient就可以搞定了 
  11.             var client = new WebClient(); 
  12.  
  13.      //以PUT方式访问Data/1/100,会映射到服务端的CreateData("1", "100") 
  14.      client.UploadString("http://localhost:8080/wcf/Data/1/100", "PUT", string.Empty); 
  15.  
  16.      //以GET方式访问Data/1,会映射到服务端的RetrieveData("1"),应该返回"100" 
  17.      Console.WriteLine(client.DownloadString("http://localhost:8080/wcf/Data/1")); 
  18.  
  19.      //以POST方式访问Data/1/200,会映射到服务端的UpdateData("1", "200")             
  20.     client.UploadString("http://localhost:8080/wcf/Data/1/200", "POST", string.Empty); 
  21.  
  22.      //再GET一次,应该返回"200" 
  23.      Console.WriteLine(client.DownloadString("http://localhost:8080/wcf/Data/1")); 
  24.  
  25.      //以DELETE方式访问Data/1,会映射到服务端的DeleteData("1") 
  26.      client.UploadString("http://localhost:8080/wcf/Data/1", "DELETE", string.Empty); 
  27.  
  28.      //再GET一次,应该返回"NOT FOUND" 
  29.          Console.WriteLine(client.DownloadString("http://localhost:8080/wcf/Data/1")); 
  30.         } 
  31.     } 
  32. } 


OK,运行一下客户端,返回如下,和预期一致:

WCF简单教程(11) REST调用

 

需要补充一下,如果用IIS做HOST,比如DataService.svc.cs是实现类,一定要在DataService.svc中加上Factory,如下:

  1. <%@ ServiceHost Language="C#" Debug="true" Service="WebServer.DataService" CodeBehind="DataService.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %> 

表明不是使用默认的ServiceHostFactory,而是适应WEB HTTP开发的WebServiceHostFactory。

 

 


分享名称:WCF简单教程(11)REST调用
网页网址:http://www.swkodf.com/article/pjhicp.html

其他资讯