WCF(一)

在学习WCF之前要知道几个术语:

一:终结点

终结点由3个要素组成:契约,绑定,地址。

    1.契约:契约属于一个服务公开接口的一部分。一个服务的契约,定义了服务端公开的服务方法,使用的传输协议,可访问的地址,传输的消息格式等内容。基本上,契约的定义描述了该服务的功能和作用,他告诉SOA系统中的其他结点这个服务是“干什么”的。常见的契约有:服务契约,数据契约,消息契约,错误契约。

    2.绑定:定义了服务与外部通信的方式。它由一组称为绑定元素的元素构造而成,这些元素组合在一起以形成通信基础结构。一个绑定可以包含以下内容。

      @1:通信所使用的协议,如HTTP,TCP,P2P等

      @2:消息编码方式,如纯文本,二进制编码,MTOM等

      @3:通信安全保障策略

      @4:通信堆栈的其他任何要素

    3.地址:每个服务都具有唯一的地址。

二:宿主

服务必须承载于某个进程中。意思是要想使服务能够正常工作,必须为服务提供一个宿主。

接下来看一个完整的Demo

    //服务契约
    [ServiceContract]
    interface IService
    {
        [OperationContract]
        string HelloWorld(string name);//服务操作
    }
    public class Service : IService
    {
        public string HelloWorld(string name)
        {
            return name + "说:HelloWorld";
        }
    }
    /// <summary>
    /// 宿主程序,封装了ServiceHost和其构造过程
    /// </summary>
    public class MyServiceHost : IDisposable
    {
        private ServiceHost _myHost;//提供服务的主机
        public const string BaseAddress = "net.pipe://localhost";//基地址
        public const string HelloWorldServiceAddress = "HelloWorld";//可选地址
        //服务契约定义的类型
        public static readonly Type ContractType = typeof(ConsoleAppWCF.IService);
        //服务契约实现的类型
        public static readonly Type ServiceType = typeof(ConsoleAppWCF.Service);
        //绑定:指定客户端和服务端之间的通信所使用的协议
        public static readonly Binding HelloWorldBinding = new NetNamedPipeBinding();

        /// <summary>
        /// 构造ServiceHost对象
        /// </summary>
        public void ConstructServiceHost()
        {
            _myHost = new ServiceHost(ServiceType, new Uri[] {
                new Uri(BaseAddress)
            });
            _myHost.AddServiceEndpoint(ContractType, HelloWorldBinding, HelloWorldServiceAddress);
        }
        /// <summary>
        ///ServiceHost只读属性
        /// </summary>
        public ServiceHost Host
        {
            get
            {
                return _myHost;
            }
        }
        /// <summary>
        /// 打开服务
        /// </summary>
        public void Open()
        {
            Console.WriteLine("启动服务中...");
            _myHost.Open();
            Console.WriteLine("服务已经启动...");
        }
        public MyServiceHost()
        {
            ConstructServiceHost();
        }
        /// <summary>
        /// 实现接口IDisposable的方法
        /// </summary>
        public void Dispose()
        {
            if (_myHost != null)
                (_myHost as IDisposable).Dispose();
        }
    }

打开服务

 static void Main(string[] args)
        {
            using (MyServiceHost host = new MyServiceHost())
            {
                host.Open();
                Console.Read();
            }
        
        }

接下来客户端代码

 [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string HelloWorld(string name);
    }
    /// <summary>
    /// 客户端代理
    /// </summary>
    public class HelloWorldProxy : ClientBase<IService>, IService
    {
        public static readonly Binding helloWorldBinding = new NetNamedPipeBinding();
        public static readonly EndpointAddress helloWorldAddress =
            new EndpointAddress(new Uri("net.pipe://localhost/HelloWorld"));
        public HelloWorldProxy() : base(helloWorldBinding, helloWorldAddress)
        {

        }
        public string HelloWorld(string name)
        {
            return Channel.HelloWorld(name);
        }
    }

客户端启动代码

 static void Main(string[] args)
        {
            using (HelloWorldProxy proxy = new HelloWorldProxy())
            {
                Console.WriteLine(proxy.HelloWorld("小猫"));
                Console.Read();
           }
        }

注意:这是两个控制台程序,先启动服务端,在启动客户端。

参考书献:精通C#5.0与.net 4.5高级编程,作者:丁士锋  清华出版社。图书馆借的书,嘻嘻

posted @ 2017-11-29 11:14  咸鱼戏花猫  阅读(239)  评论(0编辑  收藏  举报