1

I have implemented a gRPC client in C#. Should I shutdown the channel in the destructor?

private readonly Channel channel;

~MyClient()
{
    this.channel.ShutdownAsync().Wait();
}
Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
user2809176
  • 1,042
  • 12
  • 29
  • 5
    No, you should only clean up unmanaged resources in a destructor. The correct place to do it is in `Dispose()`. – Matthew Watson Feb 12 '20 at 13:46
  • First, it is better to call it - `finalizer`. Second, it is better to use Dispose pattern. You don't use unmanaged resources here. – Anton Feb 12 '20 at 13:47
  • In general order, you should not use the object destructor as you would do it in C++ dispose wil be in 99% of the time your best friend. unless you are dealing with unmanaged resources :) – pix Feb 12 '20 at 13:47
  • Does this answer your question? [gRPC client do not dispose Channel](https://stackoverflow.com/questions/47594441/grpc-client-do-not-dispose-channel) – Ortiga Feb 12 '20 at 13:57
  • 1
    @MatthewWatson or in this case: `IAsyncDisposable.DisposeAsync()` – Marc Gravell Feb 12 '20 at 14:08
  • @Ortiga thanks for your suggestion, I find a code example a bit messy there. But in general you are right. – user2809176 Feb 12 '20 at 14:24
  • @MatthewWatson could you please make an answer with a code sample – user2809176 Feb 12 '20 at 15:17

0 Answers0