你是否覺得.Net Web Service 中asmx文件是多余的?---客戶端調用


本文是 《你是否覺得.Net Web Service 中asmx文件是多余的?》 的繼續。主要討論Spring.Net發布的WebService基於接口發布調用問題。

 

目錄

 

  •  .Net客戶端調用
  • Ajax調用

 

 

 

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( 12));
                }

   } 


輸出:
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);    
}
調用結果如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM