0

I am hosting my WCF service on a windows Azure website. In my program I select between 1-16 users and for each user selected I contact my wcf service and return the data using the format below:

try
{
    ...
    client.Close();
}
catch (CommunicationException e)
{
    ...
    client.Abort();
}
catch (TimeoutException e)
{
    ...
    client.Abort();
}
catch (Exception e)
{
    ...
    client.Abort();
    throw;
}

The WCF service is contacting an azure SQL database to get the data which means the service call may last up to 10 seconds in total. But if i close the program while retreiving data and open it again and try to retrieve data the connection times out and I get a HTTP server error on my azure website which is then subsequently down for 2-3 minutes.

How can I close the WCF service connections on closing my program. should I be using a "Using" statment instead? And also should I try refactor my code to only contact the service once rather than once for each user selected?

JKennedy
  • 18,150
  • 17
  • 114
  • 198
  • See http://stackoverflow.com/questions/20749484/how-to-properly-close-a-client-proxy-an-existing-connection-was-forcibly-closed – ken2k Jul 03 '14 at 14:35

2 Answers2

0

It will be closed automatically. But if you want to be sure, you can use finally block:

try
{
    ...
    client.Close();
}
finally
{
    if(client.State != CommunicationState.Closed)
        client.Abort();
}
Uriil
  • 11,948
  • 11
  • 47
  • 68
  • Is the connection also closed if the connection is being made in a plugin and the plugin is closed? I'm guessing this depends on how the plugin is set up – JKennedy Jul 03 '14 at 14:43
  • @user1 In this case it will be aborted. But in most cases it's enougth. – Uriil Jul 03 '14 at 14:45
0

When you said you close the program, did you close the program through terminating the process in Task Manager? If so, because the program is closed disgracefully, there may not be Tcp/IP conversation for closing the connection.

If you close the program gracefully so client.dispose() will be called, the server will be acknowledged of the closure of the connection, rather than waiting for tcp/ip packet arriving and keeping the connection open.

You may be using try-finally block as Uriil suggested.

or

    try
{

using (...)
{
...
}

}
catch (some exceptions...
ZZZ
  • 2,752
  • 2
  • 25
  • 37
  • So I am closing a plugin which is being hosted within a program. I find that I close the plugin page (the page is not being displayed) but the code still runs meaning I probably havent disposed of the plugin correctly. Meaning the connections are left open – JKennedy Jul 04 '14 at 07:45
  • You did not disclose how you manage the life cycle of plugin, and in your comment you had already suspected that your plugin is not disposed properly so the client proxy is not disposed. To find out, you may just add a debug trace message in the Dispose method. After all, generally it is good to dispose the client proxy right after client calls,rather than associate the disposal with the disposal of the plugin. – ZZZ Jul 04 '14 at 12:04