Spring.NET对WCF(Windows Communication Foundation)有很好的支持,Spring.Services程序集下封装了创建和调用WCF的方法。以往,我们使用svc文件来承载WCF;使用自动生产的代理来调用服务。这样便对产生了诸多依赖。而使用Spring.NET则会令应用程序得到良好的改善。

  让我们从实例中学习今天的内容:

  

  首先创建服务契约和其实现

Contract

    [ServiceContract]
    public interface IWcfContract
    {
        [OperationContract]
        string GetData(int value);
    }

public class ImplementService : IWcfContract
    {
        public string GetData(int value)
        {
            return "你输入的是:" + value;
        }
    }

  把服务契约的实现类加入Spring.NET中管理

<object id="WcfServer" type="WcfSevice.ImplementService,WcfSevice"/>

  创建一个WCF的WCF项目,并引用Common.Logging.dll、Spring.Core.dll、Spring.Data.dll、Spring.Web.dll、Spring.Services.dll(在Spring.NET库下的3.0文件夹里)

  Spring.ServiceModel.Activation.ServiceHostFactory类继承自System.ServiceModel.Activation.ServiceHostFactory,用于在BS架构中承载WCF

  建立svc文件

<%@ ServiceHost Language="C#" Debug="true" Service="WcfServer" Factory="Spring.ServiceModel.Activation.ServiceHostFactory"%>

指定Service属性为服务契约的实现类(WcfServer)

  配置web.config文件

代码

<system.serviceModel>
        <services>
            <service name="WcfServer" behaviorConfiguration="WcfServerBehavior">
                <!-- Service Endpoints -->
                <endpoint address="" binding="wsHttpBinding" contract="IContract.IWcfContract"/>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WcfServerBehavior">
                    <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点-->
                    <serviceMetadata httpGetEnabled="true"/>
                    <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息-->
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>

指定Service节点的name属性为刚才配置的Spring.NET对象(WcfServer)。

在Global的Application_Start方法中实例化Spring.NET对象(Spring.Context.Support.ContextRegistry.GetContext();)

这样WEB宿主的WCF就搭建成功了。

而在winform或者控制台等程序中无法使用svc文件来承载WCF,但这一点被Spring.NET开发团队考虑到了。Spring.Services程序集下的Spring.ServiceModel.Activation.ServiceHostFactoryObject则是用于非WEB环境的WCF创建。

我们以控制台程序为例,来讲解在非WEB环境的WCF创建:

在控制台程序中创建一项Spring.NET对象的配置

  <object id="WcfServerHost" type="Spring.ServiceModel.Activation.ServiceHostFactoryObject, Spring.Services">
    <property name="TargetName" value="WcfServer" />
  </object>

指明TargetName属性为实现服务契约的Spring.NET对象(WcfServer)

配置app.config文件,这里为了区别WEB环境,我使用netTcpBinding的绑定方式。

代码

<system.serviceModel>

<behaviors>
      <serviceBehaviors>
        <behavior name="WcfServerBehavior">
          <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点-->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息-->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

<services>

<service name="WcfServer" behaviorConfiguration="WcfServerBehavior">
        <endpoint binding="netTcpBinding" contract="IContract.IWcfContract"
                  bindingConfiguration="ServerBinding"
                  address="net.tcp://localhost:3286/App/Server/"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:3285/App/Server/"/>
          </baseAddresses>
        </host>
      </service>
    
    </services>
   
    <bindings>
      
      <netTcpBinding>
        <binding name="ServerBinding">
          <security mode="None">
            <transport clientCredentialType="None" />
            <message clientCredentialType="None" />
          </security>
        </binding>
      </netTcpBinding>
      
    </bindings>
    
  </system.serviceModel>

指明baseAddress是为了便于生成WCF的代理类。

最后在控制台程序的Main方法中实例化Spring.NET容器,这样一个以控制台为宿主的WCF环境便搭建成功了。

  然而对于调用WCF,我们通常使用“添加服务应用”的方式来生成WCF的代理类。这样,在整个项目都使用Spring.NET框架的环境下,很难管理WCF的代理类。

为了让Spring.NET来管理WCF的代理类,我们需要配置System.ServiceModel.ChannelFactory<T>这个泛型类,再配置代理类中的factory-method属性。
至于System.ServiceModel.ChannelFactory<T>类,是用来创建和管理客户端用来将消息发送到服务终结点的通道,这里不细讲,请查看MSDN。

Client

<!--创建web宿主的信道工厂-->
  <object id="WebChannelFactory"
      type="System.ServiceModel.ChannelFactory&lt;IContract.IWcfContract>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="WSHttpBinding_IContract" />
  </object>

<!--调用web宿主的代理类-->
  <object id="WebProxy"
        type="IContract.IWcfContract, IContract"
        factory-object="WebChannelFactory"
        factory-method="CreateChannel" singleton="false" />

<!--创建app宿主的信道工厂-->
  <object id="AppChannelFactory"
    type="System.ServiceModel.ChannelFactory&lt;IContract.IWcfContract>, System.ServiceModel">
    <constructor-arg name="endpointConfigurationName" value="NetTcpBinding_IContract" />
  </object>

<!--调用app宿主的代理类-->
  <object id="AppProxy"
        type="IContract.IWcfContract, IContract"
        factory-object="AppChannelFactory"
        factory-method="CreateChannel"/>

