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
  • 相关阅读:
    mac os programming
    Rejecting Good Engineers?
    Do Undergrads in MIT Struggle to Obtain Good Grades?
    Go to industry?
    LaTex Tricks
    Convert jupyter notebooks to python files
    How to get gradients with respect to the inputs in pytorch
    Uninstall cuda 9.1 and install cuda 8.0
    How to edit codes on the server which runs jupyter notebook using your pc's bwroser
    Leetcode No.94 Binary Tree Inorder Traversal二叉树中序遍历(c++实现)
  • 原文地址:https://www.cnblogs.com/starcrm/p/1360782.html
Copyright © 2011-2022 走看看