WCF之客户端

来源:互联网 发布:淘宝店现在生意怎么样 编辑:程序博客网 时间:2024/05/17 01:34

终于等到你,其实时间也不长,代码部分已经完成,网上资源也很多,复制粘贴删减,分分钟来个秒杀;打开链接

客户端来了,由于应用程序之间是靠Endpoint来通信的,那么我们在Client端也必须定义终结点,只有当Client与Service的终结点完全匹配的时候(配置好同样的终结点信息,然后添加服务的引用,最后调用相应的方法)才能进行通信。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.ServiceModel;using System.ServiceModel.Channels;using SayHelloService;namespace HelloClient{    class Program    {        static void Main(string[] args)        {            using (HelloProxy proxy = new HelloProxy())            {                //利用代理调用服务                Console.WriteLine(proxy.Say("lhc"));                Console.Read();            }        }    }    //硬编码定义服务契约    [ServiceContract]    interface IService    {        //服务操作        [OperationContract]        String Say(String name);    }    //ClientBase可用泛型,创建服务端对象 ;IService实现    class HelloProxy : ClientBase<IHelloService>, IService    {        //硬编码 定义 绑定        public static readonly Binding HelloBinding = new NetNamedPipeBinding();        //定义 基地址 必须与服务端地址匹配(协议+地址)  宿主中的BaseAddress+HelloServiceAddress        public static readonly EndpointAddress HelloAddress=new EndpointAddress(new Uri("net.pipe://localhost/Hello"));        //base重写        public HelloProxy() : base(HelloBinding, HelloAddress){}        public String Say(string name)        {            //通过Channel调用服务            return Channel.SayHello(name);        }    }   }
运行的话要先开启服务


才能调用



后语:

客户端——宿主——服务端:endpoint ,现在貌似理清了,WCF学习中


0 0
原创粉丝点击