通道用于发送和接收消息。通道负责传输工作和WS-×协议的实现,安全及事务处理。
6.1 正确认识通道
没有一个具体的,万能的通道类型。WCF类型系统包含许多通道类型的定义,并且每个通道类型的定义都有特定的目的。WCF类型系统也包含许多通道类型,它们是可靠性、事务处理和安全等高级特性的物理实现方式。
通道是由通道工厂创建的。
6.2 通道状态机
System.ServiceModel.ICommunicationObject接口
#region Assembly System.ServiceModel.dll, v4.0.30319
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.ServiceModel.dll
#endregion
using System;
namespace System.ServiceModel
{
// Summary:
// Defines the contract for the basic state machine for all communication-oriented
// objects in the system, including channels, the channel managers, factories,
// listeners, and dispatchers, and service hosts.
public interface ICommunicationObject
{
// Summary:
// Gets the current state of the communication-oriented object.
//
// Returns:
// The value of the System.ServiceModel.CommunicationState of the object.
CommunicationState State { get; }
// Summary:
// Occurs when the communication object completes its transition from the closing
// state into the closed state.
event EventHandler Closed;
//
// Summary:
// Occurs when the communication object first enters the closing state.
event EventHandler Closing;
//
// Summary:
// Occurs when the communication object first enters the faulted state.
event EventHandler Faulted;
//
// Summary:
// Occurs when the communication object completes its transition from the opening
// state into the opened state.
event EventHandler Opened;
//
// Summary:
// Occurs when the communication object first enters the opening state.
event EventHandler Opening;
// Summary:
// Causes a communication object to transition immediately from its current
// state into the closed state.
void Abort();
//
// Summary:
// Begins an asynchronous operation to close a communication object.
//
// Parameters:
// callback:
// The System.AsyncCallback delegate that receives notification of the completion
// of the asynchronous close operation.
//
// state:
// An object, specified by the application, that contains state information
// associated with the asynchronous close operation.
//
// Returns:
// The System.IAsyncResult that references the asynchronous close operation.
//
// Exceptions:
// System.ServiceModel.CommunicationObjectFaultedException:
// System.ServiceModel.ICommunicationObject.BeginClose() was called on an object
// in the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The default timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to close gracefully.
IAsyncResult BeginClose(AsyncCallback callback, object state);
//
// Summary:
// Begins an asynchronous operation to close a communication object with a specified
// timeout.
//
// Parameters:
// timeout:
// The System.Timespan that specifies how long the send operation has to complete
// before timing out.
//
// callback:
// The System.AsyncCallback delegate that receives notification of the completion
// of the asynchronous close operation.
//
// state:
// An object, specified by the application, that contains state information
// associated with the asynchronous close operation.
//
// Returns:
// The System.IAsyncResult that references the asynchronous close operation.
//
// Exceptions:
// System.ServiceModel.CommunicationObjectFaultedException:
// System.ServiceModel.ICommunicationObject.BeginClose() was called on an object
// in the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The specified timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to close gracefully.
IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state);
//
// Summary:
// Begins an asynchronous operation to open a communication object.
//
// Parameters:
// callback:
// The System.AsyncCallback delegate that receives notification of the completion
// of the asynchronous open operation.
//
// state:
// An object, specified by the application, that contains state information
// associated with the asynchronous open operation.
//
// Returns:
// The System.IAsyncResult that references the asynchronous open operation.
//
// Exceptions:
// System.ServiceModel.CommunicationException:
// The System.ServiceModel.ICommunicationObject was unable to be opened and
// has entered the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The default open timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to enter the System.ServiceModel.CommunicationState.Opened state
// and has entered the System.ServiceModel.CommunicationState.Faulted state.
IAsyncResult BeginOpen(AsyncCallback callback, object state);
//
// Summary:
// Begins an asynchronous operation to open a communication object within a
// specified interval of time.
//
// Parameters:
// timeout:
// The System.Timespan that specifies how long the send operation has to complete
// before timing out.
//
// callback:
// The System.AsyncCallback delegate that receives notification of the completion
// of the asynchronous open operation.
//
// state:
// An object, specified by the application, that contains state information
// associated with the asynchronous open operation.
//
// Returns:
// The System.IAsyncResult that references the asynchronous open operation.
//
// Exceptions:
// System.ServiceModel.CommunicationException:
// The System.ServiceModel.ICommunicationObject was unable to be opened and
// has entered the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The specified timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to enter the System.ServiceModel.CommunicationState.Opened state
// and has entered the System.ServiceModel.CommunicationState.Faulted state.
IAsyncResult BeginOpen(TimeSpan timeout, AsyncCallback callback, object state);
//
// Summary:
// Causes a communication object to transition from its current state into the
// closed state.
//
// Exceptions:
// System.ServiceModel.CommunicationObjectFaultedException:
// System.ServiceModel.ICommunicationObject.Close() was called on an object
// in the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The default close timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to close gracefully.
void Close();
//
// Summary:
// Causes a communication object to transition from its current state into the
// closed state.
//
// Parameters:
// timeout:
// The System.Timespan that specifies how long the send operation has to complete
// before timing out.
//
// Exceptions:
// System.ServiceModel.CommunicationObjectFaultedException:
// System.ServiceModel.ICommunicationObject.Close() was called on an object
// in the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The timeout elapsed before the System.ServiceModel.ICommunicationObject was
// able to close gracefully.
void Close(TimeSpan timeout);
//
// Summary:
// Completes an asynchronous operation to close a communication object.
//
// Parameters:
// result:
// The System.IAsyncResult that is returned by a call to the System.ServiceModel.ICommunicationObject.BeginClose()
// method.
//
// Exceptions:
// System.ServiceModel.CommunicationObjectFaultedException:
// System.ServiceModel.ICommunicationObject.BeginClose() was called on an object
// in the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The timeout elapsed before the System.ServiceModel.ICommunicationObject was
// able to close gracefully.
void EndClose(IAsyncResult result);
//
// Summary:
// Completes an asynchronous operation to open a communication object.
//
// Parameters:
// result:
// The System.IAsyncResult that is returned by a call to the System.ServiceModel.ICommunicationObject.BeginOpen()
// method.
//
// Exceptions:
// System.ServiceModel.CommunicationException:
// The System.ServiceModel.ICommunicationObject was unable to be opened and
// has entered the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The timeout elapsed before the System.ServiceModel.ICommunicationObject was
// able to enter the System.ServiceModel.CommunicationState.Opened state and
// has entered the System.ServiceModel.CommunicationState.Faulted state.
void EndOpen(IAsyncResult result);
//
// Summary:
// Causes a communication object to transition from the created state into the
// opened state.
//
// Exceptions:
// System.ServiceModel.CommunicationException:
// The System.ServiceModel.ICommunicationObject was unable to be opened and
// has entered the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The default open timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to enter the System.ServiceModel.CommunicationState.Opened state
// and has entered the System.ServiceModel.CommunicationState.Faulted state.
void Open();
//
// Summary:
// Causes a communication object to transition from the created state into the
// opened state within a specified interval of time.
//
// Parameters:
// timeout:
// The System.Timespan that specifies how long the send operation has to complete
// before timing out.
//
// Exceptions:
// System.ServiceModel.CommunicationException:
// The System.ServiceModel.ICommunicationObject was unable to be opened and
// has entered the System.ServiceModel.CommunicationState.Faulted state.
//
// System.TimeoutException:
// The specified timeout elapsed before the System.ServiceModel.ICommunicationObject
// was able to enter the System.ServiceModel.CommunicationState.Opened state
// and has entered the System.ServiceModel.CommunicationState.Faulted state.
void Open(TimeSpan timeout);
}
}
CommunicationState类型
#region Assembly System.ServiceModel.dll, v4.0.30319
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.ServiceModel.dll
#endregion
using System;
namespace System.ServiceModel
{
// Summary:
// Defines the states in which an System.ServiceModel.ICommunicationObject can
// exist.
public enum CommunicationState
{
// Summary:
// Indicates that the communication object has been instantiated and is configurable,
// but not yet open or ready for use.
Created = 0,
//
// Summary:
// Indicates that the communication object is being transitioned from the System.ServiceModel.CommunicationState.Created
// state to the System.ServiceModel.CommunicationState.Opened state.
Opening = 1,
//
// Summary:
// Indicates that the communication object is now open and ready to be used.
Opened = 2,
//
// Summary:
// Indicates that the communication object is transitioning to the System.ServiceModel.CommunicationState.Closed
// state.
Closing = 3,
//
// Summary:
// Indicates that the communication object has been closed and is no longer
// usable.
Closed = 4,
//
// Summary:
// Indicates that the communication object has encountered an error or fault
// from which it cannot recover and from which it is no longer usable.
Faulted = 5,
}
}
CommunicationObject类型是所有通道的基类型,其保证通道或通道工厂的一致状态转换。
不同状态的转换过程