• 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
  • 相关阅读:
    Java数据类型
    Hadoop之MapReduce单词计数经典实例
    亲戚问你每月多少工资?程序员该如何机智回答
    MySQL进阶操作
    MySQL基础操作
    Redis安装教程
    希尔排序(Shell Sort)
    插入排序(Insertion Sort)
    javascriptの循序渐进(一)
    css Animation初体验
  • 原文地址:https://www.cnblogs.com/starcrm/p/1360782.html
Copyright © 2020-2023  润新知