主机:
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();
} } }