您的位置:首页 > 其它

WCF入门演练(一)

2011-11-19 16:19 375 查看
随着微软VS2008的强势推出,其中的新技术也层出不穷.每一项新技术的诞生,都是一场革命.在这个SOA的时代,跟随微软的脚步,我们看到了WCF(windows

communication foundation).据说WCF解决了以往技术中的一些列问题(如web service中的安全问题,.net remoting的跨平台问题等),做为初学者,我没资格去讨

论,既然有WCF的出现,就有它存在的理由.以下开始演练我的WCF:

现在进入第一个WCF实例,具体的WCF概念方面的东西就不多说了,一切从代码着手.

准备环境:VS2008

打开vs2008

新建一个空白解决方案:然后命名 这里我用的是WCFDemo

然后向解决方案中添加一个ClassLibrary和两个Console Application, ClassLibrary的名称为:MyWcf ,Console

Application的名称分别为Server和Client.好了准备工作做好了 我们看下解决方案的截图



首先 我们先编写类库 也就是WCF的服务程序.打开MyWcf,添加一个接口类,命名为IHelloMessage,然后给Wcf类库添加System.ServiceModel类库的引

用,IHelloMessage的代码为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace MyWcf
{
//使用特性表示服务契约。。。
[ServiceContract(Namespace="http://trampt.cnblogs.com")]
public interface IHelloMessage
{
//要暴露给客户端的操作
[OperationContract]
string Hello(string message);
}
}


我们开始编写服务端(Server)程序,打开Server控制台应用程序,我们添加System.ServiceModel和刚新建类库MyWcf的引用.然后我们在Server的Program.cs中

实现MyWcf中的IHelloMessage接口 然后给客户端暴露服务,也就是提供服务给客户端,具体代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using MyWcf;
namespace Server
{
//实现MyWcf中IHelloMessage接口,供客户端调用
public class HelloMessage : IHelloMessage
{
public string Hello(string message)
{
return String.Format("you receive: {0} at :{1}", message, DateTime.Now.ToString());
}
}
class Program
{
static void Main(string[] args)
{
//创建服务宿主ServiceHost
using(ServiceHost host=new ServiceHost(typeof(HelloMessage)))
{
//通过EndPoint将服务暴露给客户端 A:服务地址 B:通讯协议 C:服务契约
//以下代码中A为"net.tcp://localhost:8090/Hello" B:new NetTcpBinding() C:typeof(MyWcf.IHelloMessage)
host.AddServiceEndpoint(typeof(MyWcf.IHelloMessage),new NetTcpBinding() ,"net.tcp://localhost:8090/Hello");
//开启服务
host.Open();
Console.ReadLine();
}
}
}
}




然后我们在客户端添加System.ServiceModel和MyWcf类库的引用,然后编程实现对服务器端服务的调用.客户端主要是通过创建服务通道获取服务代理实现对服务器端服务的引用:代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using MyWcf;
namespace Client
{
class Program
{
static void Main(string[] args)
{
//创建服务通道 获取服务代理
MyWcf.IHelloMessage proxy = ChannelFactory<IHelloMessage>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:8090/Hello"));
//通过代理调用服务器端方法
string s = proxy.Hello("hello!");
Console.WriteLine(s);
Console.ReadLine();
}
}
}


所有准备工作就绪,我们就开始我们的测试.首先启动Server,然后我们启动Client来看下输出结果.



好了 第一个WCF程序已经调式运行成功了.....

============================================================================分割线

WCF(Windows Communication Foundation),感觉微软把异步通信的东西全部汇集成一块了,我学过.net remoting,Web Services,WCF里面很多代码N像,所以个人感觉WCF是把微软的异步通信的东西全部整合一起了,所以部署和使用起来,不管客户端,用的是IE浏览器,还是客户端应用程序,只要指定好目标地址和端口就可以访问目标对象的代理。

下面是代码:

using System;
using System.ServiceModel;

