您的位置:首页 > 其它

WCF入门教程2——创建第一个WCF程序

2016-03-27 16:13 661 查看
本节目标

掌握接口

理解契约式编程

创建宿主程序

创建客户端程序访问服务

什么是接口

认识一下接口





必须知道的接口特性


接口不可以被实例化(常作为类型使用)

实现类必须实现接口的所有方法(抽象类除外)

实现类可以实现多个接口(Java,C#中的多继承)

接口中的变量都是静态常量

理解接口

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

理解契约式编程

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



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

WCF服务契约


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

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

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

1、新建服务程序

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



添加引用





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

[csharp] view plain copy





using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel; //添加命名空间,这是WCF的核心库

namespace HelloService

{

[ServiceContract]

public interface IHelloService

{

[OperationContract]

string SayHello(string name);

}

}

添加HelloService类:

[csharp] view plain copy





public class HelloService:IHelloService

{

public string SayHello(string name)

{

return "你好,我是:" + name;

}

}

ServiceHost类型:当IIS活WAS作为宿主程序时,IIS和WAS会自动创建ServiceHost类型。

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

2、新建宿主

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



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



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

[csharp] view plain copy





using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Channels; //使用到了绑定

namespace HelloServiceHost

{

class Program

{

static void Main(string[] args)

{

using (MyHelloHost host=new MyHelloHost())

{

host.Open();

Console. Console.ReadLine();

}

}

}

public class MyHelloHost:IDisposable

{

/// <summary>

/// 定义一个服务对象

/// </summary>

private ServiceHost _myHelloHost;

public const string BaseAddress = "net.pipe://localhost"; //基地址

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

public static readonly Type ServiceType =typeof(HelloService.HelloService); //服务契约实现类型

public static readonly Type ContractType =typeof(HelloService.IHelloService); //服务契约接口

public static readonly Binding HelloBinding = new NetNamedPipeBinding(); //服务定义一个绑定

/// <summary>

/// 构造方法

/// </summary>

public MyHelloHost()

{

CreateHelloServiceHost();

}

/// <summary>

/// 构造服务对象

/// </summary>

protected void CreateHelloServiceHost()

{

_myHelloHost = new ServiceHost(ServiceType, new Uri[] { new Uri(BaseAddress) });//创建服务对象

_myHelloHost.AddServiceEndpoint(ContractType, HelloBinding,HelloServiceAddress); //添加终结点

}

/// <summary>

/// 打开服务方法

/// </summary>

public void Open()

{

Console.WriteLine("开始启动服务...");

_myHelloHost.Open();

Console.WriteLine("服务已启动");

}

/// <summary>

/// 销毁服务宿主对象实例

/// </summary>

public void Dispose()

{

if (_myHelloHost != null)

(_myHelloHost asIDisposable).Dispose();

}

}

}

3、新建客户端调用程序

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





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

[csharp] view plain copy





using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Channels;

using HelloService;

namespace HelloClient

{

class Program

{

static void Main(string[] args)

{

using(HelloProxy proxy=new HelloProxy())

{

//利用代理调用方法

Console.WriteLine(proxy.Say("郑少秋"));

Console.ReadLine();

}

}

}

[ServiceContract]

interface IService

{

[OperationContract]

string Say(string name);

}

class HelloProxy:ClientBase<IHelloService>,IService

{

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);

}

}

}



先运行HelloServiceHost



然后运行HelloClient

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: