文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>WCF Basic(2)-服务契约继承

WCF Basic(2)-服务契约继承

时间:2010-08-26  来源:Clingingboy


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(  );
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载