以下內(nèi)容基于WCF4.0,本文將對比討論配置文件方案和無配置文件方案的實(shí)現(xiàn)方式。
WCF4.0加入了對RESTFU和標(biāo)準(zhǔn)終結(jié)點(diǎn)的支持,這為實(shí)現(xiàn)跨域提供了簡單的方式。
一、有配置文件的情況:
首先我們先定義一個(gè)服務(wù):
[ServiceContract]public class MinitorServer{ [OperationContract] public bool Test() { return true; }}
在這里我故意沒有聲明接口,順便廢話幾句,正常情況下我們應(yīng)該定義接口去顯示服務(wù)契約(servercontract)和操作契約(operationcontract),但是對于一些簡單的服務(wù),我們可以省略接口的定義,做事不應(yīng)循規(guī)蹈矩。
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" 表示該終結(jié)點(diǎn)采用標(biāo)準(zhǔn)終結(jié)點(diǎn);
crossDomainScriptAccessEnabled="true" 設(shè)置該終結(jié)點(diǎn)可以響應(yīng)跨域請求;
automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" 自動將響應(yīng)類型設(shè)置為json,當(dāng)然你可以根據(jù)自己的需求修改為xml。
2、修改服務(wù)加入attribute用來響應(yīng)get請求:
[ServiceContract]public class MinitorServer{ [OperationContract] [WebGet] public bool Test() { return true; }}
在這里如果你上一步?jīng)]有配置automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json"那你應(yīng)該將[WebGet] 改為[WebGet(ResponseFormat=WebMessageFormat.Json)],必須要注意的是:如果單純響應(yīng)非跨域請求,不需要設(shè)置defaultOutgoingResponseFormat="Json" ,因?yàn)樵趆ttp請求的頭部已經(jīng)指定了數(shù)據(jù)類型。
3、在控制臺中托管服務(wù):
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); } }); });
二、無配置文件方式:
無配置文件方式的難點(diǎn)在于不能直接設(shè)置標(biāo)準(zhǔn)終結(jié)點(diǎn)。在這里要指出,標(biāo)準(zhǔn)終結(jié)點(diǎn)=綁定+終結(jié)點(diǎn)行為,所以我們可以這樣設(shè)置:
using(host = new ServiceHost(typeof(MinitorServer))){ //定義一個(gè)webHttp的綁定 WebHttpBinding webBing = new WebHttpBinding(); webBing.CrossDomainScriptAccessEnabled = true; //定義一個(gè)終結(jié)點(diǎn)行為 var endpointBehavior =new WebHttpBehavior(); endpointBehavior.AutomaticFormatSelectionEnabled = true; endpointBehavior.DefaultOutgoingResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json; //加入服務(wù) var end = host.AddServiceEndpoint(typeof(MinitorServer), webBing, "http://localhost:8088/MonitorServer/"); end.Behaviors.Add(endpointBehavior); host.Open(); Console.ReadKey(); }
現(xiàn)在可以將配置文件刪除了。
另外如果討厭去為每個(gè)操作協(xié)定設(shè)置[WebGet],那么這里有個(gè)簡單方式,在open之前,我們循環(huán)為每個(gè)操作協(xié)定加入行為即可。
var operationBehavio=new WebGetAttribute();foreach (var item in end.Contract.Operations){ if (item.Behaviors.Find<WebGetAttribute>() == null) { item.Behaviors.Add(operationBehavio); } }
新聞熱點(diǎn)
疑難解答
圖片精選