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的更多相关文章

  1. <转>WCF中出现死锁或者超时

    WCF回调中的死锁 一.服务器端死锁 对于如下服务: [ServiceContract(CallbackContract = typeof(INotify))] public class Downlo ...

  2. 跟我一起学WCF(12)——WCF中Rest服务入门

    一.引言 要将Rest与.NET Framework 3.0配合使用,还需要构建基础架构的一些部件.在.NET Framework 3.5中,WCF在System.ServiceModel.Web组件 ...

  3. 跟我一起学WCF(10)——WCF中事务处理

    一.引言 好久没更新,总感觉自己欠了什么一样的,所以今天迫不及待地来更新了,因为后面还有好几个系列准备些,还有很多东西需要学习总结的.今天就来介绍下WCF对事务的支持. 二.WCF事务详解 2.1 事 ...

  4. 我的WCF之旅(3):在WCF中实现双工通信

    双工(Duplex)模式的消息交换方式体现在消息交换过程中,参与的双方均可以向对方发送消息.基于双工MEP消息交换可以看成是多个基本模式下(比如请求-回复模式和单项模式)消息交换的组合.双工MEP又具 ...

  5. WCF技术剖析之十一:异步操作在WCF中的应用(上篇)

    原文:WCF技术剖析之十一:异步操作在WCF中的应用(上篇) 按照操作执行所需的资源类型,我们可以将操作分为CPU绑定型(CPU Bound)操作和I/O绑定型(I/O Bound)操作.对于前者,操 ...

  6. wcf 中客户端调用之死 感悟 wcf与原来的webservice2.0 的客户端调用区别(wcf调用完不关闭的话那就把web服务搞死了)

    说到wcf,本人也是刚刚使用所以不是很熟悉 在做项目的时候采用webservice+客户端程序架构 写了一个wcf中的webservice之后,又写了很多的客户端exe程序,有的是轮询调用这个webs ...

  7. 在WCF中实现双工通信

    双工(Duplex)模式的消息交换方式体现在消息交换过程中,参与的双方均可以向对方发送消息.基于双工MEP消息交换可以看成是多个基本模式下(比如请求-回复模式和单项模式)消息交换的组合.双工MEP又具 ...

  8. 理解WCF中的Contracts

    WCF中的Contracts WCF通过Contract来说明服务和操作,一般包含五种类型的Contract:ServiceContract,OperationContract,FaultContra ...

  9. WCF中的异步实现

    对于WCF中通讯的双方来说,客户端可以异步的调用服务:服务端对服务也能以异步的方式实现. 目录: 1.WCF客户端异步调用服务 2.服务端的异步实现 WCF客户端异步调用服务主要通过生成异步的代理类, ...

随机推荐

  1. Nginx- 实现跨域访问

    https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80688740

  2. 【3005】拦截导弹问题(noip1999)

    Time Limit: 3 second Memory Limit: 2 MB 某国为了防御帝国的导弹袭击,开发出一种导弹拦截系统,但是这种拦截系统有一个缺陷:虽然他的第一发炮弹能达到任意的高度,但是 ...

  3. [TypeStyle] Compose CSS classes using TypeStyle

    We will demonstrate composing classes using the utility classes function. classes is also what we re ...

  4. linux下的打包和压缩

    linux中常见的两种压缩包文件的格式是.tar..gz和.tar.gz..tar仅仅是将文件简单地打包,文件的大小没有变化,也就是说.tar文件仅仅是一个包,没有被压缩:.tar.gz文件是打包后用 ...

  5. 如何知道刚刚插入数据库那条数据的id

    如何知道刚刚插入数据库那条数据的id 一.总结 一句话总结:这些常见功能各个框架里面都有,可以查看手册,thinkphp里面是$userId = Db::name('user')->getLas ...

  6. LinearLayout的一些注意事项 分类: H1_ANDROID 2013-10-26 23:01 856人阅读 评论(0) 收藏

    1.orientation的默认值为horizontal,即从左向右排列.由于一般从上向下排列,所以必须指定orientation属性. 2.layout_gravity与gravity的区别: (1 ...

  7. php实现找两个链表的第一个公共结点(实例演示)

    php实现找两个链表的第一个公共结点(实例演示) 一.总结 因为是链表,第一个节点公共之后,后面所有的节点都公共了 画个图实例演示一下,会超清晰且简单 二.php实现找两个链表的第一个公共结点 题目描 ...

  8. 【41.43%】【codeforces 560C】Gerald's Hexagon

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 750A】New Year and Hurry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  10. Hierarchical Tree Traversal in Graphics Pipeline Stages

    BACKGROUND Many algorithms on a graphics processing unit (GPU) may benefit from doing a query in a h ...