好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用Spring.Net构建WebService

使用Spring.Net构建WebService

用.Net开发 WebService一般都会先添加一个.asmx文件,然后在其.cs文件里将类标记上WebService,WebMethod等特性来构建WebService。是否想过.asmx文件是多余的呢。?

其实.asmx即不像aspx一样用作UI,又不像.handler文件一样处理业务逻辑,它的存在就是多余。

使用Spring.Net构建WebService完全可以不需要.asmx文件,直接通过它的IoC容器将它注入到对外提供服务的类中即可。

本文将带你走进一个没有.asmx文件的WebService。

目录: 创建不依赖于asmx文件的WebService  通过Spring.Net提供WebService服务   

了解本机,需要对Spring.Net有一些基本的认识。我大致说说它的作用:

1、可以作用一种IoC(或者说的DI)的容器实现程序的解耦。

2、使用面前方面编程(AOP)的框架

3、对不同事务之间的处理可以使用同一的管理方式

4、提供验证框架用来做验证

如怎样配置Spring.Net。如果你对Spring.Net不够了解,请参见; http://www.cnblogs.com/tyb1222/category/270053.html

 

1、 创建不依赖于asmx文件的WebService

首先通过Spring.Net创建不依赖具体asmx文件的Web Service .添加一个原始WebService的文件,只是通过Spring.Net的配置完成对具体文件的无依赖性。

    

      public   class  HelloWorldService : WebService  

    {
         // [WebMethod]
         public   string  HelloWorld( string  str)
        {
             return   " Hello World:     "  + str;
        }

        [WebMethod]
         public  Person GetPerson()
        {
             return   new  Person {Age =  25 , Name =  " zhansan " };
        }

        [WebMethod]
         public   void  SavePerson(Person person)
        {
             return ;
        }
    }

 

配置如下图:

 

注意图中abstract=true,可以让Spring.Net避免创建多余的服务对象,Spring推荐这样做。

访问服务:

 

2、通过Spring.Net提供WebService服务

这种方式是本节叙述的重点。由于服务的松散耦合性,很多人都认为服务更适合使用接口进行规范。Spring.Net也是基于这一点来实现的。

首先我们定义服务接口(有点类似WCF中的服务契约,但无需标记ServiceContract等等任何标记)   接口定义:

     public   interface  IPerson
    {
         string  SayHello( string  name);
         int  Add( int  x,  int  y);
         void  SavePerson(Person person);
        Person GetPerson( string  name);
         string  GetPersonString();
    }

 

服务实现没有什么特殊的地方,如下:

 

     public   class  PersonService : IPerson  

    {
         #region  IPerson 成员

         public   string  SayHello( string  name)
        {
             return   " Hello word:     "  + name;
        }

         public   int  Add( int  x,  int  y)
        {
             return  x + y;
        }

         public   void  SavePerson(Person person)
        {
             return ;
        }

         public  Person GetPerson( string  name)
        {
             return   new  Person {Age =  25 , Name =  " zhangsan " };
        }

         public   string  GetPersonString()
        {
             return  JsonConvert.SerializeObject( new  Person {Age =  25 , Name =  " zhangsan " });
        }
         #endregion
    }

 

通过Spring.Net对服务进行配置:

 

<configuration>  

    <configSections>
        <sectionGroup name= " spring " >
            <section name= " context "  type= " Spring.Context.Support.WebContextHandler, Spring.Web " />
            <section name= " objects "  type= " Spring.Context.Support.DefaultSectionHandler,Spring.Core " />
        </sectionGroup>
    </configSections>

    <spring>
        <context>
            <resource uri= " config://spring/objects " ></resource>
        </context>
        <objects xmlns= " http://www.springframework.net "  xmlns:aop= " http://www.springframework.net/aop " >
            < object  id= " person "  type= " SpringWebServiceIoC.PersonService,SpringWebServiceIoC " >
            </ object >
            < object  id = " personObj "  type= " SpringWebServiceIoCContract.Person,SpringWebServiceIoCContract " ></ object >
            < object  id= " PersonService "  type= " Spring.Web.Services.WebServiceExporter,Spring.Web " >
                <property name= " targetName "  value= " person " ></property>
                <property name= " MemberAttributes " >
                    <dictionary>
                        <entry key= " Add " >
                            < object  type= " System.Web.Services.WebMethodAttribute, System.Web.Services " >
                                <property name= " Description "  value= " 计算量整数之和 " ></property>
                                <property name= " MessageName "  value= " 计算 " ></property>
                            </ object >
                        </entry>
                    </dictionary>
                </property>
            </ object >
        </objects>
    </spring>
    
    <system.web>
        <compilation debug= " true "  targetFramework= " 4.0 "  />
        <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>
    </system.web>

</configuration>

 

 

此种方式除了需要配置处理amsx文件的Handler之外,还需配置httpModules模块,通过它来讲请求转发给Handler。

 

访问服务: 

注意:使用Spring.NEt实现WebService时,在3.5平台上开发的服务能正常运行在4.0上有可能会有如问题:“该方法的参数与签名不匹配。”但是在4.0上的服务,如果无参数又能正常运行,这通常是因为使用的Spring.Net版本不同导致。提醒注意。

代码下载: https://files.cnblogs.com/tyb1222/SpringWebService.rar 

 

分类:  Spring.Net

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于使用Spring.Net构建WebService的详细内容...

  阅读:44次