wcf服务端

1,新建一个“windows窗口程序”名称为WCFServer2。

2。然后加入一个“WCF服务”名称为Service1。

详细步骤为:解决方式试图中,选中“WCFServer2”项目,右键,在弹出菜单中选择“加入->新建项”。

3,双击主窗口,在它的Load事件中编写代码启动wcf服务:BasicHttpBinding方式启动wcf服务。此文件代码例如以下:

using System.ServiceModel;

using System.ServiceModel.Description;

//…

ServiceHost m_ServiceHost;

//…

private
void Form1_Load(object sender,
EventArgse)

{

//NetTcpBinding方式启动wcf服务

m_ServiceHost = new
ServiceHost(typeof(Service1));//Service1是wcf服务的类名称

NetTcpBindingbinding =
new NetTcpBinding();

UribaseAddress =
new Uri(string.Format("net.tcp://localhost:10086/WCFHostServer/Service1"));

m_ServiceHost.AddServiceEndpoint(typeof(IService1),binding, baseAddress);

m_ServiceHost.Open();

}

4,在 IService1.cs中添加一个方法Init,同一时候添加ISvrToCliCallBack接口.

namespace WCFServer2

{

// 注意:
使用“重构”菜单上的“重命名”命令,能够同一时候更改代码和配置文件里的接口名“IService1”。

[ServiceContract(CallbackContract=
typeof(ISvrToCliCallBack))]

public
interface IService1

{

[OperationContract]

voidDoWork();

[OperationContract]

voidInit();

}

public
interface ISvrToCliCallBack

{

[OperationContract(IsOneWay=
true)]

voidNotifyClientMsg(string devStateInfo);

}

}

5, 在Service1.cs中实现接口的方法Init初始化。

public
static ISvrToCliCallBack userCallBack;

//...

public
void Init()

{

userCallBack = OperationContext.Current.GetCallbackChannel<ISvrToCliCallBack>();

}

6,主窗体添加button。

private
voidbutton1_Click(object sender,
EventArgs e)

{

Service1.userCallBack.NotifyClientMsg("服务端给client通知啦");

}

7。改动app.config,WCFServer2.Service1的binding由"wsHttpBinding"改成"wsDualHttpBinding"

客服端

1,新建一个“windows窗口程序”名称为WCFClient2。

2,添加服务引用。

服务引用地址到服务端的app.config查看,就是baseAddress。

3.在FormClient中继承接口IService1Callback,实现方法NotifyClientMsg

4。主窗体启动是初始化。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using WCFClient2.ServiceReference1;

using System.ServiceModel;

namespace WCFClient2

{

public
partial class
Form1 : Form,
IService1Callback

{

publicForm1()

{

InitializeComponent();

}

public
void NotifyClientMsg(stringdevStateInfo)

{

MessageBox.Show(devStateInfo,"ddd");

}

IService1m_Innerclient;

privatevoid Form1_Load(objectsender,
EventArgs e)

{

InstanceContextm_CallBackContext;

m_CallBackContext = new
InstanceContext(this);

DuplexChannelFactory<IService1> m_ChannelFactory;

NetTcpBindingbinding =
new NetTcpBinding();

stringstrUrl =
string.Format("net.tcp://{0}:{1}/WCFHostServer/Service1",

"localhost",10085);

UribaseAddress =
new Uri(strUrl);

m_ChannelFactory = new
DuplexChannelFactory<IService1>(m_CallBackContext, binding,
new EndpointAddress(baseAddress));

m_Innerclient =m_ChannelFactory.CreateChannel();

Service1Clienthost =
new ServiceReference1.Service1Client(m_CallBackContext);

host.Init();//调用GetSvrTime获取到wcfserver上的时间

}

}

}

WCF服务端调用client.的更多相关文章

  1. WCF服务端开发和客户端引用小结

    1.服务端开发 1.1 WCF服务创建方式 创建一个WCF服务,总是会创建一个服务接口和一个服务接口实现.通常根据服务宿主的不同,有两种创建方式. (1)创建WCF应用程序 通过创建WCF服务应用程序 ...

  2. wcf第三方客户端与wcf服务之间调用入门

    Wcf服务与我们的客户端如何建立联系的呢.本文简单记录一下 1.创建我们的wcf服务程序. 第一个wcf服务库是创建我们的wcf库,运行时会单独来托管我们的程序,而非托管在iis下. 第二个wcf服务 ...

  3. WCF 服务端异常封装

    通常WCF服务端异常的详细信息只有在调试环境下才暴露出来,但我目前有需求需要将一部分异常的详细信息传递到客户端,又需要保证一定的安全性. 最简单的办法当然是在服务端将异常捕获后,序列化传给客户端,但这 ...

  4. grpc(3):使用 golang 开发 grpc 服务端和client

    1,关于grpc-go golang 能够能够做grpc的服务端和client. 官网的文档: http://www.grpc.io/docs/quickstart/go.html https://g ...

  5. [并发并行]_[线程模型]_[Pthread线程使用模型之三 客户端/服务端模型(Client/Server]

    Pthread线程使用模型之三 客户端/服务端模型(Client/Server) 场景 1.在客户端/服务端模型时,客户端向服务端请求一些数据集的操作. 服务端执行执行操作独立的(多进程或跨网络)– ...

  6. 服务端调用接口API利器之HttpClient

    前言 之前有介绍过HttpClient作为爬虫的简单使用,那么今天在简单的介绍一下它的另一个用途:在服务端调用接口API进行交互.之所以整理这个呢,是因为前几天在测试云之家待办消息接口的时候,有使用云 ...

  7. C# WCF服务端搭建和客户端调用

    1) 打开Visual Studio 2012,在菜单上点击文件->新建->项目->WCF服务应用程序.在弹出界面的"名称"对应的文本框中输入"WcfS ...

  8. WCF服务端返回:(413) Request Entity Too Large

    出现这个原因我们应该都能猜测到,文件传出过大,超出了WCF默认范围,那么我们需要进行修改. 服务端和客户端都需要修改. 第一.客户端: <system.serviceModel> < ...

  9. WCF服务端与客户端时间匹配问题

    当服务端部署的WCF服务服务在被客户机调用时,如果显示: 错误,展开后,详细错误为:An error occurred when verifying security for the message ...

随机推荐

  1. jQuery对象与JS原生对象之间的转换

    1.将jQuery转换为dom对象的方法 [index] 或者.get(index): a.$(“#form”)[index] ,该方法获取form元素的dom对象 b.$(“#form”).get( ...

  2. 插入标识列identity_insert

    插入标识列identity_insert 在进行数据插入时,如果插入列名包括标识列,常常会遇到以下3种提示: 一.“当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'xxxxxxxx ...

  3. MySQL学习笔记:concat、concat_ws、group_concat —— 字符串连接

    在MySQL中,实现字符串拼接主要有以下3种函数: concat(x,y,...) concat_ws(分隔符,x,y,...) group_concat(distinct xxx order by ...

  4. CVE-2012-4792Microsoft Internet Explorer 释放后使用漏洞

    Microsoft Internet Explorer是微软Windows操作系统中默认捆绑的WEB浏览器. Microsoft Internet Explorer 6至8版本中存在释放后使用漏洞.通 ...

  5. Ubuntu 17.10开启 root 登陆

    使用过 Ubuntu 的人都知道,Ubuntu 默认是不能以 root 登陆的,但是我们是不是就完全不能使用 root 进行登陆了呢?当然不是,只是我们需要做一些设置.而 Ubuntu 17.10 和 ...

  6. mysql千万级表关联优化

    MYSQL一次千万级连表查询优化(一) 概述: 交代一下背景,这算是一次项目经验吧,属于公司一个已上线平台的功能,这算是离职人员挖下的坑,随着数据越来越多,原本的SQL查询变得越来越慢,用户体验特别差 ...

  7. 收集Nginx的json格式日志(五)

    一.配置nginx [root@linux-node1 ~]# vim /etc/nginx/nginx.conf #修改日志格式为json格式,并创建一个nginxweb的网站目录 log_form ...

  8. RabbitMQ错误检查

    今天使用RabbitMQ做数据下发操作,当在发送端声明了Exchange后 打开RabbitMQ的管理控制台,可以查看,其中已经创建了Exchange 但并没有Queue 接着运行接收端,发现以下错误 ...

  9. 数据挖掘算法:关联分析二(Apriori)

    二.Apriori算法 上文说到,大多数关联规则挖掘算法通常采用的策略是分解为两步: 频繁项集产生,其目标是发现满足具有最小支持度阈值的所有项集,称为频繁项集(frequent itemset). 规 ...

  10. chromedriver与chrome版本对应表,firefox、geckodriver

     一. chromedriver与chrome对应表(记得就会更新): chromedriver版本 支持的Chrome版本 v2.36 v64-66 v2.35 v62-64 v2.34 v61-6 ...