虽然目前.NET对WebService支持的非常好,Spring.NET认为还是有几个方面可以改进:

  第一、.NET在.asmx文件中保存WebService请求和服务对象的关联关系,这些.asmx文件不管有用没用都得放在那儿。

  第二、Spring.NET希望能通过IoC容器对WebService进行依赖注入。一般说来WebService总会依赖其它服务对象,所以,如果能用配置方式来选择服务对象,这个功能就相当强大了。

  第三、目前在.NET中WebService的创建完全是一个实现(特定类型)的过程。多数服务(虽不能说是全部)都应实现为使用粗粒度服务接口的普通类型,并且,某个对象能否发布为远程对象、WebService还是企业(COM+)组件应该只与配置有关,而不应该取决于它的实现方式。

  ASP.NET用.aspx文件来保存表示层代码,用code-behind文件中的类保存应用逻辑,.aspx与类代码文件各有分工;但WebService却不同,WebService的逻辑完全是在code-behind的类中实现的。.asmx文件并没有什么真正的用途,实际上,这个文件既没有必要存在、也不应该存在。

在将WebServiceFactoryHandler类注册为响应*.asmx请求的HTTP Handler之后,开发人员就可以在IoC容器中用标准的Spring.NET对象定义来发布WebService。在以外编写WebService的过程中,需要增加两个特性:方法级的[WebMethod]及类型级的[WebService]。要想将这个对象发布为WebService,只需依次作以下操作:在web.config文件中,将Spring.Web.Services.WebServiceFactoryHandler注册为响应*.asmx请求的HTTP Handler。

  在客户端,.NET本身最大的缺陷是将客户端代码绑定在了代理类而非服务接口上。除非按照Jubal Lowy的著作《Programming .NET Components》中提到的方法,手工让代理类实现某个服务接口,否则应用程序代码的灵活性会很低,同时,如果需要使用一个新的、改进过的WebService类,或者打算用一个本地服务替换掉WebService时,相应的更换工作会很复杂。 在Spring.NET的支持下,可以很方便的为WebService创建实现了指定服务接口的客户端代理。

  

  准备条件:

    [Serializable]
    
public class Person
    {
        
public string Name { getset; }
        
public int Age { getset; }
    }

 

    public interface IPersonContract
    {
        
void SavePerson(Person model);

        Person GetPerson();
    }

 


    public class PersonContract : IPersonContract
    {

        
private Person m_person;

        
public void SavePerson(Person model)
        {
            
this.m_person = model;
            
this.m_person.Name += "";
            
this.m_person.Age++;
        }

        
public Person GetPerson()
        {
            
return this.m_person;
        }
    }

 

  服务器端:

  首先让我在Web.config中增加配置

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

 


</system.webServer>
    
</handlers>
        
<add name="SpringWebServiceSupport" verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web"/>
    
</handlers>
</system.webServer>

 


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

      
<object id="PersonContract" type="SpringNetWebService.PersonContract,SpringNetWebService"/>

      
<object id="PersonContractServer" type="Spring.Web.Services.WebServiceExporter, Spring.Web">
        
<property name="TargetName" value="PersonContract"/>
        
<property name="Namespace" value="http://tempuri.org/"/>
        
<property name="Description" value="刘冬编写的Spring.NET整合WebService的例子"/>
        
<property name="MemberAttributes">
          
<dictionary>
            
<entry key="GetPerson">
              
<object type="System.Web.Services.WebMethodAttribute, System.Web.Services">
                
<property name="Description" value="获取Person的方法描述"/>
                
<property name="MessageName" value="获取Person"/>
              
</object>
            
</entry>
            
<entry key="SavePerson">
              
<object type="System.Web.Services.WebMethodAttribute, System.Web.Services">
                
<property name="Description" value="保存Person的方法描述"/>
                
<property name="MessageName" value="保存Person"/>
              
</object>
            
</entry>
          
</dictionary>
        
</property>
      
</object>

    
</objects>

 

  在Global.asax的Application_Start中实例化Spring.NET容器
  WebApplicationContext ctx = ContextRegistry.GetContext() as WebApplicationContext;

 

  运行效果:

 

 

  客户端:


<object id="PersonServer" type="Spring.Web.Services.WebServiceProxyFactory, Spring.Services">
        
<!--服务器Uri-->
        
<property name="ServiceUri" value="http://localhost:1600/PersonContractServer.asmx"/>
        
<!--服务契约-->
        
<property name="ServiceInterface" value="IContract.IPersonContract, IContract"/>

        
<property name="ProductTemplate">
          
<object>
            
<!--超时时间10000毫秒-->
            
<property name="Timeout" value="10000" />
          
</object>
        
</property>
      
</object>

 


    class Program
    {
        
static void Main(string[] args)
        {
            IApplicationContext ctx 
= ContextRegistry.GetContext();

            IPersonContract server 
= ctx.GetObject("PersonServer"as IPersonContract;
            Person tempPerson 
= server.GetPerson();
            tempPerson 
= tempPerson ?? new Person() { Name = "刘冬", Age = 26 };
            server.SavePerson(tempPerson);

            Person person 
= server.GetPerson();
            
string msg = person == null ? "对象为空" : string.Format("姓名:{0},年龄:{1}", person.Name, person.Age);
            Console.WriteLine(msg);

            Console.ReadLine(); 
        }
    }

 

    运行效果:

 

posted on 2009-12-10 22:32  Tedd  阅读(235)  评论(0编辑  收藏  举报