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")]

     publicinterface 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(12)));
                    (proxy as ICommunicationObject).Close();
                }

复制代码
方式2:
 
IAdd proxy0 = ctx.GetObject("addService"as IAdd;               

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

 
运行结果:
服务端:
 

客户端端:

 
 
原文地址:https://www.cnblogs.com/Leo_wl/p/2561310.html