zoukankan      html  css  js  c++  java
  • WCF Basic(2)服务契约继承


    1 wcf允许接口契约继承,但每个接口必须明确以ServiceContract来声明,不能因为是继承,而忽略了父类的声明.如下声明

    [ServiceContract]
     interface ISimpleCalculator
     {
         [OperationContract]
         int Add(int arg1, int arg2);
     }
     [ServiceContract]
     interface IScientificCalculator : ISimpleCalculator
     {
         [OperationContract]
         int Multiply(int arg1, int arg2);
     }

    2.实现

    class MyCalculator : IScientificCalculator
     {
         public int Add(int arg1, int arg2)
         {
             return arg1 + arg2;
         }
         public int Multiply(int arg1, int arg2)
         {
             return arg1 * arg2;
         }
     }

    3.config配置

    <service name = "MyCalculator">
       <endpoint
          address  = "http://localhost:8001/MyCalculator/"
          binding  = "basicHttpBinding"
          contract = "IScientificCalculator"
      />
     </service>
    4.客户端生成

    [ServiceContract]
     interface ISimpleCalculator
     {
         [OperationContract]
         int Add(int arg1, int arg2);
     }
     class SimpleCalculatorClient : ClientBase<ISimpleCalculator>, ISimpleCalculator
     {
         public int Add(int arg1, int arg2)
         {
             return Channel.Add(arg1, arg2);
         }
         //Rest of the proxy
     }
     
     [ServiceContract]
     interface IScientificCalculator : ISimpleCalculator
     {
         [OperationContract]
         int Multiply(int arg1, int arg2);
     }
     class ScientificCalculatorClient :
                               ClientBase<IScientificCalculator>, IScientificCalculator
     {
         public int Add(int arg1, int arg2)
         {
             return Channel.Add(arg1, arg2);
         }
         public int Multiply(int arg1, int arg2)
         {
             return Channel.Multiply(arg1, arg2);
         }
         //Rest of the proxy
     }


    5.客户端可以指向父级接口,配置文件可以指向同一地址

    <client>
       <endpoint name = "SimpleEndpoint"
          address  = "http://localhost:8001/MyCalculator/"
          binding  = "basicHttpBinding"
          contract = "ISimpleCalculator"
      />
       <endpoint name = "ScientificEndpoint"
          address  = "http://localhost:8001/MyCalculator/"
          binding  = "basicHttpBinding"
          contract = "IScientificCalculator"
      />
     </client>

    6.客户端也可以像接口般继承与调用

    SimpleCalculatorClient proxy1 = new SimpleCalculatorClient(  );
     SimpleCalculatorClient proxy2 = new ScientificCalculatorClient(  );
     ScientificCalculatorClient proxy3 = new ScientificCalculatorClient(  );
  • 相关阅读:
    每天一个JavaScript实例-铺货鼠标点击位置并将元素移动到该位置
    Max-Min Fairness带宽分配算法
    Centos Apache和tomcat集成配置,同一时候支持PHP和JAVA执行
    Linux硬件信息查询命令
    D3DXMatrixMultiply 函数
    垃圾回收GC:.Net自己主动内存管理 上(三)终结器
    使用python抓取CSDN关注人的全部公布的文章
    公司-科技:百度
    公司-科技:阿里巴巴
    公司-科技:腾讯
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1809591.html
Copyright © 2011-2022 走看看