zoukankan      html  css  js  c++  java
  • 在一个服务中实现 多个契约 和终结点 z

    一个服务作为一系列终结点被定义的。每个终结点都有一个地址,绑定和契约。契约就是暴露终结点能力的。地址就是这些应用或服务从网络的哪个地址可找到,契约是关于如何访问他们的。
    在终结点和契约间有一对多的关系。一个终结点可以只有一个契约,但是一个契约可以被很多终结点引用。尽管一个终结点可以仅仅确认一个契约,接口 聚合使能一个单独的契约来暴露多个接口。另外,多个有同样绑定但是不同契约的终结点可以位于同一个地址,给一个单独终结点实现所有契约的假象。
    通过在一个服务中的多个终结点暴露一个契约,你可以让服务在不同绑定下都可以访问。你可以定义一个终结点使用WS-I基础协议绑定来得到最大访 问量同时使用另外一个使用TCP协议和二进制编码的终结点来实现更快的性能。通过把多个接口变成一个接口的假象,你可以通过合并在一个单一的服务访问,初 步编纂成独立的接口能力。
    列表2.10 在一个终结点中暴露多个契约
    using System;
    using System.ServiceModel;
    using System.Threading;
    
    namespace EssentialWCF
    {
        [ServiceContract]
        public interface IGoodStockService
        {
            [OperationContract]
            double GetStockPrice(string ticker);
        }
        [ServiceContract]
        public interface IGreatStockService
        {
            [OperationContract]
            double GetStockPriceFast(string ticker);
        }
        [ServiceContract]
        public interface IALLStockServices : IGoodStockService, IGreatStockService{};
        public class AllStockServices : IALLStockServices
        {
            public double GetStockPrice(string ticker)
            {
                Thread.Sleep(5000);
                return 94.85;
            }
            public double GetStockPriceFast(string ticker)
            {
                return 94.85;
            }
        }
    }
    
    列表2.11一个为三个契约暴露多个终结点的配置文件。有一个为IGoodStockService契约的终结点,两个为IGreateStockService契约的终结点和一个为IAllStockServices契约的终结点。
    因为有多个终结点共享一个寻址方案,每个终结点必须定义不同的地址。使用了相对地址,所以每个终结点的完全地址是服务基地址加上相对地址。
    列表2.11 在一个服务中暴露多一个终结点
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name ="EssentialWCF.StockServices"                behaviorConfiguration="mexServiceBehavior">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8000/EssentialWCF"/>
              </baseAddresses>
            </host>
            <endpoint name="GoodStockService" 
                      binding="basicHttpBinding" 
                      contract="EssentialWCF.IGoodStockService"/>
            <endpoint name="BetterStockService" 
                      address="better" 
                      binding="basicHttpBinding"
                      contract="EssentialWCF.IGreatStockService"/>
            <endpoint name="BestStockService" 
                      address="best"
                      binding="wsHttpBinding" 
                      contract="EssentialWCF.IGreatStockService"/>
            <endpoint name="AllStockServices" 
                      address="all" 
                      binding ="wsHttpBinding" 
                      contract="EssentialWCF.IAllStockServices"/>
            <endpoint name ="mex" 
                      binding ="mexHttpBinding" 
                      contract="IMetadataExchange"
          </service>
        </services>
      </system.serviceModel>
    </configuration>
    
    因为IGreatStockService契约在多个终结点上暴露,客户端应用程序必须在为契约创建一个代理实例时引用终结点名称。如果终结点 名称没有确认,WCF将会抛出一个错误因为它无法知道使用哪个终结点。列表2.12显示了对GreatStockServiceClient代理的两次使 用:一次使用basicHttpBinding访问BetterStockService,另一次使用wsHttpBinding访问 BestStockService.
    列表2.12 当多个终结点被定义使用名字确认每个终结点
    using (localhost.GreatStockServiceClient proxy = 
      new Client.localhost.GreatStockServiceClient("BetterStockService"))
    {
      Console.WriteLine(proxy.GetStockPriceFast("MSFT"));
    }
    using (localhost.GreatStockServiceClient proxy = 
      new Client.localhost.GreatStockServiceClient("BestStockService"))
    {
      Console.WriteLine(proxy.GetStockPriceFast("MSFT"));
    }
    
  • 相关阅读:
    Hbase数据备份&&容灾方案
    maven 高级玩法
    Python操作MySQL -即pymysql/SQLAlchemy用法
    python
    Redis的AOF功能
    Redis的快照功能
    查看哪些进程占用了SWAP分区?
    Java进程CPU使用率高排查
    利用iptables实现基于端口的网络流量统计
    从free命令看Linux内存管理
  • 原文地址:https://www.cnblogs.com/zeroone/p/4337618.html
Copyright © 2011-2022 走看看