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

 

再启动客户端

posted @   PEPE YU  阅读(474)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2012-09-05 Objective-C 日记⑧ 对象初始化
点击右上角即可分享
微信分享提示