zoukankan      html  css  js  c++  java
  • WCF系列教程之WCF消息交换模式之单项模式

    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(6000);
            }
        }
    }

    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服务进行测试,输入服务地址

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

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

  • 相关阅读:
    linux嵌入式系统交叉开发环境
    Codeforces Round #208 E. Dima and Kicks
    mvn 编译错误java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter. <init>(Z)V
    黑马程序员_<<TCP>>
    微信/易信公共平台开发(四):公众号调试器 (仿真微信平台,提供PHP源码)
    用pdb调试OpenStack Havana
    MySql Odbc等驱动下载地址分享下
    导入exce表格中的数据l到数据库
    关闭数据备份信息写入数据库日志
    SQL Server之RAID简介
  • 原文地址:https://www.cnblogs.com/GreenLeaves/p/6842782.html
Copyright © 2011-2022 走看看