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(  );
  • 相关阅读:
    临时禁用大型列表上的列表视图阈值
    临时禁用大型列表上的列表视图阈值
    SharePoint Foundation 2010 托管客户端对象模型概述 创建 Windows 控制台托管的客户端对象模型应用程序
    如何隐藏MOSS2010的网站操作菜单
    MOSS2010的列表视图参数设置以及列表记录样式设置
    使用SharePoint Server 2010搜索PDF文档
    Sharepoint2010如何使用Linq to Sharepoint
    水晶報表字段超鏈結4/28
    4月21自考4/23
    [轉]統籌方法華羅庚
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1809591.html
Copyright © 2011-2022 走看看