wcf
http://www.cnblogs.com/artech/archive/2007/02/26/656901.html_ _[原创]
我的WCF之旅(1):创建一个简单的WCF程序
<http://www.cnblogs.com/artech/archive/2007/02/26/656901.html>
http://www.cnblogs.com/jun1st/archive/2007/12/25/1014690.html
http://www.cnblogs.com/wayfarer/archive/2007/11/02/947095.html
WCF 之旅——1
<http://www.cnblogs.com/jun1st/archive/2007/12/25/1014690.html>
/1.什么是WCF?/
根据微软官方的解释,WCF(之前的版本名为“Indigo”)是使用托管代码建
立和运行面向服务(Service Oriented)应用程序的统一框架。它使得开发者能够
建立一个跨平台的安全、可信赖、事务性的解决方案,且能与已有系统兼容协作。
WCF是微软分布式应用程序开发的集大成者,它整合了.Net平台下所有的和分布式
系统有关的技术,例如.Net Remoting、ASMX、WSE和MSMQ。
WCF是微软重点介绍的产品,因此也推出了专门的官方网站
(http://windowscommunication.net <http://windowscommunication.net/>),
该网站有最新的WCF新闻发布,以及介绍WCF的技术文档和样例代码。
/2. WCF的ABC/
每一篇入门性质的介绍WCF的文章,都会提到ABC:Address, Binding,
Contract。
* Address: 每一个WCF的Service都有一个唯一的地址。这个地址给出了Service
的地址和传输协议(Transport Protocol)
* Binding: 通信(Communication)的方式很多,同步的request/reply模式,非同
步的fire-and-forget模式。消息可以单向或者双向的发送接收,可以立即发
送或者把它放入到某一个队列中再处理。所供选择的传输协议也有Http,
Tcp,P2P, IPC等。当要考虑Client/Server如何进行通讯的时候,除了考虑以
上提到的几点之外,还有其它很多需要考虑的因素,如安全,性能等。因此,
简单来说,Binding只不过是微软提供的一组考虑比较周全、常用的封装好的
通信方式。
* Contract:Contract描述了Service能提供的各种服务。Contract有四种,包括
Service Contract, Data Contract, Fault Contract和Message Contract
3. Endpoint
每一个Service都需要具备ABC三个元素,而WCF把这三者之间的关系规范化为
Endpoint.
image
<http://www.cnblogs.com/images/cnblogs_com/jun1st/WindowsLiveWriter/WCF1_12F2A/image_6.png>
4. 简单的例子
每一个Service都需要有一个host。这个host的形式可以是多种多样,可以
是WinForm Application, Console Application,也可以IIS,或者是
WAS(Vista),Windows Service等。
首先,来定义Service的Contract.
[ServiceContract]
public interface IHelloService
{
[OperationContract]
void SayHello();
[OperationContract]
string SayHelloToEmployee(Employee employee);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations
[DataContract]
public class Employee
{
private string firstName;
private string lastName;
[DataMember]
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
[DataMember]
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
}
在定义好了Service Contract和Data Contract之后,还需要定义另外两个元
素,Address和Binding。这两者都可以通过编程和配置文件来控制。这里就用配置
文件的方式了,把这些代码写入host程序(Console)的App.config文件中:
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Default" >
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="HelloServiceLibrary.HelloService" behaviorConfiguration="Default">
<host>
<baseAddresses>
<add baseAddress="http://localhost/HelloService"/>
</baseAddresses>
</host>
<endpoint address="net.tcp://localhost/HelloService01" binding="netTcpBinding"
bindingConfiguration="" contract="HelloServiceLibrary.IHelloService" />
<endpoint address="" binding="wsHttpBinding"
contract="HelloServiceLibrary.IHelloService" ></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
再通过Servicehost提供的方法Open(),就可以启动Service了。
Client端要调用Service,都需要通过Proxy来完成,Proxy可以用VS2005或者
Windows SDK提供的工具SvcUtil来完成。
写的不好,请大家包涵,轻拍
不知道如何添加附件,要源代码的请留下email