zoukankan      html  css  js  c++  java
  • WCF入门教程2——创建第一个WCF程序

    本节目标

    • 掌握接口
    • 理解契约式编程
    • 创建宿主程序
    • 创建客户端程序访问服务

    什么是接口

    认识一下接口

    必须知道的接口特性

    • 接口不可以被实例化(常作为类型使用)
    • 实现类必须实现接口的所有方法(抽象类除外)
    • 实现类可以实现多个接口(Java,C#中的多继承)
    • 接口中的变量都是静态常量

    理解接口

    定义一个接口是为了遵循同一种规范,便于程序的扩展。
    接口是一种能力
    接口是一种约定
    关键字
    Interface
    public
    abstract

    理解契约式编程

    契约合同能保障双方的利益,对客户来说,合同规定了供应者要做的工作;对供应者来说,合同说明了如果约定的条件不满足,供应者没有义务一定要完成规定的任务。该道理同样也适用于软件. 所以,契约式编程是编程的一种方法。

    引入契约观念之后,这种Client 与 Server 关系被打破,大家都是平等的,你需要我正确提供服务,那么你必须满足我提出的条件,否则我没有义务“排除万难”地保证完成任务。

    WCF服务契约

    服务契约描述了暴露给外部的类型(接口或类)、服务所支持的操作、使用的消息交换模式和消息的格式。每个WCF服务必须实现至少一个服务契约。使用服务契约必须要引用命名空间System.ServiceModel 。

    ServiceContractAttribute:该特性可被用来作用于子类或者接口之上,并允许重复声明。

    OperationContractAttribute:只有定义了该特性的方法才会被放入服务之中。

    1、新建服务程序

    新建项目——类库,这里我们先不直接新建一个WCF服务,而是新建一个类库,命名为HelloService


    添加引用

    删除Class1.cs,然后新建一个接口IHelloService.cs

    [csharp] view plain copy 在CODE上查看代码片派生到我的代码片
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.ServiceModel; //添加命名空间,这是WCF的核心库  
    6.   
    7. namespace HelloService  
    8.   
    9. {  
    10.    [ServiceContract]  
    11.    public interface IHelloService  
    12.    {  
    13.        [OperationContract]  
    14.        string SayHello(string name);  
    15.    }  
    16. }  

    添加HelloService类:

    [csharp] view plain copy 在CODE上查看代码片派生到我的代码片
    1. public class HelloService:IHelloService  
    2.    {  
    3.        public string SayHello(string name)  
    4.        {  
    5.             return "你好,我是:" + name;  
    6.        }  
    7.    }  

    ServiceHost类型:当IISWAS作为宿主程序时,IISWAS会自动创建ServiceHost类型。

    手动创建的基本语法:public ServiceHost(Type serviceType,params Uri[] baseAddresses);

    2、新建宿主

    新建项目——控制台应用程序


    然后添加System.ServiceModel引用,和项目引用HelloService,引用之前的类库项目。

    HelloServiceHost 项目中Program.cs代码如下:

    [csharp] view plain copy 在CODE上查看代码片派生到我的代码片
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;   
    5.   
    6. using System.ServiceModel;  
    7. using System.ServiceModel.Channels; //使用到了绑定   
    8.   
    9. namespace HelloServiceHost  
    10. {  
    11.    class Program  
    12.    {  
    13.        static void Main(string[] args)  
    14.        {  
    15.             using (MyHelloHost host=new MyHelloHost())  
    16.             {  
    17.                 host.Open();  
    18.                 Console. Console.ReadLine();  
    19.             }  
    20.        }  
    21.    }  
    22.   
    23.    public class MyHelloHost:IDisposable  
    24.    {  
    25.        /// <summary>  
    26.        /// 定义一个服务对象  
    27.        /// </summary>  
    28.        private ServiceHost _myHelloHost;   
    29.        public const string BaseAddress = "net.pipe://localhost"; //基地址  
    30.        public const string HelloServiceAddress = "Hello"; //可选地址  
    31.        public static readonly Type ServiceType =typeof(HelloService.HelloService);  //服务契约实现类型  
    32.        public static readonly Type ContractType =typeof(HelloService.IHelloService);  //服务契约接口  
    33.        public static readonly Binding HelloBinding = new NetNamedPipeBinding(); //服务定义一个绑定  
    34.    
    35.        /// <summary>  
    36.        /// 构造方法  
    37.        /// </summary>  
    38.        public MyHelloHost()  
    39.        {  
    40.             CreateHelloServiceHost();  
    41.        }  
    42.   
    43.        /// <summary>  
    44.        /// 构造服务对象  
    45.        /// </summary>  
    46.        protected void CreateHelloServiceHost()  
    47.        {  
    48.             _myHelloHost = new ServiceHost(ServiceType, new Uri[] { new Uri(BaseAddress) });//创建服务对象  
    49.            _myHelloHost.AddServiceEndpoint(ContractType, HelloBinding,HelloServiceAddress); //添加终结点  
    50.        }  
    51.   
    52.        /// <summary>  
    53.        /// 打开服务方法  
    54.        /// </summary>  
    55.        public void Open()  
    56.        {  
    57.             Console.WriteLine("开始启动服务...");  
    58.             _myHelloHost.Open();  
    59.             Console.WriteLine("服务已启动");  
    60.        }  
    61.   
    62.        /// <summary>  
    63.        /// 销毁服务宿主对象实例  
    64.        /// </summary>  
    65.        public void Dispose()  
    66.        {  
    67.             if (_myHelloHost != null)  
    68.                 (_myHelloHost asIDisposable).Dispose();  
    69.        }  
    70.    }  
    71.   
    72. }  

    3、新建客户端调用程序

    新建项目——控制台应用程序

    HelloClient项目中Program.cs代码如下:

    [csharp] view plain copy 在CODE上查看代码片派生到我的代码片
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;   
    5.   
    6. using System.ServiceModel;  
    7. using System.ServiceModel.Channels;  
    8. using HelloService;  
    9.    
    10. namespace HelloClient  
    11. {  
    12.    class Program  
    13.    {  
    14.        static void Main(string[] args)  
    15.        {  
    16.           using(HelloProxy proxy=new HelloProxy())  
    17.             {  
    18.                 //利用代理调用方法  
    19.                Console.WriteLine(proxy.Say("郑少秋"));  
    20.                Console.ReadLine();  
    21.             }  
    22.        }  
    23.    }  
    24.   
    25.    [ServiceContract]  
    26.    interface IService  
    27.    {  
    28.        [OperationContract]  
    29.        string Say(string name);  
    30.    }  
    31.   
    32.    class HelloProxy:ClientBase<IHelloService>,IService  
    33.    {  
    34.        public static readonly Binding HelloBinding = new NetNamedPipeBinding();  //硬编码定义绑定  
    35.        //硬编码定义地址 注意:这里要和之前服务定义的地址保持一直  
    36.        public static readonly EndpointAddress HelloAddress =new EndpointAddress(new Uri("net.pipe://localhost/Hello"));  
    37.        public HelloProxy() : base(HelloBinding,HelloAddress) { } //构造方法  
    38.   
    39.        public string Say(string name)  
    40.        {  
    41.             //使用Channel属性对服务进行调用  
    42.             return Channel.SayHello(name);  
    43.        }  
    44.    }  
    45. }  

    先运行HelloServiceHost

    然后运行HelloClient

  • 相关阅读:
    javascript星级评分
    JavaScript input框输入实时校验
    ios 软键盘顶起这个页面
    JavaScript 属性操作
    ios隐藏软键盘
    SVN-Attempted to lock an already-locked dir错误
    javascript 判断是否使用的是ipad
    session不一定非得要cookie开启才能使用。也可以使用get传递参数
    [Linux]非外网环境下配置lnmp心得
    session和cookie的总结
  • 原文地址:https://www.cnblogs.com/jiekzou/p/5325314.html
Copyright © 2011-2022 走看看