契约:

    [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做回调操作的更多相关文章

  1. [WCF编程]10.操作:回调操作

    一.回调操作概述 WCF支持服务将调用返回给它的客户端.在回调期间,许多方面都将颠倒过来:服务将成为客户端,客户端将编程服务.回调操作可以用在各种场景和应用程序中,但在涉及事件或者服务发生时间需要通知 ...

  2. 重温WCF之数单向通讯、双向通讯、回调操作(五)

    一.单向通讯单向操作不等同于异步操作,单向操作只是在发出调用的瞬间阻塞客户端,但如果发出多个单向调用,WCF会将请求调用放入到服务器端的队列中,并在某个时间进行执行.队列的存储个数有限,一旦发出的调用 ...

  3. 跟我一起学WCF(9)——WCF回调操作的实现

    一.引言 在上一篇文章中介绍了WCF对Session的支持,在这篇文章中将详细介绍WCF支持的操作.在WCF中,除了支持经典的请求/应答模式外,还提供了对单向操作.双向回调操作模式的支持,此外还有流操 ...

  4. WCF分布式开发步步为赢(10):请求应答(Request-Reply)、单向操作(One-Way)、回调操作(Call Back).

    WCF除了支持经典的请求应答(Request-Reply)模式外,还提供了什么操作调用模式,他们有什么不同以及我们如何在开发中使用这些操作调用模式.今天本节文章里会详细介绍.WCF分布式开发步步为赢( ...

  5. WCF 采用net.tcp协议

    WCF 采用net.tcp协议实践   概述 与Socket相比,WCF真是爽得不得了,其基本指导思想为SOA——面向服务. 其基本配置在于ABC(Address,Binding,Contract), ...

  6. Restful风格API中用put还是post做新增操作有什么区别?

    Restful风格API中用put还是post做新增操作有什么区别? 转 头条面试归来,有些话想和Java开发者说!>>> 这个是华为面试官问我的问题,回来我找了很多资料,想验证这个 ...

  7. 关于有默认值的字段在用EF做插入操作时的思考(续)

    问题描述 今天下午(看现在这时间,应该是昨天下午了哈),园友 choon 写了这样一篇博文<关于有默认值的字段在用EF做插入操作时的思考>. 博文内容主要记录的是 choon 使用 EF ...

  8. 关于有默认值的字段在用EF做插入操作时的思考

    今天在用EF做插入操作的时候发现数据库中一个datetime类型的字段(CreateDate)的值居然全部为null.于是赶紧看表结构发现CreateDate字段居然是允许为空的. 虽然为空,但是设置 ...

  9. Entity Framework 的小实例:在项目中添加一个实体类,并做插入操作

    Entity Framework 的小实例:在项目中添加一个实体类,并做插入操作 1>. 创建一个控制台程序2>. 添加一个 ADO.NET实体数据模型,选择对应的数据库与表(Studen ...

随机推荐

  1. android 中解析json格式数据

    本文来自http://tonysun3544.iteye.com/category/188238 package com.tony.json; import android.app.Activity; ...

  2. 【嵌入式】——makefiles

    汇编通用makefile: 命令行编辑: 编译 arm-linux-as -march=armv5te -o led.o led.s -march 指定的指令集的版本 指定架构 连接 arm-linu ...

  3. 【oneday_onepage】—— 日常用语

    what do you do for living? 一般用在问对方的工作.如果直接说“what is your job?”会显得有点生硬了. i was wondering if you can t ...

  4. Spring cloud系列十四 分布式链路监控Spring Cloud Sleuth

    1. 概述 Spring Cloud Sleuth实现对Spring cloud 分布式链路监控 本文介绍了和Sleuth相关的内容,主要内容如下: Spring Cloud Sleuth中的重要术语 ...

  5. linux下安装redis并配置

    redis官网上给出了安装步骤,这里做一下总结. 1.Download, extract and compile Redis with: wget http://download.redis.io/r ...

  6. webrtc 源码结构

    api WebRTC 接口层.包括 DataChannel, MediaStream, SDP相关的接口.各浏览器都是通过该接口层调用的 WebRTC. call 存放的是 WebRTC “呼叫(Ca ...

  7. 企业私有云部署im,视频服务

    1,安全问题 2,员工跨地域 3,内部视频培训 考勤申请,设备借用申请 名片申请 会议室预订 审批 内网,局域网部署 Android源码 https://github.com/starrtc/andr ...

  8. linux之sshfs

    1.挂载 sshfs -p shiyu@ml.cs.tsinghua.edu.cn:/mfs/shiyu/ ~/mfs 2.卸载 fusermount -u ~/mfs

  9. python 读写二进制文件实例

    本程序,首先写入一个矩阵到二进制文件中,然后读取二进制文件恢复到另外一个矩阵中. #coding:utf--8 #https://www.cnblogs.com/cmnz/p/6986979.html ...

  10. mysql insert exists || mysql 判断数据是否存在

    情景如下: "今日前端忽然说句, 我需要做个判断, 不能重复收藏, 我犹如颈有寒冰不寒而栗, 于是思考我该怎么做?为什么她都思考到了我没有思考到这是我的工作啊" 思考后得到三种解决 ...