好得很程序员自学网

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

Spring.Net框架与WCF的集成1

Spring.Net框架与WCF的集成1

Spring.Net框架与WCF的集成(上)

 

Spring.Net集成了.Net多方面的开发,比如:WebService、.Net Remoing、WCF等。本文

简单介绍一下通过Spring.Net的IoC容器开发WCF 服务。

示例使用的Spring.Net 版本:1.3.2。本节介绍的是基于Spring.Net的IoC容器来开发WCF服

务。这种方式和之前WCF开发差别不大,只是服务的寄宿、以及客户端代理的创建都交由Spring.Net

来完成。以下通过一个简单示例进行说明。

 

1、ServiceContract定义以及服务配置:

 

 

     [ServiceContract(Namespace =  " Spring.WCF " )]

     public interface  IAdd
    {
        [OperationContract]
         int  Add( int  x,  int  y);
    }

配置:

 

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

<!--提供服务的类-->
            < object   id= " addSerivce "  type= " ServiceHost.AddService,ServiceHost "  singleton= " false " ></ object >

            < object  id= " serviceFactory "  type= " Spring.ServiceModel.Activation.ServiceHostFactoryObject,Spring.Services " >
                <property name= " TargetName "   value= " addSerivce " ></property>
            </ object >
            
        </objects>


<system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name= " DefaultBehavior " >
                    <serviceMetadata httpGetEnabled= " True " />
                    <serviceDebug includeExceptionDetailInFaults= " True " />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service  name= " addSerivce "  behaviorConfiguration= " DefaultBehavior " >
                <host>
                    <baseAddresses>
                        <add baseAddress= " http://127.0.0.1:6633/ " />
                    </baseAddresses>
                </host>
                <endpoint address= " AddService "  binding= " basicHttpBinding "  contract= " Contracts.IAdd " ></endpoint>
            </service>
        </services>
    </system.serviceModel>

 

为了寄宿服务,定义了一个继承自IFactoryObject的Spring.ServiceModel.Activation.ServiceHostFactoryObject

对象,并将它的TargetName属性指定为服务实现类(由于服务实现过于简单,就没有给出。服务的类名为:AddService,处

 于ServiceHost程序集下)。

<system.serviceModel>的配置与往常的区别是:<service name="addSerivce">。之前name指定为类的全局限

定名,即:程序集+类型名;使用Spring.Net的IoC后只需要将name设置为服务类型的定义的Id或者nane属性。

 

ServiceHostFactoryObject 创建一个与服务类型关联的Spring.ServiceModel.Activation.SpringServiceHost实例

来寄宿服务;而SpringServiceHost也通过Spring.Net进行配置。

 

 

 

2、服务寄宿:

 

ContextRegistry.GetContext(); 

Console.WriteLine( " Service is running... " );
Console.Read();

 

 

3、客户端配置:

<objects xmlns= " http://www.springframework.net "
         xmlns:wcf= " http://www.springframework.net/wcf " >
    <wcf:channelFactory channelType= " Contracts.IAdd,Contracts "  
                        endpointConfigurationName= " addService "  id= " addService " >
    </wcf:channelFactory>
</objects>

<spring>
        <context>
            <resource uri= " assembly://Client/Client.Config/WCFServiceConfig.xml " ></resource>
        </context>

    </spring>

    <system.serviceModel>

        <client>
            <endpoint address= " http://127.0.0.1:6633/AddService "  
                      binding= " basicHttpBinding "  
                      contract= " Contracts.IAdd "  name= " addService " ></endpoint>
        </client>
</system.serviceModel>

 

本例中,将此配置放在单独的配置文件WCFServiceConfig.xml中,在 App.Config文件中对他的引用进行申明。

 

提示:

1、在客户端配置中,对endpoint 配置的名称必须和wcf:channelFactory指定的id相同

2、引用.NET程序集内嵌资源时的URI语法:assembly://<AssemblyName>/<NameSpace>/<ResourceName>

3、希望配置中能有智能提示,请将Spring下的其他xsd文件拷贝到X:\Program Files\Microsoft Visual Studio 10.0\Xml\Schemas中

 

 

获取服务代理对象并调用服务:

方式1:

 

foreach  (IAdd proxy  in  ctx.GetObjectsOfType( typeof (IAdd)).Values) 

                {
                    Console.WriteLine( string .Format( " invocation result is :{0} " , proxy.Add( 1 ,  2 )));
                    (proxy  as  ICommunicationObject).Close();
                }

方式2:

 

IAdd proxy0 = ctx.GetObject( " addService " )  as  IAdd;               

Console.WriteLine( string .Format( " invocation result is :{0} " , proxy0.Add( 5 ,  2 ))); 
(proxy0  as  ICommunicationObject).Close();

 

运行结果:

服务端:

 

客户端端:

 

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

 

分类:  Spring.Net ,  WCF应用

作者: Leo_wl

    

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

    

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

版权信息

查看更多关于Spring.Net框架与WCF的集成1的详细内容...

  阅读:47次