Detect that client has closed connection

2.5k views Asked by At

I have several different types if client connected to my web api. Some of these have strictly internal policies regarding timeouts. For example a client I have well drop a connection if my api does not answer in less than 3 seconds.

In some cases my api need to grab information from a third party source and it can take longer than 5 seconds.

I am implementing a timeout reversal scenario so a client can call if he need to drop the connection for timeouts.

Is there any way to catch a connection dropped event in asp.net web api?

1

There are 1 answers

0
caliche2000 On

I've tried a stab at this because I was having the same issue and the following worked for me:

if (!HttpContext.Current.Response.IsClientConnected)
{
    return;
}

Again this worked in my case but I hope it helps you.

Here's Microsoft documentation: HttpResponse.IsClientConnected Property