HubConnection.Closed 事件

定义

连接关闭时发生。 由于错误或服务器或客户端有意关闭连接时没有出现错误,连接可能会关闭。

public:
 event Func<Exception ^, System::Threading::Tasks::Task ^> ^ Closed;
public event Func<Exception,System.Threading.Tasks.Task> Closed;
public event Func<Exception?,System.Threading.Tasks.Task>? Closed;
member this.Closed : Func<Exception, System.Threading.Tasks.Task> 
Public Event Closed As Func(Of Exception, Task) 

事件类型

Func<Exception,Task>

示例

下面的示例将处理程序附加到 Closed 事件,并检查提供的参数以确定是否有错误:

connection.Closed += (exception) =>
{
    if (exception == null)
    {
        Console.WriteLine("Connection closed without error.");
    }
    else
    {
        Console.WriteLine($"Connection closed due to an error: {exception}");
    }
};

注解

如果此事件是由于连接错误而触发的,则所 Exception 发生的将作为此处理程序的唯一参数传入。 如果客户端或服务器有意触发了此事件,则参数将为 null

适用于