WCF开发框架之ICommunicationObject 对象详解
WCF开发框架之ICommunicationObject 对象详解
ICommunicationObject 接口是WCF中所有通信对象(信道,信道工厂,信道监听器,等等)的基础。打算创建自定义信道或者直接使用信道的开发人员需要了解这个接口。WCF中的通信对象需要实现一个特殊的状态机。状态机表示了所有通信对象的状态变化。这种情况就像其他通信对象(比如,套接字)所处理的那样。 ICommunicationObject接口(还有与它相关联的方法,状态和事件)的目的是为了实现状态机。这允许WCF能够将按同样的方式处理通信对 象,并让他们下层实现与抽象层分离。
扫一扫加作者微信
ICommunicationObject 接口
C# Code:
public interface ICommunicationObject
{
CommunicationState State { get; }
event EventHandler Opening;
event EventHandler Faulted;
event EventHandler Closing;
event EventHandler Closed;
event EventHandler Opened;
void Abort();
IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state);
IAsyncResult BeginClose(AsyncCallback callback, object state);
IAsyncResult BeginOpen(TimeSpan timeout, AsyncCallback callback, object state);
IAsyncResult BeginOpen(AsyncCallback callback, object state);
void Close(TimeSpan timeout);
void Close();
void EndClose(IAsyncResult result);
void EndOpen(IAsyncResult result);
void Open(TimeSpan timeout);
void Open();
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
CommunicationState State { get; }
event EventHandler Opening;
event EventHandler Faulted;
event EventHandler Closing;
event EventHandler Closed;
event EventHandler Opened;
void Abort();
IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state);
IAsyncResult BeginClose(AsyncCallback callback, object state);
IAsyncResult BeginOpen(TimeSpan timeout, AsyncCallback callback, object state);
IAsyncResult BeginOpen(AsyncCallback callback, object state);
void Close(TimeSpan timeout);
void Close();
void EndClose(IAsyncResult result);
void EndOpen(IAsyncResult result);
void Open(TimeSpan timeout);
void Open();
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
ICommunicationState状态
C# Code:
public enum CommunicationState
{
Created = 0,
Opening = 1,
Opened = 2,
Closing = 3,
Closed = 4,
Faulted = 5
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
Created = 0,
Opening = 1,
Opened = 2,
Closing = 3,
Closed = 4,
Faulted = 5
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
CommunicationState枚举列举了通信对象改变的6个状态。所有通信对象的初始状态是Created.这是通信对象被初始化时的状 态。所有通信对象的最后状态时Closed.在这个方式中,在ICommunicationObject接口上调用的方法将通信对象从一个状态转移到另一 个状态。比如,Open() 方法被调用来传输一个通信对象并使其的状态从Created变成Opened。图3.5显示了一个显示了通信对象的状态和通信对象状态切换的示意图。
通信对象的一个例子是ClientBase<>类,是通过Visual Studio 的添加服务引用或者svcutil.exe生成的客户端的基础实现。
注意: 不能重用客户端
当一个通信对象已经由Opened状态变成Closing或者Faulted状态后就无法再变回去。这意味着通信状态不能回到Opened状态除非首先重建通信对象。因此,当客户端被关闭(当他们的状态时Closed时)以后它们需要被重新创建。
ICommunicationObject接口支持五个事件(Opening, Opened, Closing, Closed和Faulted).这些事件用来通知代码状态改变。
提示:客户端通知
应用程序维护一个到客户端代理的引用是很平常的。当客户端代理进入Faulted 状态(最后是Closed状态)时,使用状态转换事件来提醒是很重要的,因为这样客户端和服务端的通信才能保持。
使用ICommunicationObject接口时,通常通过把一个已有的通信对象强制转换成接口来获取对 ICommunicationObject暴露的事件和方法的访问能力。然后,还有些情况你需要创建一个新的通信对象来扩展WCF的能力。这种情况 下,WCF提供了一个称为CommunicationObject的抽象的基类,它提供了ICommunicationObject接口和相关状态机的实 现。列表3.10显示了一个由svcutil.exe生成的StockQuoteServiceClient类。这个客户的继承自 ClientBase<>类。代码显示了客户端被强转成一个ICommunicationObject接口以便于我们可以接受通信对象。
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace EssentialWCF
{
class Program
{
static void Main( string [] args)
{
string symbol = " MSFT " ;
double value;
StockQuoteServiceClient client = new StockQuoteServiceClient();
ICommunicationObject commObj = (ICommunicationObject)client;
commObj.Closed += new EventHandler(Closed);
commObj.Faulted += new EventHandler(Faulted);
value = client.GetQuote(symbol);
Console.WriteLine( " {0}@{1} " , symbol, value);
Console.ReadLine();
}
static void Closed( object sender, EventArgs e)
{
// Handle Closed Event
}
static void Faulted( object sender, EventArgs e)
{
// Handle Faulted Event
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace EssentialWCF
{
class Program
{
static void Main( string [] args)
{
string symbol = " MSFT " ;
double value;
StockQuoteServiceClient client = new StockQuoteServiceClient();
ICommunicationObject commObj = (ICommunicationObject)client;
commObj.Closed += new EventHandler(Closed);
commObj.Faulted += new EventHandler(Faulted);
value = client.GetQuote(symbol);
Console.WriteLine( " {0}@{1} " , symbol, value);
Console.ReadLine();
}
static void Closed( object sender, EventArgs e)
{
// Handle Closed Event
}
static void Faulted( object sender, EventArgs e)
{
// Handle Faulted Event
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网