本文是《你是否覺得.Net Web Service 中asmx檔案是多餘的?》的繼續。主要討論Spring.Net發布的WebService基於介面發布調用問題。
目錄
1、.Net用戶端調用對於類似前一節中通過介面規範發布的服務,在.Net中可以通過鬆散的調用來完成。鬆散到什麼程度呢?只需要兩個條件:1、WebService地址 2、服務介面程式集。調用過程如下:<objects xmlns="http://www.springframework.net" xmlns:aop="http://www.springframework.net/aop">
<object id="person" type="Spring.Web.Services.WebServiceProxyFactory,Spring.Services">
<!--地址-->
<property name="ServiceUri" value="http://localhost:53825/PersonService.asmx"></property>
<!--服務介面-->
<property name="ServiceInterface" value="SpringWebServiceIoCContract.IPerson, SpringWebServiceIoCContract"/>
</object>
<object id ="personObj" type="SpringWebServiceIoCContract.Person,SpringWebServiceIoCContract"></object></objects> 調用過程就比較簡單了:
using (IApplicationContext context = ContextRegistry.GetContext())
{
var person0 = context.GetObject("person") as IPerson;
if (null!=person0)
{
Console.WriteLine(person0.Add(1, 2));
}
}
輸出:2、Ajax調用ajax的調用即顯得稍顯麻煩(可能是我還沒有發現最佳的調用方式),我這裡只列出調用方式供參考之前調用傳統的WebService: $.ajax({
type: "POST",
url: "WebService1.asmx/GetPerson",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (json) { $(json.d).each(function () { alert(this.Name + "-" + this.Age ) }) },
error: function (error) {
alert("調用出錯" + error.responseText);
}
});success時,返回的資料就是json類型的。但如果通過Spring.Net發布的Web Service ,我們的設定檔中對httpModule以及HttpHandler的重新設定已經改變了用戶端請求WebService調用時的處理模組了,如果在的配置如下: <httpHandlers>
<add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web"/>
</httpHandlers>
<httpModules>
<add name="SpringModule" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
</httpModules>
還通過這種 方式調用會產生問題,如:好在Spring.Net中同時提供了Spring.Web.Extensions程式集,如果有HttpModule過濾後的模組交給它處理就沒有問題。此時,我們的配置應該是: <httpHandlers>
<add verb="*" path="*.asmx" type="Spring.Web.Script.Services.ScriptHandlerFactory, Spring.Web.Extensions"/>
</httpHandlers>
<httpModules>
<add name="SpringModule" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
</httpModules>
這時,我們上述Ajax方式調用傳統的Web Service就沒有問題。提示:.Net用戶端(ConsoleApplication)調用也能成功。問題是,使用Spring.Web.Extensions,我們能調用Spring.Net發布的WebServicve 嗎。?我們將上述Ajax調用的url換成PersonService.asmx,如下: $.ajax({
type: "POST",
url: "PersonService.asmx/GetPerson",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (json) { $(json.d).each(function () { alert(this.Name + "-" + this.Age) }) },
error: function (error) {
alert("調用出錯" + error.responseText);
}
});
這時問題就出來了:
從中我們可以看出:PersonService.asmx/GetPerson是能調用成功的,只是JSON轉換的時候出現異常。解決方式:將IPerson介面中的GetPerson返回string類型,通過JsonConvert.SerializeObject進行轉換,如下:public string GetPerson(string name)
{
Person person = new Person { Age = 25, Name = "zhangsan" };
return JsonConvert.SerializeObject(person);
}
Ajax調用: $.ajax({
type: "POST",
url: "PersonService.asmx/GetPerson",
data: { name: 'a' },
dataType: "JSON",
success: function (json) {
alert(new Function("return " + json.replace(/<[^>]+>|[\r\n]/g, ""))().Name)
},
error: function (error) {
alert("錯誤:" + error.responseText);
}
});
這樣就調用成功。看看中和調用傳統WebService的區別:注意:傳統WebService調用,dataType: "json",而現在dataType: "JSON"。"JSON"大小寫區分.還有一種AJAX調用的方式:通過<scriptManager>來進行在如下配置:即
<add verb="*" path="*.asmx" type="Spring.Web.Script.Services.ScriptHandlerFactory, Spring.Web.Extensions"/>可以通過
PersonService.GetPerson(name, GetDataOnSucceeded);
function GetDataOnSucceeded()
{
alert("姓名:" + result.Name + "\n" + "年齡:" + result.Age);
}調用結果如下: