Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

c# - Telegram Bot stops with An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll

I have written a simple Windows App using C# for managing Telegram bots using Telegram Bots API Previously it was working fine. but recently it does not work and will close automatically. When I run it in Debugging mode it says: An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll and refer to this line: Telegram.Bot.Types.Update[] update = bot.GetUpdatesAsync(offset).Result;

The whole code of my app is here:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace bot_test1
{
    public partial class Form1 : Form
    {
        private static string Token = "";
        private Thread botThread;
        Telegram.Bot.TelegramBotClient bot;
        public Form1()
        {
            InitializeComponent();
        }

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

        private void btnStart_Click(object sender, EventArgs e)
        {
            Token = txtToken.Text;
            botThread = new Thread(new ThreadStart(runBot));
            botThread.Start();
        }

        void runBot()
        {
            bot = new Telegram.Bot.TelegramBotClient(Token);
            this.Invoke(new Action(() =>
            {
                lblStatus.Text = "Online";
                lblStatus.ForeColor = Color.Green;
            }));
            int offset = 0;

            while (true)
            {
                Telegram.Bot.Types.Update[] update = bot.GetUpdatesAsync(offset).Result;
                foreach (var up in update)
                {
                    offset = up.Id + 1;
                    if (up.Message == null)
                        continue;

                    var text = up.Message.Text;
                    var from = up.Message.From;
                    var chatId = up.Message.Chat.Id;

                    if (text.Contains("/start"))
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine(from.Username + " Welcome to this bot!");
                        bot.SendTextMessageAsync(chatId, sb.ToString());
                    }
                }
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            botThread.Abort();
        }
    }
}

I could not find what is the problem. I also searched the net but could not find a solution. Could you please guide me?

PS. There is more details of the error:

System.AggregateException was unhandled
  HResult=-2146233088
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
       at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
       at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
       at System.Threading.Tasks.Task`1.get_Result()
       at bot_test1.Form1.runBot() in C:UsersPerson1DocumentsVisual Studio 2015Projectsot_test1ot_test1Form1.cs:line 47
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       HResult=-2146233088
       Message=An error occurred while sending the request.
       Source=mscorlib
       StackTrace:
            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            at Telegram.Bot.TelegramBotClient.<MakeRequestAsync>d__54`1.MoveNext()
       InnerException: 
            HResult=-2146233079
            Message=The request was aborted: Could not create SSL/TLS secure channel.
            Source=System
            StackTrace:
                 at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
                 at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
            InnerException: 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After a while I've found a solution: these codes must be added to the program code to be able to work:

using System.Net;

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

But still I am not sure what happend that the code was worked before without any problem but now it needs these changes to be done to be able to work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...