WCF 学习笔记之双工实现
2013-07-05 23:30
399 查看
其中 Client 和Service为控制台程序 Service.Interface为类库
首先了解契约Interface两个接口
using System.ServiceModel; using System.ServiceModel.Channels; namespace Artech.WcfServices.Service.Interface { [ServiceContract(Namespace = "http://www.artech.com/", CallbackContract = typeof(ICalculatorCallback))] public interface ICalculator { [OperationContract(IsOneWay = true)] void Add(double x, double y); } }这边要注意两个地方:一个是明确回调查的接口,和设置让它为单向模式[CallbackContract = typeof(ICalculatorCallback)];IsOneWay = true
回调接口的代码如下:
using System.ServiceModel; namespace Artech.WcfServices.Service.Interface { public interface ICalculatorCallback { [OperationContract(IsOneWay = true)] void DisplayResult(double result, double x, double y); } }此处也有两个地方要注意:回调契约没有定义[ServiceContact]的特性是因为在指定CallbackContract属性时就隐含对应的接口是个服务契约;此外同样也要设置为单向IsOneWay = true
接下来是服务实现层的代码:
using System.ServiceModel; using Artech.WcfServices.Service.Interface; using System.Threading; namespace Artech.WcfServices.Service { public class CalculatorService : ICalculator { public void Add(double x, double y) { double result = x + y; ICalculatorCallback callback = OperationContext.Current.GetCallbackChannel<ICalculatorCallback>(); callback.DisplayResult(result, x, y); } } }相对应的App.config配置
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="exposeExceptionDetail"> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="Artech.WcfServices.Service.CalculatorService" behaviorConfiguration="exposeExceptionDetail"> <endpoint address="net.tcp://127.0.0.1:3721/calculatorservice" binding="netTcpBinding" contract="Artech.WcfServices.Service.Interface.ICalculator"/> </service> </services> </system.serviceModel> </configuration>关于绑定类型的选择必须明确是支持双向通信的类型才可以;比如NetTcpBinding,WSDualHttpBinding ;这边应该注意在采用WSDualHttpBinding时要把可靠会话打开,因为它是通过可靠会话维护两个HTTP通道之间的匹配;
例如:<reliableSession enabled="true"/>
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <netTcpBinding> <binding name="reliableSessionBinding"> <reliableSession enabled="true"/> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="exposeExceptionDetail"> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="Artech.WcfServices.Service.CalculatorService" behaviorConfiguration="exposeExceptionDetail"> <endpoint address="net.tcp://127.0.0.1:3721/calculatorservice" binding="netTcpBinding" bindingConfiguration="reliableSessionBinding" contract="Artech.WcfServices.Service.Interface.ICalculator"/> </service> </services> </system.serviceModel> </configuration>服务层还有一个代码就是宿主打开
using System.ServiceModel; using Artech.WcfServices.Service.Interface; using System.Threading; using System.ServiceModel.Description; namespace Artech.WcfServices.Service { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(CalculatorService))) { host.Open(); Console.Read(); } } } }接下来是客户端的实现,要实现回调接口的
using Artech.WcfServices.Service.Interface; namespace Artech.WcfServices.Client { public class CalculatorCallbackService : ICalculatorCallback { public void DisplayResult(double result, double x, double y) { Console.WriteLine("x + y = {2} when x = {0} and y = {1}", x, y, result); } } }以及相对应的配置文件内容:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint name ="calculatorservice" address="net.tcp://127.0.0.1:3721/calculatorservice" binding="netTcpBinding" contract="Artech.WcfServices.Service.Interface.ICalculator"/> </client> </system.serviceModel> </configuration>
以及运行的代码:
using System.ServiceModel; using Artech.WcfServices.Service.Interface; using System.ServiceModel.Channels; using System.Threading; namespace Artech.WcfServices.Client { class Program { static void Main(string[] args) { InstanceContext callback = new InstanceContext(new CalculatorCallbackService()); using (DuplexChannelFactory<ICalculator> channelFactory = new DuplexChannelFactory<ICalculator>(callback, "calculatorservice")) { ICalculator calculator = channelFactory.CreateChannel(); calculator.Add(1, 2); } Console.Read(); } } }
针对TCP协议要把服务器的相对应服务打开;或者可能会报错;
错误一:TransportManager 无法使用 NetTcpPortSharing 服务侦听提供的 URI: 无法启动服务,因为该服务已禁用。管理员运行 "sc.exe config NetTcpPortSharing start= demand" 可以将其启用 [NetTcpPortSharing 服务没有启动]
错误二:TransportManager 无法使用 NetTcpPortSharing 服务侦听提供的 URI: 服务侦听失败 [端口被占用,换个端口]
相关文章推荐
- WCF 学习笔记之双工实现
- WCF 学习笔记之双工实现
- silverlight:wcf双工通讯学习笔记
- silverlight之wcf双工通讯学习笔记
- WCF学习笔记(五)契约双工通信、多契约绑定及对数据库增删改查等综合实例
- Silverlight学习笔记三:WCF跨域调用,实现登陆页
- iOS学习笔记-- tableView单选的实现
- hadoop学习笔记(五)——全分布模式下SSH免密码登陆的实现
- Java 代理学习笔记 —— 浅析Cglib动态代理实现
- cocos2d-x学习笔记(二)Eclipse+cdt实现cocos2dx跨平台解决方案(转)
- WCF 双工开发笔记
- ArcGIS实现矢量数据的拼接(学习笔记)
- OpenGLES入门笔记:Rajawali学习(4)物体点击事件的实现
- 学习session实现购物车笔记
- 生成对抗网络学习笔记5----DCGAN(unsupervised representation learning with deep convolutional generative adv)的实现
- 有关集合算法的实现一些学习笔记
- Bigpipe学习笔记和java版实现
- ITCAST视频-Spring学习笔记(编码剖析@Resource注解的实现原理)
- IOS学习笔记(4)——自定义相机的实现
- WCF学习笔记(三):开启net.tcp端口