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
  • 相关阅读:
    tensorflow 2.0 学习 (十) 拟合与过拟合问题
    tensorflow 2.0 学习 (九) tensorboard可视化功能认识
    tensorflow 2.0 学习 (八) keras模块的认识
    tensorflow 2.0 学习 (七) 反向传播代码逐步实现
    tensorflow 2.0 学习 (六) Himmelblua函数求极值
    tensorflow 2.0 学习 (五)MPG全连接网络训练与测试
    arp协议简单介绍
    Pthread spinlock自旋锁
    线程和进程状态
    内核态(内核空间)和用户态(用户空间)的区别和联系·
  • 原文地址:https://www.cnblogs.com/starcrm/p/1360782.html
Copyright © 2011-2022 走看看