温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

WCF简单教程(11) REST调用

发布时间:2020-06-26 11:03:32 阅读:10336 作者:BoyTNT 栏目:编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

第十一篇:REST调用

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

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


1、服务端:

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

using System; using System.ServiceModel; using System.ServiceModel.Web;   //这个命名空间要求引入System.ServiceModel.Web.dll namespace Server {     [ServiceContract(Namespace = "WCF.Demo")]     public interface IData     {         //WebInvoke中标明REST的相关属性,以这个方法为例,调用的Url是 ..../Data/key/data,HTTP方法是PUT,响应为Json格式(也可以换成xml)         //这样如果客户端用PUT方法访问 ..../Data/1/100,就会映射到CreateData方法上来,并且传入key=1,data=100         [OperationContract]         [WebInvoke(UriTemplate = "Data/{key}/{data}", Method = "PUT", ResponseFormat = WebMessageFormat.Json)]         void CreateData(string key, string data);          [OperationContract]         [WebInvoke(UriTemplate = "Data/{key}", Method = "GET", ResponseFormat = WebMessageFormat.Json)]         string RetrieveData(string key);          [OperationContract]         [WebInvoke(UriTemplate = "Data/{key}/{data}", Method = "POST", ResponseFormat = WebMessageFormat.Json)]         void UpdateData(string key, string data);          [OperationContract]         [WebInvoke(UriTemplate = "Data/{key}", Method = "DELETE", ResponseFormat = WebMessageFormat.Json)]         void DeleteData(string key);     } } 

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

using System; using System.Collections.Generic; using System.ServiceModel;  namespace Server {     //这个例子中用了Single Instance模式,这样m_DataDict的值才能保留住     [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]     public class DataProvider : IData     {         private Dictionary<string, string> m_DataDict = new Dictionary<string, string>();          public void CreateData(string key, string data)         {             m_DataDict[key] = data;         }          public string RetrieveData(string key)         {             return m_DataDict.ContainsKey(key) ? m_DataDict[key] : "NOT FOUND";         }          public void UpdateData(string key, string data)         {             m_DataDict[key] = data;         }          public void DeleteData(string key)         {             m_DataDict.Remove(key);         }     } } 

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

<?xml version="1.0" encoding="utf-8" ?> <configuration>     <system.serviceModel>         <services>             <service name="Server.DataProvider">                 <!--必须使用webHttpBinding,而且要定义此endpoint的behaviorConfiguration(见后)-->                 <endpoint address="" binding="webHttpBinding" contract="Server.IData" behaviorConfiguration="restBehavior" />                 <host>                     <baseAddresses>                         <add baseAddress="http://localhost:8080/wcf" />                     </baseAddresses>                 </host>             </service>         </services>          <behaviors>             <!--定义endpoint的behavior,webHttp节点表示启用web方式访问,这对REST是非常关键的-->             <endpointBehaviors>                 <behavior name="restBehavior">                     <webHttp/>                 </behavior>             </endpointBehaviors>         </behaviors>     </system.serviceModel> </configuration> 

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

using System; using System.ServiceModel;  namespace Server {     class Program     {         static void Main(string[] args)         {             using(ServiceHost host = new ServiceHost(typeof(Server.DataProvider)))             {                 host.Open();                 Console.WriteLine("Running ...");                 Console.ReadKey();                 host.Close();             }         }     } } 

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


2、客户端

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

using System; using System.Net;  namespace Client {     class Program     {         static void Main(string[] args)         {             //用一个WebClient就可以搞定了             var client = new WebClient();              //PUT方式访问Data/1/100,会映射到服务端的CreateData("1""100")             client.UploadString("http://localhost:8080/wcf/Data/1/100""PUT", string.Empty);              //GET方式访问Data/1,会映射到服务端的RetrieveData("1"),应该返回"100"             Console.WriteLine(client.DownloadString("http://localhost:8080/wcf/Data/1"));              //POST方式访问Data/1/200,会映射到服务端的UpdateData("1""200")                         client.UploadString("http://localhost:8080/wcf/Data/1/200""POST", string.Empty);              //GET一次,应该返回"200"             Console.WriteLine(client.DownloadString("http://localhost:8080/wcf/Data/1"));              //DELETE方式访问Data/1,会映射到服务端的DeleteData("1")             client.UploadString("http://localhost:8080/wcf/Data/1""DELETE", string.Empty);              //GET一次,应该返回"NOT FOUND"             Console.WriteLine(client.DownloadString("http://localhost:8080/wcf/Data/1"));         }     } } 


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

WCF简单教程(11) REST调用

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

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

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

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×