Spring.NET学习笔记22——整合WCF(应用篇) Level 200
Spring.NET对WCF(Windows Communication Foundation)有很好的支持,Spring.Services程序集下封装了创建和调用WCF的方法。以往,我们使用svc文件来承载WCF;使用自动生产的代理来调用服务。这样便对产生了诸多依赖。而使用Spring.NET则会令应用程序得到良好的改善。
让我们从实例中学习今天的内容:
首先创建服务契约和其实现
![](/assets/blank.gif)
![](/assets/blank.gif)
public interface IWcfContract
{
[OperationContract]
string GetData(int value);
}
public class ImplementService : IWcfContract
{
public string GetData(int value)
{
return "你输入的是:" + value;
}
}
把服务契约的实现类加入Spring.NET中管理
创建一个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文件
指定Service属性为服务契约的实现类(WcfServer)
配置web.config文件
![](/assets/blank.gif)
![](/assets/blank.gif)
<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对象的配置
<property name="TargetName" value="WcfServer" />
</object>
指明TargetName属性为实现服务契约的Spring.NET对象(WcfServer)
配置app.config文件,这里为了区别WEB环境,我使用netTcpBinding的绑定方式。
![](/assets/blank.gif)
![](/assets/blank.gif)
<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。
![](/assets/blank.gif)
![](/assets/blank.gif)
<object id="WebChannelFactory"
type="System.ServiceModel.ChannelFactory<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<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属性
![](/assets/blank.gif)
![](/assets/blank.gif)
<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代理类
![](/assets/blank.gif)
![](/assets/blank.gif)
{
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相关推荐
- Spring.NET学习笔记1——控制反转(基础篇) Level 200
在学习Spring.NET这个控制反转(IoC)和面向切面(AOP)的容器框架之前,我们先来看一下什么是控制反转(IoC). 控制反转(Inversion of Control,英文缩写为IoC),也 ...
- Spring.NET学习笔记——目录(原)
目录 前言 Spring.NET学习笔记--前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1--控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- spring学习笔记06-spring整合junit(出现的问题,解决的思路)
spring学习笔记06-spring整合junit(出现的问题,解决的思路) 文章目录 spring学习笔记06-spring整合junit(出现的问题,解决的思路) 3.1测试类中的问题和解决思路 ...
- Spring.NET学习笔记——前言
Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序.它提供了很多方面的功能,比如依赖注入.面向方面编程(AOP).数据访问抽象及ASP.NET扩展等等.Sprin ...
- Spring Boot学习笔记-基础(2)
Spring Boot学习笔记-基础(2) Spring Boot 优点: – 快速创建独立运行的Spring项目以及与主流框架集成 – 使用嵌入式的Servlet容器,应用无需打成WAR包 – st ...
- Spring Boot学习笔记(1)
文章目录 Spring Boot学习笔记(1) Spring Boot 整合 JSP Spring Boot HTML Thymeleaf 常用语法 Spring Boot 数据校验 Spring B ...
- Spring Cloud 学习笔记(3 3)
Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(2 / 3) - - - 108_Nacos之Linux版本安装 109_Nacos集群配置(上) 110_Nac ...
- Spring Cloud 学习笔记(3 / 3)
Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(2 / 3) - - - 108_Nacos之Linux版本安装 109_Nacos集群配置(上) 110_Nac ...
- Spring MVC 学习笔记 对locale和theme的支持
Spring MVC 学习笔记 对locale和theme的支持 Locale Spring MVC缺省使用AcceptHeaderLocaleResolver来根据request header中的 ...
最新文章
- div css导航栏设计,CSS+DIV设计实例:超酷的竖排导航栏
- mysql 事务值被改变_面试被问MySQL 事务的实现原理,怎么破?
- mysql左连接去重查询_MYSQL-多表查询
- Linux下kafka的部署
- Android Studio 解决The SDK platform-tools is too old问题
- timm 视觉库中的 create_model 函数详解
- 女儿是程序员爸爸的小棉袄,礼物太暖心
- opencv 读取、显示、保存视频
- Python爬虫编程实践Task 01
- Python数据库 -- SQL语句的语法问题(即对于字符串的特殊处理)
- linux查看进程加载的jar包,[Linux] 查看jar包内容
- vsftpd配置详解
- gomarket服务器位置,V712双核版机型常见问题集解答.pdf
- UDP用户数据报协议分析
- 039.并发编程之线程
- V831 MP4播放+船新版本
- Centos(Linux)系统备份与还原
- 大数据战略上升为国家战略,悄悄告诉你大数据大牛都在读的8本书
- 在浏览器中输入网址到网页展现全部过程
- python读取dat文件经纬度_自动提取kml文件中的经纬度