简体   繁体   English

如何在C#中正确处置第三方对象(TelegramBotClient)

[英]How to properly dispose third party object (TelegramBotClient) in C#

I am using TelegramBotClient component in my project. 我在我的项目中使用TelegramBotClient组件。 Unfortunately the component does not suggest any Dispose() method. 不幸的是,该组件没有建议任何Dispose()方法。 In my project I need to renew the object based on the token provided: 在我的项目中,我需要根据提供的令牌续订对象:

public TelegramBotClient NewClient(string token)
        {
            return new TelegramBotClient(token);
        }

I need to dispose the object created 我需要处理创建的对象

NewClient(token).SendTextMessageAsync(message.Chat.Id, message.Text, replyMarkup: keyboard, parseMode: Telegram.Bot.Types.Enums.ParseMode.Html);

How can I properly dispose the object? 如何正确放置物体?

If the third party-class implements IDisposable interface, then you can create object of that class with using block. 如果第三方类实现IDisposable接口,则可以using块创建该类的对象。 This way the object disposal will be taken care by the framework. 这样,框架将处理对象处置。

If the class does not implements IDisposable that means that the class does not use any unmanaged resources and the author of the class does not see any possibility of memory leak caused by this class object if it not disposed intentionally. 如果该类未实现IDisposable ,则意味着该类不使用任何非托管资源,并且该类的作者如果没有故意处置该类对象,则看不到任何由该类对象引起的内存泄漏的可能性。

If you still want to make sure that the third-party class object release the resources as soon at its usage is over, you can do that manually by wrapping it in your own class which implement IDisposable . 如果仍然要确保第三方类对象在使用结束后立即释放资源,则可以通过将其包装在实现IDisposable自己的类中来手动进行操作。

public class TextMessageClient : IDisposable
{
    bool disposed = false;
    private Telegram.Bot.TelegramBotClient client;

    public TextMessageClient()
    {
        //Write your own logic to get the token
        //Or accept the token as an argument of constructor.
        var token = Guid.NewGuid().ToString();
        client = new Telegram.Bot.TelegramBotClient(token);
    }

    public TextMessageClient(string token)
    {
        client = new Telegram.Bot.TelegramBotClient(token);
    }

    public async Task<Telegram.Bot.Types.Message> SendMessageAsync(string chatId, string message, string, IReplyMarkup keyboard, )
    {
        return await client.SendMessageAsync(chatId, message, replyMarkup: keyboard, parseMode: Telegram.Bot.Types.Enums.ParseMode.Html);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    // Protected implementation of Dispose pattern.
    protected virtual void Dispose(bool disposing)
    {
        if (disposed)
            return;

        if (disposing)
        {
            client = null;
        }

        disposed = true;
    }
}

Now you can use this class as following. 现在,您可以按以下方式使用此类。

using(var messageClient = new TextMessageClient())
{
    var message = await messageClient.SendMessageAsync(<somechatid>, <somemessage>, <somerelaymarkup>);
}

I hope this will help you resolve your issue. 希望这可以帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM