zoukankan      html  css  js  c++  java
  • WCF 学习笔记(二)

    WCF中Host和Client端都可以使用app.config 进行配置,然后程序根据配置生成相应的方法。因为这些都被包进WCF中,所以用户可以通过修改config 进行服务的设定。

    在学习笔记 一中,我们实现了一个http协议下的C/S的结构的程序。
    可以直接修改配置文件,来将其修改成一个tcp协议下的程序。
    HostServer的app.config 修改如下:
     1<?xml version="1.0" encoding="utf-8" ?>
     2<configuration>
     3  <system.serviceModel>
     4    <services>
     5      <service name="TESTWCF.MyData.MyDataService" behaviorConfiguration="serviceBehavior">
     6          <!--<endpoint binding="basicHttpBinding" contract="TESTWCF.MyData.IMyDataService" address="MyData" />-->
     7          <endpoint binding="netTcpBinding" contract="TESTWCF.MyData.IMyDataService" address="MyData" />
     8
     9          <!--<endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />-->
    10        <host>
    11          <baseAddresses>
    12            <!--<add baseAddress="http://localhost:8090"/>-->
    13              <add baseAddress="net.tcp://localhost:8010"/>
    14
    15          </baseAddresses>
    16        </host>
    17      </service>
    18    </services>
    19
    20    <behaviors>
    21      <serviceBehaviors>
    22        <behavior name="serviceBehavior">
    23          <serviceMetadata httpGetEnabled="false" />
    24        </behavior>
    25      </serviceBehaviors>
    26    </behaviors>
    27  </system.serviceModel>
    28</configuration>

    这里修改了binding的属性,修改为netTcpBinding .并且修改baseAddresses中的协议的地址。
    如果要修改别的方式,比如MSQ等,可以直接修改该文件。
    在WCF中支持9种邦定的方式,如下:
    BasicHttpBinding: 用于把 WCF 服务当作 ASMX Web 服务。用于兼容旧的Web ASMX 服务。
    WSHttpBinding: 比 BasicHttpBinding 更加安全,通常用于 non-duplex 服务通讯。
    WSDualHttpBinding: 和 WSHttpBinding 相比,它支持 duplex 类型的服务。
    WSFederationHttpBinding: WS-Federation 安全通讯协议。
    NetTcpBinding: 使用 TCP 协议,用于在局域网(Intranet)内跨机器通信。有几个特点:可靠性、事务支持和安全,优化了 WCF 到 WCF 的通信。限制是服务端和客户端都必须使用 WCF 来实现。
    NetNamedPipeBinding: 使用命名管道进行安全、可靠、高效的单机服务通讯方式。
    NetMsmqBinding: 使用消息队列在不同机器间进行非连接通讯。
    NetPeerTcpBinding: 使用 P2P 协议在多机器间通讯。
    MsmqIntegrationBinding: 将 WCF 消息转化为 MSMQ 消息,使用现有的消息队列系统进行跨机器通讯。如 MSMQ。

    接口对应的协议及Encoding如下表:



    如果要使HostServer同时支持Http。TCP的方式,可以直接上面中的注释的部分打开。

    Client端app.config 修改以适应HostServer的修改。
    app.config 修改后如下:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
      
        <client>
      <endpoint address="net.tcp://localhost:8010/MyData" binding="netTcpBinding"
                    contract="TESTWCF.MyData.IMyDataService" />
          <!--<endpoint address="http://localhost:8090/MyData" binding="basicHttpBinding"
                    contract="TESTWCF.MyData.IMyDataService" />-->
        </client>
      </system.serviceModel>
    </configuration>


    以上修改后,Client端变为使用tcp 的协议和HostServer交互。
    以上修改均不需要修改code,既可实现。

    另外,中午看过一遍文章,觉得在上一篇中,应该将MyData只定义接口的部分,而具体类可以定义在HostServer中,实现该接口,这样做可以将接口部分定义为一个dll,可以供Client端使用,同时可以使具体的实现不能暴露出去。
    当然Client已可以使用生成代理类的那种方法,但是觉得那样很麻烦,直接使用定义好的接口给Client就好啊。

    Client中使用ClientBase的类,继承该类,实现不同的初始化函数可以使Client端更灵活的使用app.config中的定义。将在以后的学习中陆续记录下各种的实现的方式。




  • 相关阅读:
    LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
    LeetCode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)
    LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
    LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)
    LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
    LeetCode 617. 合并二叉树(Merge Two Binary Trees)
    LeetCode 206. 反转链表(Reverse Linked List) 16
    LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
    LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
    LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14
  • 原文地址:https://www.cnblogs.com/zhucl1006/p/1042843.html
Copyright © 2011-2022 走看看