以下是契約層接口:
namespace Valor.ValorCom.Contracts{[ServiceContract(Name = "NAVService", Namespace = "www.valorcomm.com")]public interface INAVService{/// <summary>/// 添加訂單/// </summary>/// <param name="orderId">訂單號</param>/// <returns></returns>[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]string AddOrderForNAV(int orderId);}}
第一點要注意的:指定服務可以通過GET方式調(diào)用,設置請求和響應的格式都是JSON.
以下是服務類:
namespace Valor.ValorCom.Services{[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)][JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")]public class NAVService : INAVService{public NAVService(){}/// <summary>/// 添加訂單/// </summary>/// <param name="orderId">訂單號</param>/// <returns></returns>public string AddOrderForNAV(int orderId){string result = "";if (Common.TurnNav()){//添加訂單相關代碼}else{result = "未開啟與NAV系統(tǒng)同步訂單的接口";}return result;}}}
第二點要注意的,一定要加上[JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")],為javascript回調(diào)使用,UrlParameterName 設置用于跨域腳本訪問的 URL 查詢字符串參數(shù)名稱。[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 用于asp.net管道兼容,這樣的話此服務可以通過jquery ajax跨域調(diào)用,asp.net程序也可以通過生成此服務的代理來調(diào)用.
以下是配置文件信息
<?xml version="1.0"?><configuration><system.web><compilation debug="true"/></system.web><appSettings></appSettings><system.serviceModel><behaviors><endpointBehaviors><behavior name="webBehavior"><!--這里必須設置--><!--<webHttp />--><enableWebScript /></behavior></endpointBehaviors><serviceBehaviors><behavior name="navMetadataBehavior"><serviceMetadata httpGetEnabled="true" httpGetUrl="http://wcf.9valor.com/NAVService.svc/metadata"/></behavior></serviceBehaviors></behaviors><services><service behaviorConfiguration="navMetadataBehavior" name="Valor.ValorCom.Services.NAVService"><endpoint binding="webHttpBinding" address="http://127.0.0.1:90/NAVService/web" behaviorConfiguration="webBehavior" bindingConfiguration="webBinding" contract="Valor.ValorCom.Contracts.INAVService" /><endpoint address="http://127.0.0.1:90/NAVService" binding="basicHttpBinding" contract="Valor.ValorCom.Contracts.INAVService"></endpoint></service></services><bindings><webHttpBinding><binding name="webBinding" crossDomainScriptAccessEnabled="true"></binding></webHttpBinding></bindings><serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"><baseAddressPrefixFilters><add prefix="string"/></baseAddressPrefixFilters></serviceHostingEnvironment></system.serviceModel><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
第三點注意:
<service behaviorConfiguration="navMetadataBehavior" name="Valor.ValorCom.Services.NAVService"><endpoint binding="webHttpBinding" address="http://127.0.0.1:90/NAVService/web" behaviorConfiguration="webBehavior" bindingConfiguration="webBinding" contract="Valor.ValorCom.Contracts.INAVService" /><endpoint address="http://127.0.0.1:90/NAVService" binding="basicHttpBinding" contract="Valor.ValorCom.Contracts.INAVService"></endpoint></service>
這里配置了兩上終結(jié)點,第一個終結(jié)點的配置給jquery ajax以web的形式調(diào)用該服務,指定該終結(jié)點的綁定為webHttpBinding,我們看下behaviorConfiguration的配置,
behaviorConfiguration="webBehavior",如下圖配置,<enableWebScript /> 配置指定允許web腳本訪問。
<endpointBehaviors><behavior name="webBehavior"><!--這里必須設置--><!--<webHttp />--><enableWebScript /></behavior></endpointBehaviors>
接下來我們再看下bindingConfiguration的配置,bindingConfiguration="webBinding",詳細配置如下圖,crossDomainScriptAccessEnabled指定腳本可以跨域訪問.
<webHttpBinding><binding name="webBinding" crossDomainScriptAccessEnabled="true"></binding></webHttpBinding>
第二個終結(jié)點的配置提供給asp.net通過服務代理的方式調(diào)用.
最后就是客戶端調(diào)用(注:GET方式在各瀏覽器下都正常,POST方式只有在IE下能通過,其它瀏覽器因為安全原因拒絕跨域POST提交)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title></title><link href="~/Styles/Site.css" rel="stylesheet" type="text/css" /><script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script><script type="text/javascript">$(function () {$("#btnExcute").click(function () {var url = $("#txtServiceUrl").val();url += "&orderId="+$("#txtOrderId").val();$.ajax({type: "get",dataType: "json",url: url,success: function (returndata) {alert(returndata);}});});});</script></head><body><h2>修改單個產(chǎn)品</h2><p>Wcf Service Url:<input type="text" style="width: 700px;" id="txtServiceUrl" name="txtServiceUrl"value="http://127.0.0.1:90/AspNavService/web/AddOrderForNAV?jsoncallback=?" /></p><p>Order Id:<input type="text" id="txtOrderId" name="txtOrderId" value="11665369" /><br /><input type="button" id="btnExcute" name="btnExcute" value="修改" /></p></body></html>
浙公網(wǎng)安備 33010602011771號