1、使用WCF单项模式须知

(1)、WCF服务端接受客户端的请求,但是不会对客户端进行回复

(2)、使用单项模式的服务端接口,不能包含ref或者out类型的参数,至于为什么,请参考C# ref与out关键字解析

(3)、使用单项模式的服务端接口没有返回值,返回类型只能为void

(4)、通过设置OperationContract契约的IsOneWay=True可以将满足要求的方法设置为这种消息交换模式

2、代码实例

(1)、第一步

i、首先建一个名为IService的类库作为服务层,新建IOneWay接口

ii、导入System.ServiceModel命名空间

iii、在IOneWay接口中定义一个符合单项模式要求的方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace IService
{
[ServiceContract]
public interface IOneWay
{
[OperationContract(IsOneWay=true)]
void HelloWorld(string name);
}
}

(2)、第二步

i、建一个Service类库实现IService中的单项模式的方法

using IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace Service
{
public class OneWay : IOneWay
{
void IOneWay.HelloWorld(string name)
{
Thread.Sleep();
}
}
}

ok,服务初始化成功

(3)、第三步

创建WCF宿主,这里因为本地以及有WCF宿主了,所以这里就不建了,不知道怎么建的话,参考WCF系列教程之初识WCF,所以将服务通过配置配置文件发布到WCF宿主中

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Service.OneWayService" behaviorConfiguration="OneWayBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/OneWay/"/>
</baseAddresses>
</host> <endpoint address="" binding="wsHttpBinding" contract="IService.IOneWayService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services> <behaviors>
<serviceBehaviors>
<behavior name="OneWayBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> </configuration>

(4)、第四步

i、重新生成解决方案,打开Host.exe,打开WCF服务,浏览器中输入http://localhost:8000/OneWay/

ok,服务发布成功

(5)、第五步

i、创建客户端程序,并通过微软的svcutil工具生成UserInfoService服务的客户端代理类,开始菜单/Microsoft Visual Studio 2012/Visual Studio Tools/Visual Studio 2012开发人员命令提示工具,定位到当前客户端l路径,输入命令:svcutil http://localhost:8000/OneWay/?wsdl /o:OneWay.cs,生成客户端代理类,生成成功之后,将文件添加到项目中.

ii、调用代理类,代码如下:

Console.WriteLine("****************单向通讯服务示例*******************");
OneWayServiceClient proxy = new OneWayServiceClient();
Console.WriteLine("方法调用前时间:" + System.DateTime.Now);
proxy.HelloWorld("WCF");
Console.WriteLine("方法调用后时间:" + System.DateTime.Now);
Console.Read();

虽然服务器方法的时间进程暂停了6s,但客户端的表现出的只是单向的,并没有等待服务器的时间,也就是服务器并没有像客户端发送响应的消息。

iii、通过Vs提供的WCF测试工具,来对WCF服务进行测试,输入服务地址

,点击调用,瞬间服务端返回

我们发现只有请求消息,没有返回的消息,说明服务器并没有对此作出任何反应。

最新文章

  1. 【转】javascript打印设置
  2. nginx + lua +redis环境搭建
  3. 单片机TM4C123学习(十):ADC采样模块
  4. HTML Hyperlink between and within pages
  5. 1.4.2 solr字段类型--(1.4.2.1)字段类型定义和字段类型属性
  6. C++多态的实现与局限性
  7. sql - Invalid object name &#39;dbo.in$&#39;
  8. js_day2
  9. (原)ubuntu安装libtbb.so.2
  10. java读取配置文件的几种方法
  11. uva10718 - Bit Mask(贪心)
  12. SDL2源代码分析3:渲染器(SDL_Renderer)
  13. 题解-HAOI2018全套
  14. Dumb Bones UVA - 10529(概率dp)
  15. 内置函数 filter zip map
  16. win8下C盘不能读写的解决方案[zz]
  17. 开源项目CircleImageView
  18. layer.load()加载层如何加入文字描述
  19. SQL SERVER 压缩数据库日志文件
  20. windows下面安装easy_install和pip教程

热门文章

  1. Android 类加载器
  2. 学习python2
  3. Awk使用方法简介
  4. airpods2代连接macbook失败
  5. 我的Linux主机操作记录续
  6. mysql中判断记录是否存在方法比较【转】
  7. MVC的 url 传递参数无效
  8. SCVMM 安装
  9. Origin如何使曲面变平滑?
  10. 原生态js展开高度自适应100%