如何在WCF中处理意外的错误连接

时间:2011-12-07 01:37:15

标签: wcf

我使用WCF双工与net.tcp绑定聊天项目。我已经在IIS 7.5中构建了

但是我遇到了与客户端实现的回调相关的问题。当客户端点击断开连接按钮时,它运行良好,但万一,客户端因为断开网络,计算机关闭等原因而崩溃,之后服务无法正常工作。看来服务也会出错。

我的聊天服务界面:

[ServiceContract(Name="ServiceChat", SessionMode=SessionMode.Required, CallbackContract=typeof(IChatCallback))]
public interface IChat
{
    [OperationContract]
    bool Connect(Client client);

    [OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = true)]
    void Disconnect();

    [OperationContract(IsInitiating = false)]
    void Refresh();

    [OperationContract(IsOneWay = true, IsInitiating = false)]
    void Whisper(string target, string content);

    [OperationContract(IsInitiating = false)]
    Room[] GetRooms();

    [OperationContract(IsInitiating = false)]
    bool CreateRoom(string roomName);

    [OperationContract(IsInitiating = false)]
    bool JoinRoom(string roomName);

    [OperationContract(IsOneWay = true, IsInitiating = false)]
    void LeaveRoom(string roomName);

    [OperationContract(IsOneWay = true, IsInitiating = false)]
    void SayToRoom(string roomName, string content);

    [OperationContract(IsOneWay = true, IsInitiating = false)]
    void InviteMember(Client client, string roomName);

    [OperationContract(IsOneWay = true, IsInitiating = false)]
    void KickMember(Client member, string roomName);

    [OperationContract(IsOneWay = true, IsInitiating = false)]
    void DisconnectRoom(string roomName);

    [OperationContract(IsOneWay = true, IsInitiating = false)]
    void DisconnectAllRoom();

    [OperationContract(IsOneWay = true, IsInitiating = false)]
    void SendFile(string target, File file);
}

我的服务回调:

public interface IChatCallback
{
    [OperationContract(IsOneWay=true)]
    void RefreshClient(List<Client> clients);

    [OperationContract(IsOneWay = true)]
    void ReceiveWhisper(Message message);

    [OperationContract(IsOneWay = true)]
    void ReceiveRoomMessage(string roomName, Message message);

    [OperationContract(IsOneWay = true)]
    void ReceiveNotifyClientConnect(Client joinedClient);

    [OperationContract(IsOneWay = true)]
    void ReceiveNotifyClientDisconnect(Client leaver);

    [OperationContract(IsOneWay = true)]
    void ReceiveNotifyClientJoinRoom(Client joinedClient, string roomName);

    [OperationContract(IsOneWay = true)]
    void ReceiveNotifyClientLeaveRoom(Client leaver, string roomName, bool kicked);

    [OperationContract(IsOneWay = true)]
    void ReceiveNotifyClientDisconnectRoom(Client owner, string roomName);

    [OperationContract(IsOneWay = true)]
    void ReceiveInvitedMessage(Room room);

    [OperationContract(IsOneWay = true)]
    void ReceivedFile(File file);
}

如何处理此意外错误?

1 个答案:

答案 0 :(得分:0)

查看双工服务上的this MSDN article。正如微软所警告的那样:

  

双工模型不会自动检测服务或客户端何时关闭其频道。 因此,如果服务意外终止,默认情况下不会通知服务,或者如果客户端意外终止,则不会通知服务。客户端和服务可以实现自己的协议,以便相互通知他们这样选择。

因此,每当您尝试将数据发送回客户端时,您都需要执行检查。永远不要假设连接仍然是纯粹的每次传输,因为它可能已经失败,如果没有服务知道它。