博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

学习笔记-"WCF课程笔记1"

Posted on 2008-03-04 09:23  wuhang  阅读(178)  评论(0编辑  收藏  举报

星期天,装了个VS2008,想学习学习如何使用WCF的,后来看了WebCast的课程,重昨天开始,进行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
    }

}

这个类是服务端提供给客户端的应用类,提供了一个接口给客户端调用!有点像.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()就可以服务啦!