WCF(一) 创建第一个WCF

定义服务契约-创建宿主程序-创建客户端程序访问服务

namespace
HelloService { /// <summary> /// 服务契约 /// </summary> [ServiceContract] public interface IHelloService { /// <summary> /// 服务操作 /// </summary> /// <param name="name"></param> /// <returns></returns> [OperationContract] string SayHello(string name); } }

类HelloService继承接口IHelloService

namespace HelloService
{
   public class HelloService:IHelloService
    {
        /// <summary>
        /// 打招呼
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        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();
            }
        }
    }
    /// <summary>
    /// 硬编码服务契约
    /// </summary>
    interface IService
    {
        /// <summary>
        /// 服务操作
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [OperationContract]
        string Say(string name);
    }

    /// <summary>
    /// 客户端代理类型
    /// </summary>
    class HelloProxy : ClientBase<HelloService.IHelloService>, IService
    {
        /// <summary>
        /// 硬编码定义绑定
        /// </summary>
        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
    {
        /// <summary>
        /// 定义一个服务对象
        /// </summary>
        private ServiceHost _myHost;

        public ServiceHost MyHost
        {
            get { return _myHost; }
            set { _myHost = value; }
        }

        /// <summary>
        /// 定义一个基地址
        /// </summary>
        public const string BaseAddress = "net.pipe://localhost";

        /// <summary>
        /// 可选地址
        /// </summary>
        public const string HelloServiceAddress = "Hello";

        /// <summary>
        /// 服务契约实现类型
        /// </summary>
        public static readonly Type ServiceType=typeof(HelloService.HelloService);
        
        /// <summary>
        /// 服务契约接口
        /// </summary>
        public static readonly Type ContractType=typeof(HelloService.IHelloService);

        /// <summary>
        /// 服务定义一个绑定
        /// </summary>
        public static readonly Binding helloBinding = new NetNamedPipeBinding();

        /// <summary>
        /// 构造服务对象
        /// </summary>
        protected void CreateHelloService()
        {
            //创建服务对象
            _myHost = new ServiceHost(ServiceType, new Uri[] { new Uri(BaseAddress)});
            //给当前数组对象添加终结点
            _myHost.AddServiceEndpoint(ContractType, helloBinding, HelloServiceAddress);
        }
        /// <summary>
        /// 打开服务
        /// </summary>
        public void Open()
        {
            Console.WriteLine("开始启动服务!");
            _myHost.Open();
            Console.WriteLine("服务已经启动!");
        }
        /// <summary>
        /// 构造方法
        /// </summary>
        public MyHelloHost()
        {
            CreateHelloService();
        }
        /// <summary>
        /// 销毁对象
        /// </summary>
        public void Dispose() {
            if (_myHost!=null)
            {
                (_myHost as IDisposable).Dispose();
            }
        }
    }
}

要先启动服务HelloServiceHost

再启动客户端


作者:PEPE
出处:http://pepe.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/PEPE/p/3304485.html