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

    一个服务作为一系列终结点被定义的。每个终结点都有一个地址,绑定和契约。契约就是暴露终结点能力的。地址就是这些应用或服务从网络的哪个地址可找到,契约是关于如何访问他们的。
    在终结点和契约间有一对多的关系。一个终结点可以只有一个契约,但是一个契约可以被很多终结点引用。尽管一个终结点可以仅仅确认一个契约,接口聚合使能一个单独的契约来暴露多个接口。另外,多个有同样绑定但是不同契约的终结点可以位于同一个地址,给一个单独终结点实现所有契约的假象。
    通过在一个服务中的多个终结点暴露一个契约,你可以让服务在不同绑定下都可以访问。你可以定义一个终结点使用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"));
    }
  • 相关阅读:
    时间戳(UnixTimestamp)与 《2038年问题》
    端口相关命令
    Ubuntu中的在文件中查找和替换命令
    A Reusable Aspect for Memory Profiling
    acc文件的运行
    A Reusable Aspect for Memory Allocation Checking
    ACC常用语句
    aspectC++常用命令
    c++调用DOS命令,不显示黑屏
    fopen文件目录问题
  • 原文地址:https://www.cnblogs.com/danielWise/p/2088447.html
Copyright © 2011-2022 走看看