✅ Will I fall into a gotcha with this constructor?
Hi, all:
I'm trying to setup a class for managing my AWS S3 interactions, but I'm wondering if there are any issues with my definition:
Looks simple enough, but I'm worried about two things, TransferUtility inherits from
IDisposable
, will the framework properly handle the disposability of the _transferUtility
? Also, would it be more appropriate to create a new TransferUtility
object under the scope of each function that I want to use the class rather than creating a class field? For example:
I'll be expecting to upload many (~50 25MB) files at once. If the latter approach is more appropriate I can of course pass in a list of objects I'd like to upload.
Thanks 🙂TransferUtility Class | AWS SDK for .NET V3
TransferUtility Class
20 Replies
Since
S3Service
is new
ing up the TransferUtility
, it owns its lifetime. Therefore, you should either implement IDisposable
on S3Service
, or move ownership by injecting TransferUtility
instead of IAmazonS3
.Sorry @C@NYON⭐ONL!NE your message contained blocked content and has been removed!
Hmmm
let me try messaging in parts
Sorry @C@NYON⭐ONL!NE your message contained blocked content and has been removed!
IDK why my message is blocked 😭
Sorry @C@NYON⭐ONL!NE your message contained blocked content and has been removed!
Whatever
$paste
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Sorry @C@NYON⭐ONL!NE your message contained blocked content and has been removed!
What're you trying to paste?
🤷

What I mean by "injecting TransferUtility instead of IAmazonS3" is that you replace your constructor parameter from
IAmazonS3
to TransferUtility
and leave the disposal to whatever is creating your S3Service
you can add it as
services.AddScoped(sp => new TransferUtility(sp.GetRequiredService<IAmazonS3>()))
if you resolve your service from a di container then that would be responsible for disposal.
Other than that, your
Dispose
implementation is fine.okay with sehra's line of code i see what you mean. i use functions available in
IAmazonS3
but not TransferUtility
in some of my implementation, particularly for listing buckets and keys in them, so i'll have to go with the Dispose
route i thinkyep, that makes sense. Still, you could inject both
IAmazonS3
and TransferUtility
and not deal with disposal in your service.that is tempting particularly because VS is warning me about
CA1816: Call GC.SuppressFinalize correctly
which is approaching ends of the language i don't fully comprehendforget about that
public void Dispose() => foo.Dispose();
is fine if your disposable dependency (_transferUtility
) is managed (i.e. implements IDisposable
itself).
It does, so you don't have to worry about the finalization stuff.Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
If you have no further questions, please use /close to mark the forum thread as answered