WCF, How to capture the client disconnection at the server side.

parabellum
parabellum used Ask the Experts™
on

Hello,

I  have written a file server/client using WCF (net.tcp) .  Here is my problem:

While a client is uploading a file to the server, when I pull the ethernet cable at the client side my client gets an exception.(Which is obvious since i have disconnected the client from server).

 Below is the Upload file function. The problem is i  am not getting any exception  at the server side at all. So i can not detect that there is a problem with the client side.

How can i understand that the file transfer is not complete ?

 


public string UploadToServer(Stream stream)
{
  FileStream fStream=File.Open(.....);

 try
{
  while(stream.Read(fileUploadBuffer,0,bufferLen)>0)
   fStream.Write(fileUploadBuffer,0,....)

  return "OK";
}
 catch (Exception ex)
{
  // Log operations here.
  return "ERROR";
}


}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Did you set a read-timeout? If so, you will have a difference between an EOF or a read-timeout. The latter meaning that the connection has been broken, ghe former meaning that you have reached the end of the file and that the stream can be closed.

Author

Commented:

Well the problem is my input(Stream stream) does not support Read timeout.(Its CanTimeout property is false). And my FileSTream fStream object does not support Timeout too.  So i can not get any timeout at the server side.
Commented:
There should be an exception occurring that nothing can be read from the stream.
Most likely, this would be an IOException which you can trap and handle as needed.
http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx 
Since you do not receive an exception (as you mentioned in your original post), what does happen in your application? Does it wait indefinately or ...?

public string UploadToServer(Stream stream)  {  
  FileStream fStream=File.Open(.....);  
  
  try  {  
     while(stream.Read(fileUploadBuffer,0,bufferLen)>0)  
        fStream.Write(fileUploadBuffer,0,....)  
  
     return "OK";  
  }  
  catch (IOException ioEx) {
    fStream.Close();
    // delete created file?
    return "ABORTED";
  }
  catch (Exception ex) {  
     // Log operations here.  
     return "ERROR";  
  }  
}

Open in new window

Author

Commented:

Currently the server does not throw any exceptions and other clients can upload to the server.(Server CPU usage does not go peak when a client is forced to disconnect.)

 I do not know the inner working details of WCF maybe it is suspending the operation internally. I am not sure about that.
Hello:
Be sure not been catching the exception somewhere else in your program...
I'm not sure, but if you can get the Length of the incoming stream, you could compare it to the local stream lengh to see if they are the same.
Best regards.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial