I have a WCF service that I am consuming, and have been doing well so far.
However on our production system with a lot of traffic, I am noticing that that after gradual consistent rise and falls in memory (time in between gradually elongates and the delta gradually increases), the memory consumption is trending higher.
I’m wondering if it could be due to the way I am consuming the DAL web service:
For example:
public static int GetUserTypeFromProfileID(int profileID)
{
try
{
memberServiceClient = new MemberServiceClient(); // connect to the data service
return memberServiceClient.GetUserTypeFromProfileID(profileID); // get the profileID associated with the sessionID
}
catch (Exception ex)
{
ErrorLogging.Instance.Fatal(ex);
return 0;
}
}
If I changed this to the following, using a using statement:
public static int GetProfileIDFromSessionID(string sessionID)
{
try
{
using (memberServiceClient = new MemberServiceClient()) // connect to the data service
{
return memberServiceClient.GetProfileIDFromSessionID(sessionID); // get the profileID associated with the sessionID
}
}
catch (Exception ex)
{
ErrorLogging.Instance.Fatal(ex);
return 0;
}
}
Is it good form to perform the return inside the using section?
I believe there is nothing specific to WCF with using statement. It will dispose your MemberServiceClient before returning the value.
However Dispose() method on a WCF service client calls Close() method inside, which can throw exceptions. So it’s better to call Close() method directly. You should also call Abort() method when exceptions occur. Here’s the recommended implementation.
Note: I have written a simple base class that handles these details. It’s on NuGet.
Update:
Here’s an example with WcfClientBase as requested:
You can also take a look at the source code on GitHub