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 ...
随机推荐
- plsql developer配置
一:今天plsql developer连接 出问题了 ,Oracleclient没正确安装 0.连接vpn 1.环境变量:TNS_ADMIN = D:\worksoftware\oracleClien ...
- Python基础 - 正则表达式
Python自带正则表达式模块,即re模块. 导入正则模块: import re 用dir()函数查看re模块内的属性和方法: dir(re)
- JS中给函数参数添加默认值
最近在Codewars上面看到一道很好的题目,要求用JS写一个函数defaultArguments,用来给指定的函数的某些参数添加默认值.举例来说就是: // foo函数有一个参数,名为x var f ...
- 使用 ResponseBodyAdvice 拦截Controller方法默认返回参数,统一处理返回值/响应体
使用 @ControllerAdvice & ResponseBodyAdvice 拦截Controller方法默认返回参数,统一处理返回值/响应体 1.Controller代码 以下是Con ...
- hdu 4006 第K大的数(优先队列)
N次操作 I是插入一个数 Q是输出第K大的数 Sample Input8 3 //n kI 1I 2I 3QI 5QI 4Q Sample Output123 # include <iostre ...
- 《精通Python设计模式》学习之工厂方法
小书,在我以前作数据库的连接时,就用了这个工厂方法的. 归纳总结一下,更有利于成长吧. import xml.etree.ElementTree as etree import json class ...
- abp zero 4.3 发布
Demo URL: http://abpzerodemo.demo.aspnetzero.com Username: systemPassword: 123456 需要源码,请加QQ:3833-255 ...
- android拾遗——Android之Notification和NotificationManager
1.使用系统自带的Notification //创建一个NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; Notific ...
- 【转】实践最有效的提高Android Studio运行、编译速度方案
原文:https://blog.csdn.net/xwh_1230/article/details/60961723 实践最有效的提高Android Studio运行.编译速度方案 最有效提升Andr ...
- SPOJ - NSUBSTR 后缀自动机板子
SPOJ - NSUBSTR #include<bits/stdc++.h> #define LL long long #define fi first #define se second ...