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. OC学习篇之---Foundation框架中的其他类(NSNumber,NSDate,NSExcetion)

    1.NSNumber 这个类主要是用来封装基本类型的,说到这里,就不得不说一下了: OC中的集合是不允许存入基本类型的,所以NSNumber类就诞生了,需要将基本类型封装一下,然后存进去,这个类似于J ...

  2. hdu 3605 Escape 二分图的多重匹配(匈牙利算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    ...

  3. 影响stm32仿真的因素

    可能是因为电池电量不足??? 电量不足可能会妨碍SD卡的挂载

  4. C标签的使用.md

    <c:set> 设置变量 <c:set var="a" scope="request" value="${'www'}"/ ...

  5. 前端css常用的选择小汇

    要使用css对HTML页面中的元素实现一对一,一对多或者多对一的控制,这就需要用到CSS选择器.选择器就是选择器用来指定样式的作用范围. 类选择器: 类选择器在css中比较常见,首先要在普通标签中设置 ...

  6. 机器学习算法笔记1_2:分类和逻辑回归(Classification and Logistic regression)

    形式: 採用sigmoid函数: g(z)=11+e−z 其导数为g′(z)=(1−g(z))g(z) 如果: 即: 若有m个样本,则似然函数形式是: 对数形式: 採用梯度上升法求其最大值 求导: 更 ...

  7. [Math Processing Error] 问题的解决(F5刷新页面与 Ctrl/Shift + F5 刷新页面的区别)

    Why is [Math Processing Error] all over the place today? 当打开某页面出现 [Math Processing Error],一般表示 MathJ ...

  8. php 随机数中奖demo演示

    感谢https://blog.csdn.net/z960339491/article/details/69511491提供的思路,应该是java,于我不合适,写了php <?php // 中奖概 ...

  9. DIKW模型与数据工程(了解)

    DIKW 体系 DIKW体系是关于数据.信息.知识及智慧的体系,可以追溯至托马斯·斯特尔那斯·艾略特所写的诗--<岩石>.在首段,他写道:"我们在哪里丢失了知识中的智慧?又在哪里 ...

  10. Spinlock implementation in ARM architecture

    Spinlock implementation in ARM architecture   SEV and WFE are the main instructions used for impleme ...