博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WCF笔记:ServiceContract继承
阅读量:4135 次
发布时间:2019-05-25

本文共 2333 字,大约阅读时间需要 7 分钟。

主机:

 

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(""));
         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();        

      }
   }
}

 

转载地址:http://tspvi.baihongyu.com/

你可能感兴趣的文章
Linux将服务设置为开机自启,linux启动VUE项目,设置VUE项目自启
查看>>
JAVA中判断数据不为空后执行数据操作、防止空指针报错的工具类,safes工具类
查看>>
TypeError: Cannot read property ‘parseComponent‘ of undefined解决办法-VUE
查看>>
kingbase逻辑备份报错解决-sys_dump:
查看>>
java8-Stream流的介绍\创建\基本操作\
查看>>
我们写的程序就像我们的孩子
查看>>
JAVA文件批量下载打成压缩包
查看>>
JAVA列表转树状结构-列表拼装树状tree,递归,hutool,效率
查看>>
Linux解决上传的文件访问不到,nginx访问文件403,新建的文件夹没有读写权限
查看>>
io.minio.errors.ErrorResponseException: Access denied
查看>>
使用反射在 ArrayList<Integer> 集合中添加一个字符串数据;
查看>>
mysql 中union和union的区别和使用方法
查看>>
mysql 数据库三大设计结构,三大范式概念
查看>>
mysql中四大连接 内连接、左外连接、右外连接、外连接
查看>>
java中 @Test注解的使用和其他成员
查看>>
使用java将mp3文件写入mysql数据库中
查看>>
使用java将数据库文件复制到本地磁盘中
查看>>
mysql事物处理的四大特征和简单用法
查看>>
jsoup爬虫项目基础用法,如何用jsoup从网上爬东西
查看>>
js中onchange事件举例用法
查看>>