Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用TLSharp的电报认证_C#_Telegram - Fatal编程技术网

C# 使用TLSharp的电报认证

C# 使用TLSharp的电报认证,c#,telegram,C#,Telegram,我尝试开发一个用于电报的客户端,它只接收消息并对其内容运行一些简单的逻辑 我的代码目前看起来像这样 using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using TLSharp.Core; namespace TelegramBot { public se

我尝试开发一个用于电报的客户端,它只接收消息并对其内容运行一些简单的逻辑

我的代码目前看起来像这样

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TLSharp.Core;

namespace TelegramBot
{
    public sealed class Service
    {
        private TelegramClient client;

        public Service()
        {
            this.client = new TelegramClient(etc.Constants.AppApiId, etc.Constants.AppApiHash);
        }

        public async void Connect()
        {
            await this.client.ConnectAsync();
        }

        public async void Authenticate(String phoneNumber)
        {
            var hash = await client.SendCodeRequestAsync(phoneNumber);

            {
                Debugger.Break();
            }

            var code = "<code_from_telegram>"; // you can change code in debugger

            var user = await client.MakeAuthAsync(phoneNumber, hash, code);
        }
    }
}
static void Main(string[] args)
{
    Service bot = new Service();

    bot.Connect();
    bot.Authenticate(etc.Constants.PhoneNumber);

    Debugger.Break();
}
但是,当调用“SendCodeRequestAsync”时,我得到了一个“NullPointerException”。我如何修复/处理此问题?数字以“+12223334444”的格式提供


问题在于不能等待
异步void
方法。它们抛出的任何异常也无法捕获。它们仅用于事件处理程序或类似事件处理程序的方法

void
方法的等价物是
async Task
,而不是
async void

在这种情况下,应将方法更改为:

    public async Task Connect()
    {
        await this.client.ConnectAsync();
    }

    public async Task Authenticate(String phoneNumber)
    {
    //...
    }
Main()
应更改为:

static void Main(string[] args)
{
    Service bot = new Service();

    bot.Connect().Wait();
    bot.Authenticate(etc.Constants.PhoneNumber).Wait();

    Debugger.Break();
}
或者,更好的是:

static void Main(string[] args)
{
    Service bot = new Service();

    Authenticate(bot).Wait();

    Debugger.Break();
}

static async Task Authenticate(Service bot)
{
    await bot.Connect();
    await bot.Authenticate(etc.Constants.PhoneNumber);
}

问题是不能等待
异步void
方法。它们抛出的任何异常也无法捕获。它们仅用于事件处理程序或类似事件处理程序的方法

void
方法的等价物是
async Task
,而不是
async void

在这种情况下,应将方法更改为:

    public async Task Connect()
    {
        await this.client.ConnectAsync();
    }

    public async Task Authenticate(String phoneNumber)
    {
    //...
    }
Main()
应更改为:

static void Main(string[] args)
{
    Service bot = new Service();

    bot.Connect().Wait();
    bot.Authenticate(etc.Constants.PhoneNumber).Wait();

    Debugger.Break();
}
或者,更好的是:

static void Main(string[] args)
{
    Service bot = new Service();

    Authenticate(bot).Wait();

    Debugger.Break();
}

static async Task Authenticate(Service bot)
{
    await bot.Connect();
    await bot.Authenticate(etc.Constants.PhoneNumber);
}

为什么
异步无效
?为什么
异步无效