This is a follow up question to this question:
I have created a WCF service and it is up and running. When I used it, I was closing the connection after each time I got a response in a finally.
Trying to use the client for a second time, I got the exception of the linked question - can not access a disposed object. I tried implementing the IDisposable pattern without closing the connection every time.
1) First of all - Did I implement the dispose pattern correctly?
the code (it's short, don't worry):
I got a singleton class which is responsible for the service communication in which it creates the client class:
public class WebService : IDisposable
{
// Flag: Has Dispose already been called?
bool disposed = false;
private MyWebServiceContractClient client;
private static readonly Lazy<WebService> webServiceHandler = new Lazy<WebService>(() => new WebService());
private WebService()
{
client = new MyWebServiceContractClient();
}
public static WebService Instance
{
get
{
return webServiceHandler.Value;
}
}
public double GetAnswer()
{
try
{
if (!(client.State == CommunicationState.Opened) && !(client.State == CommunicationState.Opening))
{
client.Open();
}
//do some work
return answer;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return -1;
}
finally
{
// here i would normally call client.Close(); but now im not
}
}
public void Dispose()
{
// Dispose of unmanaged resources.
Dispose(true);
// Suppress finalization.
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
client.Close();
}
disposed = true;
}
}
2) If I'm only closing the connection in the dispose method, doesn't it mean I'm always keeping the connection alive? Isn't it a waste of resources and even a security risk to just keep it open?
MyWebServiceContractClient? \$\endgroup\$