namespace HelloIndigo
{
[ServiceContract(Namespace="http://www.monkeyfu.net")]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo(string message);
}

public class HelloIndigoService : IHelloIndigoService
{

IHelloIndigoService Members#region IHelloIndigoService Members

public string HelloIndigo(string message)
{
return string.Format("Receivied message at{0}:{1}", DateTime.Now, message);
}

#endregion
}
}


这个类是服务端提供给客户端的应用类,提供了一个接口给客户端调用!有点像.net Remoting中的服务对象。提供的接口需要加上属性“ServiceContract”,接口方法上面需要加上“OperationContract”属性。命名空间中需要引用"System.ServiceModel"。

上面的代码中,包括了一个接口"IHelloIndigoService"和继承这个接口的类"HelloIndigoService",这个类没什么好看的就是实现了接口,倒是要看看接口,很明显可以看出来异步调用的时候客户端是在调用接口,接口上面有个属性叫"ServiceContract",查了MSDN后(哎。中文的打不开)上面写了他的作用是“The
ServiceContract objects represents a contract that specifies the direction and type of messages in a conversation”,由于英语水平有限,采用Google翻译来说:"ServiceContract这个类代表了,在指明方向和类型等信息后会话时用的合同“,嗯,说简单些就是,2个程序相互交互必须有共知的对象和交换的方向(Server,Client)然后这个类就相当于一个合同,让交互双方都知道,都可以看到,用到的合同,有点像.netremoting的[Serializable],不过两者有着本质的区别。我的理解就是声明了[ServiceContract]属性后相当于向外界发布了该类(接口),外界就可以调用该类(接口)啦!

接口方法里面有个"OperationContract"查了一下"OperationContract"学名又叫" OperationContractAttribute",又是一个类,这个类估计跟ServiceContract差不多,只是一个用于类,一个用于方法,英文解释就是“Indicates that
a method defines an operation that is part of a service contract in a Windows Communication Foundation (WCF) application.”用Google翻译就是“这个方法定义了一个行动,用于WCF应用程序的交互。果然是用于公共交互的,跟Web Service里面的WebMethod差不多,下面还有个Remark,说是“Apply the
OperationContractAttribute to a method to indicate that the method implements a service operation as part of a service contract (specified by a

ServiceContractAttribute attribute). "嗯,大概意思是说这个东西需要用在声明了ServiceContractAttribute下面。用了Google翻译,越翻越糊涂。。。。

嗯,总的来说,就是这个类,暴露了个接口,这个接口应用程序都可以访问。

再来看一下Host端,服务端代码:

using System;
using System.Collections.Generic;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Dispatcher;

namespace Host
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService)))
{
host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new NetTcpBinding(), "net.tcp://localhost:9000/HelloIndigo");
host.Open();
Console.ReadLine();
}
}
}
}


服务端代码更简单,(怎么看怎么像.net remoting的代码。。。。)

ServiceHost类MSDN 上面说“Provides a host for services.”,这句话简单,为多个服务提供了host,它的构造函数有三个,分别是:

ServiceHost()Initializes a new instance of the
ServiceHost class.
初始化一个ServiceHost的新实例。

ServiceHost(Object,
array<Uri>)
Initializes a new instance of the
ServiceHost class with the instance of the service and its base addresses specified.
初始化一个基于地址的新实例

ServiceHost(Type,
array<Uri>)
Initializes a new instance of the
ServiceHost class with the type of service and its base addresses specified.
初始化一个基础类型及它的地址

ServiceHost类大概也就是用来提供服务的类吧,看后面的代码,用host.AddServiceEndpoint用来天加了,新的Endpoint,哎,极其像.netremoting,就像一座大楼,添加了一个服务前台,这个服务前台指定了服务的地点“net.tcp://localhost:9000/HelloIndigo”,服务的内容“typeof(HelloIndigo.IHelloIndigoService)”,采用的通信渠道“new
NetTcpBinding()”等等,然后host.open()就可以服务啦!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: