WCF服务端调用client.
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.的更多相关文章
- WCF服务端开发和客户端引用小结
1.服务端开发 1.1 WCF服务创建方式 创建一个WCF服务,总是会创建一个服务接口和一个服务接口实现.通常根据服务宿主的不同,有两种创建方式. (1)创建WCF应用程序 通过创建WCF服务应用程序 ...
- wcf第三方客户端与wcf服务之间调用入门
Wcf服务与我们的客户端如何建立联系的呢.本文简单记录一下 1.创建我们的wcf服务程序. 第一个wcf服务库是创建我们的wcf库,运行时会单独来托管我们的程序,而非托管在iis下. 第二个wcf服务 ...
- WCF 服务端异常封装
通常WCF服务端异常的详细信息只有在调试环境下才暴露出来,但我目前有需求需要将一部分异常的详细信息传递到客户端,又需要保证一定的安全性. 最简单的办法当然是在服务端将异常捕获后,序列化传给客户端,但这 ...
- grpc(3):使用 golang 开发 grpc 服务端和client
1,关于grpc-go golang 能够能够做grpc的服务端和client. 官网的文档: http://www.grpc.io/docs/quickstart/go.html https://g ...
- [并发并行]_[线程模型]_[Pthread线程使用模型之三 客户端/服务端模型(Client/Server]
Pthread线程使用模型之三 客户端/服务端模型(Client/Server) 场景 1.在客户端/服务端模型时,客户端向服务端请求一些数据集的操作. 服务端执行执行操作独立的(多进程或跨网络)– ...
- 服务端调用接口API利器之HttpClient
前言 之前有介绍过HttpClient作为爬虫的简单使用,那么今天在简单的介绍一下它的另一个用途:在服务端调用接口API进行交互.之所以整理这个呢,是因为前几天在测试云之家待办消息接口的时候,有使用云 ...
- C# WCF服务端搭建和客户端调用
1) 打开Visual Studio 2012,在菜单上点击文件->新建->项目->WCF服务应用程序.在弹出界面的"名称"对应的文本框中输入"WcfS ...
- WCF服务端返回:(413) Request Entity Too Large
出现这个原因我们应该都能猜测到,文件传出过大,超出了WCF默认范围,那么我们需要进行修改. 服务端和客户端都需要修改. 第一.客户端: <system.serviceModel> < ...
- WCF服务端与客户端时间匹配问题
当服务端部署的WCF服务服务在被客户机调用时,如果显示: 错误,展开后,详细错误为:An error occurred when verifying security for the message ...
随机推荐
- github后端开发面试题大集合(三)
作者:小海胆链接:https://www.nowcoder.com/discuss/3616来源:牛客网 13.软件架构相关问题: 什么情况下缓存是没用的,甚至是危险的? 为什么事件驱动的架构能提高可 ...
- Hex Dump In Many Programming Languages
Hex Dump In Many Programming Languages See also: ArraySumInManyProgrammingLanguages, CounterInManyPr ...
- php中的单引号与双引号详解
一.引号定义字符串 在Php中,通常一个字符串被定义在一对引号中,如: 'I am a string in single quotes'"I am a string in double qu ...
- Python学习:socket.gaierror: [Errno -8]
在终端内打开python模式,利用如下代码查询本机hostname,这里举例为“xxMacBookPro.local”: import socket socket.gethostname() 在/et ...
- js对象的属性:数据(data)属性和访问器(accessor)属性
此文为转载,原文: 深入理解对象的数据属性与访问器属性 创建对象的方式有两种:第一种,通过new操作符后面跟Object构造函数,第二种,对象字面量方式.如下 var person = new Obj ...
- MATLAB读写Excel文件中的数据
读取: %读取filename文件中指定表的数据,存入dataRead矩阵中 dataRead=xlsread('filename.xls',sheet); 写入: %将矩阵dataWrite中的数据 ...
- Spark(五)Spark任务提交方式和执行流程
一.Spark中的基本概念 (1)Application:表示你的应用程序 (2)Driver:表示main()函数,创建SparkContext.由SparkContext负责与ClusterMan ...
- python开发学习-day13(js、jQuery)
s12-20160409-day13 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- Kylin使用笔记-1: 安装
2016年1月14日 9:57:23 星期四 背景介绍 Apache Kylin是一个开源的分布式分析引擎,提供Hadoop之上的SQL查询接口及多维分析(OLAP)能力以支持超大规模数据,最 ...
- chrome安装(sentos7)
在服务器上安装chrome是用来模拟浏览器抓取数据的. 直接 yum install chrome是安装不了的 你要做以下几步就可以了. 配置yum源 1. vim /etc/yum.repos.d/ ...