博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WCF(一) 创建第一个WCF
阅读量:4543 次
发布时间:2019-06-08

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

定义服务契约-创建宿主程序-创建客户端程序访问服务 namespace HelloService{    ///     /// 服务契约    ///     [ServiceContract]    public interface IHelloService    {        ///         /// 服务操作            ///         ///         /// 
[OperationContract] string SayHello(string name); }}

类HelloService继承接口IHelloService

namespace HelloService{   public class HelloService:IHelloService    {        ///         /// 打招呼        ///         ///         /// 
public string SayHello(string name) { return name + ":say hello!"; } }}

定义一个客户端

namespace HelloClient{    class Program    {        static void Main(string[] args)        {            using (HelloProxy proxy=new HelloProxy())            {                Console.WriteLine(proxy.Say("KAKA"));                Console.Read();            }        }    }    ///     /// 硬编码服务契约    ///     interface IService    {        ///         /// 服务操作        ///         ///         /// 
[OperationContract] string Say(string name); } /// /// 客户端代理类型 /// class HelloProxy : ClientBase
, IService { ///
/// 硬编码定义绑定 /// public static readonly Binding HelloBinding = new NetNamedPipeBinding(); public static readonly EndpointAddress HelloAddress = new EndpointAddress(new Uri("net.pipe://localhost/Hello")); public HelloProxy() : base(HelloBinding, HelloAddress) { } public string Say(string name) { //使用Channel属性对服务进行调用 return Channel.SayHello(name); } }}

 

宿主程序

namespace HelloServiceHost{    class Program    {        static void Main(string[] args)        {            using (MyHelloHost host=new MyHelloHost())            {                host.Open();                Console.Read();            }        }    }    public class MyHelloHost:IDisposable    {        ///         /// 定义一个服务对象        ///         private ServiceHost _myHost;        public ServiceHost MyHost        {            get { return _myHost; }            set { _myHost = value; }        }        ///         /// 定义一个基地址        ///         public const string BaseAddress = "net.pipe://localhost";        ///         /// 可选地址        ///         public const string HelloServiceAddress = "Hello";        ///         /// 服务契约实现类型        ///         public static readonly Type ServiceType=typeof(HelloService.HelloService);                ///         /// 服务契约接口        ///         public static readonly Type ContractType=typeof(HelloService.IHelloService);        ///         /// 服务定义一个绑定        ///         public static readonly Binding helloBinding = new NetNamedPipeBinding();        ///         /// 构造服务对象        ///         protected void CreateHelloService()        {            //创建服务对象            _myHost = new ServiceHost(ServiceType, new Uri[] { new Uri(BaseAddress)});            //给当前数组对象添加终结点            _myHost.AddServiceEndpoint(ContractType, helloBinding, HelloServiceAddress);        }        ///         /// 打开服务        ///         public void Open()        {            Console.WriteLine("开始启动服务!");            _myHost.Open();            Console.WriteLine("服务已经启动!");        }        ///         /// 构造方法        ///         public MyHelloHost()        {            CreateHelloService();        }        ///         /// 销毁对象        ///         public void Dispose() {            if (_myHost!=null)            {                (_myHost as IDisposable).Dispose();            }        }    }}

 

 

要先启动服务HelloServiceHost

 

再启动客户端

转载于:https://www.cnblogs.com/PEPE/p/3304485.html

你可能感兴趣的文章
正则表达式
查看>>
tensorflow models flags 初步使用
查看>>
[.NET] SQL数据分页查询
查看>>
[转]Ext自定义vtype动态验证
查看>>
Java - Java Web - Listener
查看>>
K3Cloud 后台修改账户密码策略
查看>>
Python内置函数
查看>>
第15章 面向对象程序设计
查看>>
C#读写socket的常用方式
查看>>
c语言链表fwrite二进制保存,读取时出现 屯 的问题
查看>>
谷歌Cartographer学习(1)-快速安装测试(转载)
查看>>
lib32gcc1 : Depends: gcc-4.9-base (= 4.9-20140406-0ubuntu1) but 4.9.3-0ubuntu4
查看>>
简单的将字符串转换成日期对象格式
查看>>
HTC G7 搜索和感光按键修改
查看>>
Dll注入经典方法完整版
查看>>
在非主线程中创建窗口
查看>>
查看selenium api的方法
查看>>
移植opencv2.4.2到tiny6410
查看>>
14种植物放入室内的奇异功效,为自己的健康而转
查看>>
全功能Python测试框架:pytest
查看>>