4 WCF中的RPC和OneWay
1 创建两个控制台项目
WcfService和WcfClient
在wcfService项目中新建一个wcf服务的文件项(HomeService)会自动附带生成一个IHomeService.cs的文件
using System.ServiceModel; namespace WcfService
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IHomeService”。
[ServiceContract]
public interface IHomeService
{
//默认生成的
[OperationContract]
void DoWork(string msg); //这个是我后来新加入的接口方法
[OperationContract(IsOneWay =true)]
void DoWork_OneWay(string msg);
}
}
IHomeService
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets; namespace WcfService
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“HomeService”。
public class HomeService : IHomeService
{
public void DoWork_OneWay(string msg)
{
Console.WriteLine($"这是OneWay通讯,{msg}");
} void IHomeService.DoWork(string msg)
{
var ip = Dns.GetHostAddresses(Dns.GetHostName()).
FirstOrDefault(i => i.AddressFamily == AddressFamily.InterNetwork).ToString();
var info = string.Format($"当前 request 由 server={ip}返回message={msg}");
Console.WriteLine(info);
}
}
}
HomeService
2 创建WcfService项目会默认在其配置文件中生成相应的wcf配置
我们请求endPoint中的address,在打开的页面中可以看到有关wsdl的链接
在1中,我们wcf接口定义了两个方法,这两个方法一个是rpc的 一个是oneway的。rpc发送信息有去有回,oneway只去不回(适合用来指示服务器写日志)
以下是wsdl代码:
<?xml version="1.0" encoding="ISO-8859-1"?>
<wsdl:definitions xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:tns="http://tempuri.org/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://tempuri.org/" name="HomeService">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import namespace="http://tempuri.org/" schemaLocation="http://localhost:8733/Design_Time_Addresses/WcfService/HomeService?xsd=xsd0"/>
<xsd:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" schemaLocation="http://localhost:8733/Design_Time_Addresses/WcfService/HomeService?xsd=xsd1"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="IHomeService_DoWork_InputMessage">
<wsdl:part name="parameters" element="tns:DoWork"/>
</wsdl:message>
<wsdl:message name="IHomeService_DoWork_OutputMessage">
<wsdl:part name="parameters" element="tns:DoWorkResponse"/>
</wsdl:message>
<wsdl:message name="IHomeService_DoWork_OneWay_InputMessage">
<wsdl:part name="parameters" element="tns:DoWork_OneWay"/>
</wsdl:message>
<wsdl:portType name="IHomeService">
<wsdl:operation name="DoWork">
<wsdl:input message="tns:IHomeService_DoWork_InputMessage" wsaw:Action="http://tempuri.org/IHomeService/DoWork"/>
<wsdl:output message="tns:IHomeService_DoWork_OutputMessage" wsaw:Action="http://tempuri.org/IHomeService/DoWorkResponse"/>
</wsdl:operation>
<wsdl:operation name="DoWork_OneWay">
<wsdl:input message="tns:IHomeService_DoWork_OneWay_InputMessage" wsaw:Action="http://tempuri.org/IHomeService/DoWork_OneWay"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_IHomeService" type="tns:IHomeService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="DoWork">
<soap:operation style="document" soapAction="http://tempuri.org/IHomeService/DoWork"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="DoWork_OneWay">
<soap:operation style="document" soapAction="http://tempuri.org/IHomeService/DoWork_OneWay"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HomeService">
<wsdl:port name="BasicHttpBinding_IHomeService" binding="tns:BasicHttpBinding_IHomeService">
<soap:address location="http://localhost:8733/Design_Time_Addresses/WcfService/HomeService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
wsdl
我们看到dowork方法由于是rpc(默认rpc),其节点中有两个节点 一个input 一个output;dowork_OneWay方法是OneWay的(其特性构造函数中OneWay=true),其节点中只有一个input节点
3.有一点要注意的是
rpc的方法 请求的时候默认是 wsaw:Action="http://tempuri.org/IHomeService/DoWork"/> 响应是wsaw:Action="http://tempuri.org/IHomeService/DoWorkResponse"/>,当然这个可以设置,正式由于请求响应的这个头不同,服务才知道哪是请求、哪是响应
4 WCF中的RPC和OneWay的更多相关文章
- <转>WCF中出现死锁或者超时
WCF回调中的死锁 一.服务器端死锁 对于如下服务: [ServiceContract(CallbackContract = typeof(INotify))] public class Downlo ...
- 跟我一起学WCF(12)——WCF中Rest服务入门
一.引言 要将Rest与.NET Framework 3.0配合使用,还需要构建基础架构的一些部件.在.NET Framework 3.5中,WCF在System.ServiceModel.Web组件 ...
- 跟我一起学WCF(10)——WCF中事务处理
一.引言 好久没更新,总感觉自己欠了什么一样的,所以今天迫不及待地来更新了,因为后面还有好几个系列准备些,还有很多东西需要学习总结的.今天就来介绍下WCF对事务的支持. 二.WCF事务详解 2.1 事 ...
- 我的WCF之旅(3):在WCF中实现双工通信
双工(Duplex)模式的消息交换方式体现在消息交换过程中,参与的双方均可以向对方发送消息.基于双工MEP消息交换可以看成是多个基本模式下(比如请求-回复模式和单项模式)消息交换的组合.双工MEP又具 ...
- WCF技术剖析之十一:异步操作在WCF中的应用(上篇)
原文:WCF技术剖析之十一:异步操作在WCF中的应用(上篇) 按照操作执行所需的资源类型,我们可以将操作分为CPU绑定型(CPU Bound)操作和I/O绑定型(I/O Bound)操作.对于前者,操 ...
- wcf 中客户端调用之死 感悟 wcf与原来的webservice2.0 的客户端调用区别(wcf调用完不关闭的话那就把web服务搞死了)
说到wcf,本人也是刚刚使用所以不是很熟悉 在做项目的时候采用webservice+客户端程序架构 写了一个wcf中的webservice之后,又写了很多的客户端exe程序,有的是轮询调用这个webs ...
- 在WCF中实现双工通信
双工(Duplex)模式的消息交换方式体现在消息交换过程中,参与的双方均可以向对方发送消息.基于双工MEP消息交换可以看成是多个基本模式下(比如请求-回复模式和单项模式)消息交换的组合.双工MEP又具 ...
- 理解WCF中的Contracts
WCF中的Contracts WCF通过Contract来说明服务和操作,一般包含五种类型的Contract:ServiceContract,OperationContract,FaultContra ...
- WCF中的异步实现
对于WCF中通讯的双方来说,客户端可以异步的调用服务:服务端对服务也能以异步的方式实现. 目录: 1.WCF客户端异步调用服务 2.服务端的异步实现 WCF客户端异步调用服务主要通过生成异步的代理类, ...
随机推荐
- 5、qq物联开发步骤
1.QQ物联开发步骤 1)测试环境无需申请上线,即可任意调试.意思是什么呢,它是告诉大家,在调试的时候不要点击上面的提交上线,因为目还在调试此设备,一旦上线,QQ物联官方就会来审核你的设备,导致延长研 ...
- Hadoop读书笔记(四)HDFS体系结构
Hadoop读书笔记(一)Hadoop介绍:http://blog.csdn.net/caicongyang/article/details/39898629 Hadoop读书笔记(二)HDFS的sh ...
- WebService--CXF与Spring的整合(jaxws:endpoint形式配置)以及客户端调用(spring配置文件形式,不需要生成客户端代码)
一.CXF与Spring整合(jaxws:endpoint形式配置) 工具要点:idea.maven 1.新建一个maven项目 <?xml version="1.0" en ...
- ZOJ 1914 Arctic Network (POJ 2349 UVA 10369) MST
ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1914 POJhttp://poj.org/problem?id=23 ...
- matlab 正则表达式
regexprep Replace text using regular expression collapse all in page Syntax newStr = regexprep(str,e ...
- iir调试记录
1.目的 实现採样率fs=50MHz,通带为5MHz~15MHz.阻带衰减60dB的IIR带通滤波器 2.方案 採取直接型 3.具体设计 (1)确定滤波器的系数,系数和滤波器输出量化位宽 先依据要求的 ...
- [Vue] Create Vue.js Layout and Navigation with Nuxt.js
Nuxt.js enables you to easily create layout and navigation by replacing the default App.vue template ...
- Finder那点事
事件是这样,我MAC PRO,关不了机了,是有什么线程在用 defaults write com.apple.Finder QuitMenuItem 1 这个命令是让Finder 有退出BTN ,co ...
- 如何查看一个网页特定效果的js代码(动画效果可js和css)(页面可以看到js的源代码)
如何查看一个网页特定效果的js代码(动画效果可js和css)(页面可以看到js的源代码) 一.总结 1.动画效果可能是 CSS 实现的,也可能是 JS 实现的. 2.直接Chrome的F12调试即可, ...
- Android菜鸟的成长笔记(27)——SurfaceView的使用
前面有关自定义View中进行了绘图,但View的绘图机制存在如下缺陷: 1.View缺乏双缓冲机制. 2.当程序需要更新View上的图像时,程序必须重绘View上显示的整张图片. 3.新线程无法直接更 ...