国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

WCF自托管、無配置文件實現jsonp(跨域)的訪問

2019-11-14 14:35:05
字體:
來源:轉載
供稿:網友

以下內容基于WCF4.0,本文將對比討論配置文件方案和無配置文件方案的實現方式。

    WCF4.0加入了對RESTFU和標準終結點的支持,這為實現跨域提供了簡單的方式。

一、有配置文件的情況:

首先我們先定義一個服務:

[ServiceContract]public class MinitorServer{        [OperationContract]        public bool Test()        {            return true;        }}

    在這里我故意沒有聲明接口,順便廢話幾句,正常情況下我們應該定義接口去顯示服務契約(servercontract)和操作契約(operationcontract),但是對于一些簡單的服務,我們可以省略接口的定義,做事不應循規蹈矩。

1、配置文件

<?xml version="1.0" encoding="utf-8" ?> <configuration>   <system.serviceModel>     <behaviors>      <endpointBehaviors>         <behavior name="webHttp">              <webHttp automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" />          </behavior>         </endpointBehaviors>          </behaviors>     <standardEndpoints>       <webHttpEndpoint>         <standardEndpoint crossDomainScriptaccessEnabled="true" />       </webHttpEndpoint>     </standardEndpoints>     <bindings>       <webHttpBinding>         <binding crossDomainScriptAccessEnabled="true"  />       </webHttpBinding>     </bindings>     <services>             <service name="HD.ExamMonitorClient.MinitorServer">         <endpoint kind="webHttpEndpoint"                   behaviorConfiguration="webHttp"                   address="http://localhost:8088/MonitorServer/"                   contract="HD.ExamMonitorClient.MinitorServer"/>       </service>     </services>   </system.serviceModel> </configuration>

在這里比較重要的是:

    kind="webHttpEndpoint" 表示該終結點采用標準終結點;

  crossDomainScriptAccessEnabled="true" 設置該終結點可以響應跨域請求;

  automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" 自動將響應類型設置為json,當然你可以根據自己的需求修改為xml。

2、修改服務加入attribute用來響應get請求:

[ServiceContract]public class MinitorServer{        [OperationContract]        [WebGet]        public bool Test()        {            return true;        }}

在這里如果你上一步沒有配置automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json"那你應該將[WebGet] 改為[WebGet(ResponseFormat=WebMessageFormat.Json)],必須要注意的是:如果單純響應非跨域請求,不需要設置defaultOutgoingResponseFormat="Json" ,因為在http請求的頭部已經指定了數據類型。

3、在控制臺中托管服務:

using(host = new ServiceHost(typeof(MinitorServer))){    host.Open();
   Console.ReadKey();}

4、瀏覽器測試

$(function () {             $.Ajax({                 type: "get",                 url: "http://localhost:8088/MonitorServer/Test",                 dataType: "jsonp",                 success: function (ret) {                     console.log(ret);                 }             });         });

二、無配置文件方式:

無配置文件方式的難點在于不能直接設置標準終結點。在這里要指出,標準終結點=綁定+終結點行為,所以我們可以這樣設置:

using(host = new ServiceHost(typeof(MinitorServer))){            //定義一個webHttp的綁定            WebHttpBinding webBing = new WebHttpBinding();            webBing.CrossDomainScriptAccessEnabled = true;                        //定義一個終結點行為            var endpointBehavior =new WebHttpBehavior();            endpointBehavior.AutomaticFormatSelectionEnabled = true;            endpointBehavior.DefaultOutgoingResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json;                        //加入服務            var end = host.AddServiceEndpoint(typeof(MinitorServer), webBing, "http://localhost:8088/MonitorServer/");            end.Behaviors.Add(endpointBehavior);                                   host.Open();            Console.ReadKey();  }            

現在可以將配置文件刪除了。

另外如果討厭去為每個操作協定設置[WebGet],那么這里有個簡單方式,在open之前,我們循環為每個操作協定加入行為即可。

var operationBehavio=new WebGetAttribute();foreach (var item in end.Contract.Operations){                if (item.Behaviors.Find<WebGetAttribute>() == null)                {                    item.Behaviors.Add(operationBehavio);                } }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宾川县| 克拉玛依市| 绥芬河市| 南城县| 杭州市| 手游| 项城市| 蒙山县| 龙江县| 温宿县| 白水县| 丰县| 陆河县| 西峡县| 开平市| 盘锦市| 洞口县| 万盛区| 聂拉木县| 安吉县| 金塔县| 梁河县| 喀喇沁旗| 平安县| 集贤县| 广安市| 阳新县| 张掖市| 永顺县| 福清市| 察隅县| 庆阳市| 吉隆县| 土默特右旗| 镇雄县| 眉山市| 博野县| 肇州县| 吉安市| 皋兰县| 乐亭县|