注意的是System.ServiceModel.ChannelFactory<T>的构造函数中需要指明endpointConfigurationName属性为endpoint的name属性

app.config

    <client>
      <endpoint address="http://localhost:3287/WebHost.svc" binding="wsHttpBinding"
          bindingConfiguration="WSHttpBinding_IContract" contract="IContract.IWcfContract"
          name="WSHttpBinding_IContract"/>

<endpoint address="net.tcp://localhost:3286/App/Server/" binding="netTcpBinding"
            bindingConfiguration="NetTcpBinding_IContract" contract="IContract.IWcfContract"
            name="NetTcpBinding_IContract"/>
    </client>

客户端中调用WCF代理类

Program

class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext cxt = ContextRegistry.GetContext();
            
            //调用web宿主
            IWcfContract webProxy = (IWcfContract)cxt.GetObject("WebProxy");
            Console.WriteLine(webProxy.GetData(1));

//调用app宿主
            IWcfContract appProxy = (IWcfContract)cxt.GetObject("AppProxy");
            Console.WriteLine(appProxy.GetData(2));

Console.ReadLine();
        }
    }

程序运行的效果:

代码下载

Spring.NET学习笔记22——整合WCF(应用篇) Level 200相关推荐

  1. Spring.NET学习笔记1——控制反转(基础篇) Level 200

    在学习Spring.NET这个控制反转(IoC)和面向切面(AOP)的容器框架之前,我们先来看一下什么是控制反转(IoC). 控制反转(Inversion of Control,英文缩写为IoC),也 ...

  2. Spring.NET学习笔记——目录(原)

    目录 前言 Spring.NET学习笔记--前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1--控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  3. spring学习笔记06-spring整合junit(出现的问题,解决的思路)

    spring学习笔记06-spring整合junit(出现的问题,解决的思路) 文章目录 spring学习笔记06-spring整合junit(出现的问题,解决的思路) 3.1测试类中的问题和解决思路 ...

  4. Spring.NET学习笔记——前言

    Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序.它提供了很多方面的功能,比如依赖注入.面向方面编程(AOP).数据访问抽象及ASP.NET扩展等等.Sprin ...

  5. Spring Boot学习笔记-基础(2)

    Spring Boot学习笔记-基础(2) Spring Boot 优点: – 快速创建独立运行的Spring项目以及与主流框架集成 – 使用嵌入式的Servlet容器,应用无需打成WAR包 – st ...

  6. Spring Boot学习笔记(1)

    文章目录 Spring Boot学习笔记(1) Spring Boot 整合 JSP Spring Boot HTML Thymeleaf 常用语法 Spring Boot 数据校验 Spring B ...

  7. Spring Cloud 学习笔记(3 3)

    Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(2 / 3) - - - 108_Nacos之Linux版本安装 109_Nacos集群配置(上) 110_Nac ...

  8. Spring Cloud 学习笔记(3 / 3)

    Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(2 / 3) - - - 108_Nacos之Linux版本安装 109_Nacos集群配置(上) 110_Nac ...

  9. Spring MVC 学习笔记 对locale和theme的支持

    Spring MVC 学习笔记 对locale和theme的支持 Locale Spring MVC缺省使用AcceptHeaderLocaleResolver来根据request header中的 ...

最新文章

  1. html导入excel文件,使用js-xlsx简单实现一个导入excel
  2. wsl安装配置vscode(亲测有用)
  3. ubuntu mysql备份_Ubuntu下进行MYSQL自动备份
  4. 利用Cydia Substrate进行Android HOOK(二)
  5. SpringSecurity系列(四) Spring Security 实现权限树形菜单
  6. POSTFIX服务简介
  7. 如何使用高大上的方法调参数
  8. 谷歌发布最强AI机器人AlphaGo Zero,融360拟融资3亿美元即将赴美上市 | 大数据周周看
  9. pycharm2019+破解补丁
  10. XiaoZi's CrackMe
  11. 【ZZ】从入门到高阶,你需要刷哪些书?丨高中物理竞赛辅导书推荐
  12. FPGA数字信号处理(25)数字相关器设计(经典结构)
  13. echarts修改鼠标悬浮样式和默认高亮效果,和传值高亮修改字体模糊
  14. GB28181国际标准监控对接web平台搭建
  15. 为员工营造宽松的工作氛围能提高工作效率
  16. 科技企业上市就是一大悲剧(下)
  17. 钉钉爆火背后,真正的在线教育机构过得怎么样?已有 1 家倒闭
  18. python 处理行列数据集数据_数据预处理(python)
  19. 《数据结构》实验报告(四)——二叉树的遍历及相关应用
  20. Comparison of Big Data OLAP DB : ClickHouse, Druid, and Pinot

热门文章

  1. HDU 5781 ATM Mechine(概率dp)
  2. dbms_stats包更新、导出、导入、锁定统计信息
  3. 设计模式之笔记--简单工厂模式(Simple Factory)
  4. Tomcat JVM 初始化加大内存
  5. csdn的blog后台程序的导航菜单的实现
  6. 8种主流深度学习框架介绍
  7. 我的世界python俄罗斯方块手机版下载_欢乐俄罗斯方块
  8. 突发!联想被责令立即开展全面整改
  9. session、token、jwt、oauth2 傻傻分不清
  10. Jeewx-Enterprise_1.1版本发布,开源微信企业号开发平台