zoukankan      html  css  js  c++  java
  • WCF 第四章 绑定

    正如第三章“信道”所描述的,信道栈是一个由一个或多个信道组成用来处理消息的层次 通信栈。绑定是预先设置的信道栈。它们代表了在客户端和服务端之间的线上契约。每个绑定由通信中涉及的传输,编码和协议确定。WCF使用绑定为多样化通信 场景集合配置信息。最普通的通信场景,比如网络服务,REST/POX 服务和基于队列的应用都在盒子外面提供。例如,basicHttpBinding绑定意味着使用基于ASP.NET Web Services的服务或者与WS-I 基础协议1.1 相适应的服务。ws2007HttpBinding 和wsHttpBinding 绑定类似于basicHttpBinding绑定,但是它们支持更多的特性,比如可信赖消息和事务,而且使用新的标准WS-Addressing. ws2007HttpBinding 绑定继承自.NET 3.5而且比wsHttpBinding基于更新的标准。表4.1 列举了12种用来通信的绑定以及它们中每一个的使用细节。
    表4.1 .NET Framework 3.5 中的WCF 通信绑定
    表4.1中的绑定可以通过代码或者配置文件设置。列表4.1 显示了由配置文件设置的basicHttpBinding绑定。使用配置文件允许开发人员在以后灵活的改变或修改绑定而不用重新编译代码。
    列表4.1 在配置文件中使用绑定
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <system.serviceModel>
    <client>
    <endpoint address="http://localhost/helloworld"
    binding
    ="basicHttpBinding"
    contract
    ="EssentialWCF.HelloWorld">

    </endpoint>
    </client>
    </system.serviceModel>
    </configuration>
    BasicHttpBinding绑定类在列表4.2中显示。使用代码允许一个开发人员使用特定的绑定而杜绝了以后对它更改的可能性。
    列表4.2 在代码中使用一个绑定
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using System.ServiceModel.Channels;

    namespace EssentialWCF
    {
    class Program
    {
    BasicHttpBinding binding
    = new BasicHttpBinding();
    using(HelloWorldClient client = new HelloWorldClient(binding, "http://localhost/helloworld"))
    {
    client.SayHello(
    "Rich");
    Console.ReadLine();
    }
    }
    }
    绑定通过一个绑定元素集合构成了一个信道栈。绑定元素代表了信道栈中的一个信道对象。每个绑定,比如basicHttpBinding绑定,由很多绑定元素组成。你可以在代码中通过初始化绑定而且枚举绑定元素集合来检查这个。这些在列表4.3中显示。
    列表4.3 检查BindingElementCollection
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using System.ServiceModel.Channels;

    namespace EssentialWCF
    {
    class Program
    {
    static void Main(string[] args)
    {
    OutputBindingElements(
    new WSHttpBinding());
    OutputBindingElements(
    new NetTcpBinding());
    OutputBindingElements(
    new NetNamedPipeBinding());
    OutputBindingElements(
    new BasicHttpBinding());
    Console.ReadLine();
    }
    static void OutputBindingElements(Binding binding)
    {
    Console.WriteLine(
    "Binding:{0}", binding.GetType().Name);
    BindingElementCollection elements
    = binding.CreateBindingElements();
    foreach(BindingElement element in elements)
    {
    Console.WriteLine(
    "{0}", element.GetType().Name);
    Console.WriteLine();
    }
    }
    }
    }
    图片4.1 显示了列表4.3中代码的输出,使用了四个out-of-the-box中的绑定:WSHttpBinding, NetTcpBinding, NetNamedPipeBinding和BasicHttpBinding.我们将查看WSHttpBinding绑定以便于我们可以理解用来构成绑定 的绑定元素。
    默认的WSHttpBinding配置由四个绑定元素组成:HttpTransportBindingElement, TextMessageEncodingBindingElement, SymmetricSecurityBindingElement和TransactionFlowBindingElement.这些绑定元素允许 HTTP协议,基于文本的消息编码,安全和事务支持,等等的通信。注意列表中的绑定元素是基于默认配置的。绑定可能添加或移除绑定元素,这取决于你如何皮 遏制绑定。
    注意每个绑定是如何由一个或多个绑定元素组成以及那些绑定中的一部分在不同绑定间是共有的。例如,WSHttpBinding和 BasicHttpBinding使用HttpTransportBindingElement.这两个绑定使用同样的传输结构但是在它们支持的功能和能 力上有所区分。我们将在这一章中讨论这些绑定的差异。
    这章的余下部分将会关注Web Services, 扩机器,本机和基于队列的通信。这些是开发人员需要了解来开始使用WCF的通信形式。也有基于REST/POX,对等网络和统一安全形式的通信会在第十三 章”可编程网页”,第十二章”对等网络”和第十一章“安全”分别介绍。


    ==========

    转载自

     

  • 相关阅读:
    ajax专题
    luogu P1346 电车 最短路
    luogu P1462 通往奥格瑞玛的道路 最短路
    luogu P1328 生活大爆炸版石头剪刀布
    luogu P1315 联合权值 枚举
    luogu P1156 垃圾陷阱 背包问题
    luogu P1217 回文质数 枚举
    luogu P3650 滑雪课程设计 枚举
    luogu1209 修理牛棚 贪心
    luogu P1223 排队接水 贪心
  • 原文地址:https://www.cnblogs.com/llbofchina/p/2092982.html
Copyright © 2011-2022 走看看