你是否觉得.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