使用Spring.Net将业务类封装成WebService

本文PDF下载

转载请注明出处

文章概述

在三层开发中,业务逻辑层不仅会在本系统中使用,而且常常会开放为WebService以供其他系统调用,从而共享计算能力。

然而,编写的WebService其实已经在业务逻辑层中实现了,只不过又重复编写了一次。Spring.NET框架提供了一整套的解决方案来简化这一过程,开发人员只需要创建配置文件,即可以实现将现有的业务逻辑开发为WebService

项目环境

l  VS2008

l  ASP.NET

l  Framework 3.5

l  Spring.NET 1.2

业务逻辑接口与实现类

在使用Spring.NET提供的将业务逻辑类开放为WebService方式时,此业务逻辑类必须要有一个接口

在下面的示例中,简单地创建了一个无参数方法GetName,这个方法返回一个字符串:

接口

namespace Test.IService.Default

{

    public interface IUserService

    {

        string GetName();

    }

}

实现类

namespace Test.Service.Default

{

    public class UserService : IUserService

    {

        public string GetName()

        {

            return "Sunny D.D";

        }

    }

}

 

引入Spring.NET

需要引入的Spring.NET框架的dll文件列表如下:

1.         antlr.runtime.dll

2.         Common.Logging.dll

3.         Spring.Aop.dll

4.         Spring.Core.dll

5.         Spring.Data.dll

6.         Spring.Web.dll

7.         Spring.Web.Extensions.dll

Web.config配置文件

configSections节点

  <configSections>

    <sectionGroup name="spring">

      <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>

      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>

      <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>

    </sectionGroup>

  </configSections>

Spring节点

<appSettings/>

  <connectionStrings/>

  <spring>

    <parsers>

      <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data"/>

      <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data"/>

      <parser type="Spring.Aop.Config.AopNamespaceParser, Spring.Aop"/>

    </parsers>

    <context>

      <resource uri="~/WebService.config"/>

    </context>

  </spring>

在此配置中由“<resource uri="~/WebService.config"/>”指明了Spring配置文件的路径。

httpHandlers节点

    <httpHandlers>

      <remove verb="*" path="*.asmx"/>

      <add path="*.asmx" verb="*" type="Spring.Web.Script.Services.ScriptHandlerFactory, Spring.Web.Extensions" validate="false" /> <!--这里是将业务逻辑类封装为WebService的关键配置-->

      <add path="*.aspx" verb="*" type="Spring.Web.Support.PageHandlerFactory, Spring.Web" />

    </httpHandlers>

在红色注释的代码行中,如果用户访问了.asmx结尾的文件(ASP.NETWebService资源),都将由Spring进行处理。

httpModules节点

    <httpModules>

      <add name="SpringModule" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>

    </httpModules>

WebService.config配置文件

在网站根目录中创建WebService.config,如图所示:

clip_image002

其代码如下:

<?xml version="1.0" encoding="utf-8" ?>

<objects xmlns="http://www.springframework.net"

           xmlns:db="http://www.springframework.net/database"

           xmlns:tx="http://www.springframework.net/tx"

     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

 

  <object id="Root.Test.UserService"

      type="Test.Service.Default.UserService, Test.Service.Default"

      scope="request">

  </object>

 

  <object id="TestService" type="Spring.Web.Services.WebServiceExporter, Spring.Web">

    <property name="TargetName" value="Root.Test.UserService"/>

    <property name="Namespace" value="http://tempuri.org/"/>

    <property name="Description" value="测试服务"/>

    <property name="MemberAttributes">

      <dictionary>

        <entry key="*">

          <object type="System.Web.Services.WebMethodAttribute, System.Web.Services">

          </object>

        </entry>

      </dictionary>

    </property>

  </object>

 

</objects>

Spring配置文件的格式及原理就不在这里讲述了,网络中有大量的资源。

测试页面

启动测试服务器,我的端口是2287

输入URLhttp://localhost:2287/SrpingWebServices/TestService.asmx

需要说明的是,TestService.asmx实际是不存在的,而是在WebService.config配置文件中,WebService对象的id。在本实例中就是:

<object id="TestService" type="Spring.Web.Services.WebServiceExporter, Spring.Web">

这时候可以看到WebService的说明页面,如图所示:

clip_image003

点击“GetName”超链接,可以看到如下页面:

clip_image005

点击“调用”按钮,可以获得WebService执行结果:

clip_image007

至此,大功告成。

本文资源

Spring.NET官方下载

 

转载于:https://www.cnblogs.com/sunnycoder/archive/2010/05/08/1730191.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值