zoukankan      html  css  js  c++  java
  • WCF笔记:ServiceContract继承

    主机:

    using System;
    using System.ServiceModel;

    namespace InheritanceDemo
    {
       [ServiceContract]
       interface ISimpleCalculator
       {
          [OperationContract]
          int Add(int arg1,int arg2);
       }

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

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

    主机控制台:

    using System;
    using System.ServiceModel;

    namespace InheritanceDemo
    {
       class Program
       {
          public static void Main()
          {
             ServiceHost serviceHost = new ServiceHost(typeof(MyCalculator),new Uri("http://localhost:8000/"));
             serviceHost.Open();

             Console.WriteLine("Press ENTER to shut down service.");
             Console.ReadLine();
            
             serviceHost.Close();
          }
       }
    }

    客户端:

    using System.ServiceModel;


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

    //局部类
    public partial class SimpleCalculatorClient : ClientBase<ISimpleCalculator>,ISimpleCalculator
    {
       public SimpleCalculatorClient()
       {}

       public SimpleCalculatorClient(string endpointConfigurationNamee) : base(endpointConfigurationNamee)
       {}

       public int Add(int arg1,int arg2)
       {
           return this.Channel.Add(arg1, arg2);
       }
    }

    using System.ServiceModel;

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



    public partial class ScientificCalculatorClient : ClientBase<IScientificCalculator>,IScientificCalculator
    {
       public ScientificCalculatorClient()
       {}

       public ScientificCalculatorClient(string endpointConfigurationName) : base(endpointConfigurationName)
       {}

       public int Add(int arg1,int arg2)
       {
          return Channel.Add(arg1,arg2);
       }

       public int Multiply(int arg1,int arg2)
       {
          return Channel.Multiply(arg1,arg2);
       }
    }

    调用:

    using System;
    using System.ServiceModel;

    namespace InheritanceDemo
    {
       class MyClient
       {
          static void Main(string[] args)
          {
             int result;

             SimpleCalculatorClient proxy1 = new SimpleCalculatorClient();

             result = proxy1.Add(1,2);

             Console.WriteLine("1 + 2 = " + result);

             proxy1.Close();

             ScientificCalculatorClient proxy2 = new ScientificCalculatorClient();
            
             result = proxy2.Add(3,4);
             Console.WriteLine("3 + 4 = " + result);
            
             result = proxy2.Multiply(5,6);
             Console.WriteLine("5 * 6 = " + result);
            
             proxy2.Close();

             Console.ReadLine();        
          }
       }
    }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    网文阅读笔记
    UUID
    各种网站
    项目
    常用正则表达式
    Struts 2.0 HelloWorld
    接口与抽象类(深入多态)
    #define的优点/volatile的好处
    基本套接口编程
    大小端判断及相互转化
  • 原文地址:https://www.cnblogs.com/starcrm/p/1360782.html
Copyright © 2011-2022 走看看