zoukankan      html  css  js  c++  java
  • 使用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
  • 相关阅读:
    循环的注意点
    c语言实践输出某个区间中不是3的倍数的偶数
    while循环for循环优缺点和应用
    while 和do while循环的区别
    多重if else和switch case的区别
    if else的执行流程
    多个if和一个ifelse的区别
    对两个变量排序,从小到大输出
    【译】第四篇 Integration Services:增量加载-Updating Rows
    【译】第三篇 Integration Services:增量加载-Adding Rows
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2537679.html
Copyright © 2011-2022 走看看