I'm in a quarrel with a colleague regarding the appropriate scope of the using statement. This is the method in question.
public Guid IsServerReachable()
{
try
{
WhoAmIResponse whoAmI;
using (OrganizationServiceProxy service = GetService())
whoAmI = service.Execute(new WhoAmIRequest()) as WhoAmIResponse;
return whoAmI.UserId;
}
catch { return Guid.Empty; }
}
One of us claims that the using statement should include the declaration of whyAmI, while the other argues that it's only the service instance that needs to be using-ified. I'm not telling which is my theory but one is clearly wrong. Which?
using (OrganizationServiceProxy service = GetService())
whoAmI = service.Execute(new WhoAmIRequest()) as WhoAmIResponse;
Will be equivalent to:
OrganizationServiceProxy service = GetService();
try
{
whoAmI = service.Execute(new WhoAmIRequest()) as WhoAmIResponse;
}
finally
{
if (myRes!= null)
// Call the object's Dispose method.
((IDisposable)service).Dispose();
}