WCF技术内幕 第6章(3)

Close和Abort方法

CommunicationObject类型展示了可以销毁对象的方法。通常,Close和BeginClose方法可以以一种优雅的方式关闭CommunicationObject对象,而Abort方法则会立即关闭对象。

Close方法包含一个异步的实现,而Abort方法则没有。

Fault方法

虽然保护方法Fault也是一种关闭CommunicationObject对象的方式,但它不属于ICommunicationObject接口,它只适用于CommunicationObject对象的子类型。

调用Fault方法会把State属性转换为CommunicationState.Faulted,并且调用OnFaulted虚方法。大部分情况下,OnFaulted方法都会调用Abort方法。


6.3 通道形状介绍

消息交换模式与通道形状的关系

 

MEP Sender Receiver
数据报 IOutputChannel IInputChannel
请求/应答 IRequestChannel IReplyChannel
双工 IDuplexChannel IDuplexChannel
P2P IDuplexChannel IDuplexChannel

 

通道需要实现System.ServiceModel.Channels.ISessionChannel<T>接口来支持会话。ISessionChannel<T>的泛型参数必须实现System.ServiceModel.Channels.ISession接口。

在WCF中,实现ISessionChannel<T>接口的通道类型成为会话通道。

消息交换模式与会话通道形状的关系

 

MEP Sender Receiver
数据报 IOutputSessionChannel IInputSessionChannel
请求/应答 IRequestSessionChannel IReplySessionChannel
双工 IDuplexSessionChannel IDuplexSessionChannel
P2P IDuplexSessionChannel IDuplexSessionChannel

 

 

6.4 通道接口和基本类型

IChannel接口

 

    public interface IChannel : ICommunicationObject
    {
        T GetProperty<T>() where T : class;
    }


GetProperty<T>方法提供了在CommunicationObject堆栈里查询特定功能的途径。

 

 

            MessageVersion messageVersion = channel.GetProperty<MessageVersion>();
            if (messageVersion != null)
            {
            }


数据报通道 IInputChannel与IOutputChannel

 

 

    public interface IOutputChannel : IChannel, ICommunicationObject
    {
        EndpointAddress RemoteAddress { get; }
        Uri Via { get; }

        IAsyncResult BeginSend(Message message, AsyncCallback callback, object state);
        IAsyncResult BeginSend(Message message, TimeSpan timeout, AsyncCallback callback, object state);
        void EndSend(IAsyncResult result);
        void Send(Message message);
        void Send(Message message, TimeSpan timeout);
    }


对于接收者在数据报交换模式中的角色,IInoutChannel只定义了接收成员而没有定义发送成员。

    public interface IInputChannel : IChannel, ICommunicationObject
    {
        EndpointAddress LocalAddress { get; }

        IAsyncResult BeginReceive(AsyncCallback callback, object state);
        IAsyncResult BeginReceive(TimeSpan timeout, AsyncCallback callback, object state);
        IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback, object state);
        IAsyncResult BeginWaitForMessage(TimeSpan timeout, AsyncCallback callback, object state);
        Message EndReceive(IAsyncResult result);
        bool EndTryReceive(IAsyncResult result, out Message message);
        bool EndWaitForMessage(IAsyncResult result);
        Message Receive();
        Message Receive(TimeSpan timeout);
        bool TryReceive(TimeSpan timeout, out Message message);
        bool WaitForMessage(TimeSpan timeout);
    }


接收程序会消极地等待消息的到来。

 









 

posted @ 2013-03-29 14:06  xinyuyuanm  阅读(173)  评论(0编辑  收藏  举报