I have an application that utilizes Google's Drive service using the C# SDK. I am using Service Account credentials, with impersonation to specify the user I am acting on behalf of.
The issue I'm having is that I need to create a new DriveService for each impersonated user, and each of those DriveService isntances creates 2 HttpConnection objects that are NOT cleaned up when I Dispose() the DriveService. I have repro'd this in a unit test. I create the DriveService in this method:
public DriveService GetDriveService(string email)
{
var serviceAccountEmail = "service account email";
var privateKey = "private key";
var serviceAcctCreds = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
User = email,
Scopes = "scopes" }),
}.FromPrivateKey(privateKey));
var driveService = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = serviceAcctCreds,
ApplicationName = "app name",
});
return driveService;
}
Then use it as so:
public async Task DoTheGDriveMemoryTest()
{
var userEmails = new List<string>
{
"email1@example.com", "email2@example.com"
};
var i = 1;
while (true)
{
foreach (var email in userEmails)
{
//recommend using Fiddler Autoresponders here
using (var service = GetDriveService(email))
{
var request = service.Changes.List("delta id");
try
{
var result = await request.ExecuteAsync();
}
catch (Exception e)
{
//do nothing
}
}
}
}
}
This results in an ever-increasing number of HttpConnection objects held in memory, that are not cleaned up with Garbage Collection.