一、创建一个类库程序,定义服务协定IOperationService和服务实现OperationService。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace ServiceLib { [ServiceContract] public interface IOperationService { [OperationContract] int Add(); [OperationContract] int Multi(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace ServiceLib { public class OperationService:IOperationService { public int Add() { return 5; } public int Multi() { return 10; } } }
二、创建一个控制台程序作为宿主。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Channels; namespace MyServiceHost { class Program { static void Main(string[] args) { using (HostService hostService = new HostService()) { hostService.OpenService(); Console.ReadLine(); } } public class HostService:IDisposable{ /// <summary> /// 定义服务对象 /// </summary> private ServiceHost host; public HostService(){ this.CreateOperationService(); } public ServiceHost Host { get { return host; } } /// <summary> /// 基地址 /// </summary> public const string BASEADDRESS = "net.pipe://localhost"; //或者http、net.tcp 等 /// <summary> /// 备用地址 /// </summary> public const string OPERATIONSERVICEADDRESS = "Operation"; public static readonly Binding OperationBingding = new NetNamedPipeBinding(); /// <summary> /// 定义服务类型 /// </summary> public static readonly Type ServiceType=typeof(ServiceLib.OperationService); /// <summary> /// 接口类型 /// </summary> public static readonly Type ContractType = typeof(ServiceLib.IOperationService); /// <summary> /// 构建服务对象 /// </summary> protected void CreateOperationService() { host = new ServiceHost(ServiceType, new Uri[] { new Uri(BASEADDRESS) }); //为服务对象添加终结点 host.AddServiceEndpoint(ContractType, OperationBingding, OPERATIONSERVICEADDRESS); } /// <summary> /// 释放空间 /// </summary> public void OpenService() { Console.WriteLine("开始启动服务...."); host.Open(); Console.WriteLine("启动服务成功...."); } public void Dispose() { if (host != null) { (host as IDisposable).Dispose(); } } } } }
三、创建一个控制台程序作为客户端,来测试服务的调用。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Channels; using ServiceLib; namespace ClientConsole { class Program { static void Main(string[] args) { using (OperationProxy proxy = new OperationProxy()) { Console.WriteLine(proxy.Add().ToString()); Console.WriteLine(proxy.Multi().ToString()); Console.ReadLine(); } } /// <summary> /// 硬编码定义服务协定 /// </summary> [ServiceContract] interface IService { [OperationContract] int Add(); [OperationContract] int Multi(); } class OperationProxy : ClientBase<IOperationService>, IService { /// <summary> /// 硬编码定义绑定 /// </summary> public static readonly Binding OperationBinding = new NetNamedPipeBinding(); public static readonly EndpointAddress OperationAddress = new EndpointAddress(new Uri("net.pipe://localhost/Operation")); //Operation 表示宿主中定义的备选地址 public OperationProxy() : base(OperationBinding, OperationAddress) { } public int Add() { return Channel.Add(); } public int Multi() { return Channel.Multi(); } } } }
时间: 2024-10-04 23:16:22