C# Telegram Bot Application Crashes with System.AggregateException in mscorlib.dll

I created a Windows Forms application in C# that handles Telegram bot operations through the Telegram Bot API. The app worked perfectly before but now it crashes unexpectedly. When I debug it, I get this error: System.AggregateException occurred in mscorlib.dll pointing to this specific line: var updates = client.GetUpdatesAsync(lastUpdateId).Result;

Here’s my complete application code:

using System;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;

namespace TelegramBotManager
{
    public partial class MainForm : Form
    {
        private static string apiToken = "";
        private Thread workerThread;
        Telegram.Bot.TelegramBotClient client;
        
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
        }

        private void btnActivate_Click(object sender, EventArgs e)
        {
            apiToken = txtApiToken.Text;
            workerThread = new Thread(new ThreadStart(startBotService));
            workerThread.Start();
        }

        void startBotService()
        {
            client = new Telegram.Bot.TelegramBotClient(apiToken);
            this.Invoke(new Action(() =>
            {
                lblConnectionStatus.Text = "Connected";
                lblConnectionStatus.ForeColor = Color.Green;
            }));
            int lastUpdateId = 0;

            while (true)
            {
                var updates = client.GetUpdatesAsync(lastUpdateId).Result;
                foreach (var update in updates)
                {
                    lastUpdateId = update.Id + 1;
                    if (update.Message == null)
                        continue;

                    var messageText = update.Message.Text;
                    var user = update.Message.From;
                    var chatIdentifier = update.Message.Chat.Id;

                    if (messageText.Contains("/start"))
                    {
                        StringBuilder response = new StringBuilder();
                        response.AppendLine(user.Username + " Hello and welcome!");
                        client.SendTextMessageAsync(chatIdentifier, response.ToString());
                    }
                }
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            workerThread.Abort();
        }
    }
}

I can’t figure out what’s causing this issue. I’ve searched online but haven’t found a working solution. Any help would be appreciated?

The detailed error information shows:

System.AggregateException was unhandled
HResult=-2146233088
Message=One or more errors occurred.
Source=mscorlib
InnerException: The request was aborted: Could not create SSL/TLS secure channel.

sounds like an ssl/tls problem. had that issue before when telegram changed their ssl stuff. try adding ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; before creating the bot client. also, avoid using .Result, it’s blocking your thread. use async/await instead!

That SSL/TLS error means your app can’t connect securely to Telegram’s servers. Usually happens when your system’s stuck on old TLS versions that Telegram doesn’t support anymore. You’ll want to update the security protocol, but also ditch that infinite while loop for a proper CancellationToken setup. Thread.Abort() is deprecated and causes resource leaks. I’ve fixed similar issues by switching to async patterns with cancellation instead of blocking with .Result. Also check for pending Windows updates - they often include newer TLS libraries.

This AggregateException with SSL/TLS errors happens when your app can’t connect securely to Telegram’s API. Telegram’s been stricter about security lately, so it’s more common now. Besides fixing the TLS version, wrap your GetUpdatesAsync in try-catch to handle network drops. I’ve seen this exact crash when internet cuts out briefly or antivirus blocks the connection. Add exponential backoff for retries and proper exception handling around API calls. Also, ditch Thread.Abort() - it’ll terminate threads while they’re holding resources, causing memory leaks and weird behavior.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.