Wcf使用Net.Tcp做回调操作
契约:
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode = SessionMode.Required,
CallbackContract = typeof(ITransferCallback))] 如果要使用回调,必须加上这句话
public interface IMonitorService
{
[OperationContract(IsOneWay = true)]
void MonitorOnlineService(string strJson);
}
[ServiceContract(CallbackContract = typeof(IMonitorService))]
public interface ITransferCallback
{
[OperationContract(IsOneWay = true)]
void ReturnResult(string strJson);
}
server1
接口实现
public class TransferCallback : ITransferCallback
{
public void ReturnResult(string strJson)
{
Console.WriteLine("start service");
Monitor.MonitorOnlineService(strJson);
} private IMonitorService Monitor {
get
{
return OperationContext.Current.GetCallbackChannel<IMonitorService>();
}
} }
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup> <system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior0">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors> <services>
<service name="WcfServer.TransferCallback">
<endpoint address="net.tcp://127.0.0.1:8000/TransferCallback"
binding="netTcpBinding" bindingConfiguration="" contract="WcfServer.ITransferCallback" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://127.0.0.1:8000/TransferCallback" />
</baseAddresses>
</host>
</service>
</services> <bindings>
<basicHttpBinding>
<binding name="basicHttpBind" closeTimeout="00:10:00" openTimeout="00:05:00"
receiveTimeout="00:10:00" sendTimeout="00:05:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="" maxBufferSize="" maxReceivedMessageSize=""
textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"
messageEncoding="Text">
<readerQuotas maxStringContentLength="" />
<security mode="None" />
</binding>
</basicHttpBinding>
<netTcpBinding>
<binding name="TransferNetTcpBind" closeTimeout="00:10:00" openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" maxBufferPoolSize="" maxReceivedMessageSize="" maxBufferSize="">
<readerQuotas maxStringContentLength=""/>
<security mode="None"></security>
</binding>
<binding name="NetTcpBinding_ITransferCallback" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog=""
maxBufferPoolSize="" maxBufferSize="" maxConnections=""
maxReceivedMessageSize="">
<readerQuotas maxDepth="" maxStringContentLength="" maxArrayLength=""
maxBytesPerRead="" maxNameTableCharCount="" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings> <client>
<endpoint address="net.tcp://127.0.0.1:9000/MonitorService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ITransferCallback"
contract="IMonitorService" name="NetTcpBinding_ITransferCallback">
</endpoint> </client>
</system.serviceModel> </configuration>
启用服务
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(DataTransferCallback)))
{
ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null)
host.Description.Behaviors.Add(new ServiceMetadataBehavior()); //暴露出元数据,以便能够让SvcUtil.exe自动生成配置文件
host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex"); //开启服务
host.Open();
Console.WriteLine("Service listen begin to listen...");
Console.WriteLine("press any key to teriminate...");
Console.ReadKey();
host.Abort();
host.Close();
}
}
}
Wcfserver2
public class MonitorService : IMonitorService
{
#region
/// <summary>
/// 写入集成控制平台数据
/// </summary>
/// <param name="strJson">JSON字符串</param>
/// <param name="strOrgCode">直属库编号</param>
/// <param name="strTableName">表明</param>
/// <returns></returns>
public void MonitorOnlineService(string strJson)
{Callback.ReturnResult(result);
} #endregion
ITransferCallback Callback
{
get
{
return OperationContext.Current.GetCallbackChannel<ITransferCallback>();
}
}
}
调用服务
new ChannelFactory<IDataTransferCallback>("NetTcpBinding_ITransferCallback").CreateChannel().ReturnResult("Hello");
<system.serviceModel>
<client>
<endpoint address="net.tcp://127.0.0.1:8000/TransferCallback"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ITransferCallback"
contract="ITransferCallback" name="NetTcpBinding_ITransferCallback">
</endpoint>
</client>
<services>
<service name="MonitorService">
<host>
<baseAddresses>
<add baseAddress="net.tcp://127.0.0.1:9000/MonitorService" />
</baseAddresses>
</host>
<endpoint address="net.tcp://127.0.0.1:9000/MonitorService" binding="netTcpBinding" bindingConfiguration="TransferNetTcpBind"
contract="IMonitorService"/>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="TransferNetTcpBind" closeTimeout="00:10:00" openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" maxBufferPoolSize="" maxReceivedMessageSize="" maxBufferSize="">
<readerQuotas maxStringContentLength=""/>
<security mode="None"></security>
</binding>
<binding name="NetTcpBinding_ITransferCallback" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog=""
maxBufferPoolSize="" maxBufferSize="" maxConnections=""
maxReceivedMessageSize="">
<readerQuotas maxDepth="" maxStringContentLength="" maxArrayLength=""
maxBytesPerRead="" maxNameTableCharCount="" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="False"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
这样服务就可以循环交互了,直到一个服务停止为止
Wcf使用Net.Tcp做回调操作的更多相关文章
- [WCF编程]10.操作:回调操作
一.回调操作概述 WCF支持服务将调用返回给它的客户端.在回调期间,许多方面都将颠倒过来:服务将成为客户端,客户端将编程服务.回调操作可以用在各种场景和应用程序中,但在涉及事件或者服务发生时间需要通知 ...
- 重温WCF之数单向通讯、双向通讯、回调操作(五)
一.单向通讯单向操作不等同于异步操作,单向操作只是在发出调用的瞬间阻塞客户端,但如果发出多个单向调用,WCF会将请求调用放入到服务器端的队列中,并在某个时间进行执行.队列的存储个数有限,一旦发出的调用 ...
- 跟我一起学WCF(9)——WCF回调操作的实现
一.引言 在上一篇文章中介绍了WCF对Session的支持,在这篇文章中将详细介绍WCF支持的操作.在WCF中,除了支持经典的请求/应答模式外,还提供了对单向操作.双向回调操作模式的支持,此外还有流操 ...
- WCF分布式开发步步为赢(10):请求应答(Request-Reply)、单向操作(One-Way)、回调操作(Call Back).
WCF除了支持经典的请求应答(Request-Reply)模式外,还提供了什么操作调用模式,他们有什么不同以及我们如何在开发中使用这些操作调用模式.今天本节文章里会详细介绍.WCF分布式开发步步为赢( ...
- WCF 采用net.tcp协议
WCF 采用net.tcp协议实践 概述 与Socket相比,WCF真是爽得不得了,其基本指导思想为SOA——面向服务. 其基本配置在于ABC(Address,Binding,Contract), ...
- Restful风格API中用put还是post做新增操作有什么区别?
Restful风格API中用put还是post做新增操作有什么区别? 转 头条面试归来,有些话想和Java开发者说!>>> 这个是华为面试官问我的问题,回来我找了很多资料,想验证这个 ...
- 关于有默认值的字段在用EF做插入操作时的思考(续)
问题描述 今天下午(看现在这时间,应该是昨天下午了哈),园友 choon 写了这样一篇博文<关于有默认值的字段在用EF做插入操作时的思考>. 博文内容主要记录的是 choon 使用 EF ...
- 关于有默认值的字段在用EF做插入操作时的思考
今天在用EF做插入操作的时候发现数据库中一个datetime类型的字段(CreateDate)的值居然全部为null.于是赶紧看表结构发现CreateDate字段居然是允许为空的. 虽然为空,但是设置 ...
- Entity Framework 的小实例:在项目中添加一个实体类,并做插入操作
Entity Framework 的小实例:在项目中添加一个实体类,并做插入操作 1>. 创建一个控制台程序2>. 添加一个 ADO.NET实体数据模型,选择对应的数据库与表(Studen ...
随机推荐
- linux源配置
阿里云源配置官网:http://mirrors.aliyun.com 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS ...
- substance新版及问题
新版地址:https://github.com/Insubstantial,目前是7.3版 http://stackoverflow.com/questions/3657538/substance-u ...
- Android安装后没有完成和打开按钮
File apkFile = new File(filePath); Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVIT ...
- Java中的Set与List 的关系与区别
两个接口都是继承自Collection. List (inteface) 次序是List 的最重要特点,它确保维护元素特定的顺序. --ArrayList 允许对元素快速随机访问. --LinkedL ...
- MPAndroidChart的具体属性方法
android中常用的第三方图表MPAndroidChart的一些具体属性及方法说明 注意:在将折线图转为曲线图时,lineDataSet.setMode(LineDataSet.Mode.CUBIC ...
- charles系列破解激活办法(最高charles4.2.5都可以激活,亲测可用)
Registered Name: https://zhile.io License Key: 48891cf209c6d32bf4 抓包工具Charles的使用心得 https://www.jians ...
- 《Qt数据类型》--QByteArray,QString,int,hex之间的转化
对于QString和QByteArray,他们都有一个toInt的静态函数,QString::toInt()是根据string的字面值转化为int类型,比如string:"123" ...
- 从Container内存监控限制到CPU使用率限制方案
转自:http://blog.csdn.net/Androidlushangderen/article/details/50282593 前言 最近在运维我们部门的hadoop集群时,发现了很多Job ...
- USB2.0学习笔记连载(六):USB2.0硬件设计需要注意事项
笔者在设计USB2.0时找到了一个官方给的硬件设计正确设计指南,其中有些内容还挺nice的.不单单只是USB的设计,其中有些思想可以应用到其他的场合中. 对于USB2.0而言,全速状态下可以达到480 ...
- 珍藏40个android应用源码分享
1.高仿京东商城源码http://www.apkbus.com/android-115203-1-1.html 2.抽屉demohttp://www.apkbus.com/android-115205 ...