繁体   English   中英

使用 HttpContent.ReadAsAsync 时代码停止

[英]Code stops when using HttpContent.ReadAsAsync

我正在尝试从 HttpContent 读取异步,但我的代码存在于此方法中。 它曾经可以工作,但我最近不得不切换到控制台应用程序,因为我想制作一个 discord 机器人。 当我在控制台应用程序中使用它时,有谁知道为什么代码“停止”?

我已经尝试按照建议使用 async Main 但它不起作用

我正在使用这段代码:

public class BazaarInfo
{
    [JsonProperty("products")]
    public List<BazaarProduct> Products { get; set; }
    public static async Task<BazaarInfo> BuildAsync()
    {
        string url = "https://api.hypixel.net/skyblock/bazaar";

        using (HttpResponseMessage response = await ApiHelper.GetApiClient("application/json").GetAsync(url))
        {
            if (response.IsSuccessStatusCode)
            {
                BazaarInfo output = await response.Content.ReadAsAsync<BazaarInfo>(); //Stops

                return output;
            }
            else
            {
                return null;
            }
        }
    }
}

我从这里调用它:

public class Bazaar
{
    public Dictionary<string, BazaarProduct> Products { get; set; }

    public static async Task<Bazaar> BuildAsync()
    {
        var output = new Bazaar();

        var bazaarInfo = await BazaarInfo.BuildAsync();
        output.Products = bazaarInfo.Products.ToDictionary(product => product.Name);

        return output;
    }
}

和这个:

 [Command("bazaar")]
    public async Task BuildBzItem ([Remainder]string id)
    {
        var bazaar = await Bazaar.BuildAsync();

        string sellSummary = "";
        foreach (var summary in bazaar.Products[id].SellSummary)
            sellSummary += summary.Amount + summary.Price;

        var builder = new Discord.EmbedBuilder()
        {

            Description = sellSummary
        };

        await ReplyAsync("", false, builder.Build());
        
    }

然后在这里使用 discord 聊天事件:

    private async Task HandleCommandAsync(SocketMessage arg)
    {
        var message = arg as SocketUserMessage;
        var context = new SocketCommandContext(Client, message);

        if (message.Author.IsBot) return;

        int argPos = 0;
        if(message.HasStringPrefix("!", ref argPos))
        {
            var result = await Commands.ExecuteAsync(context, argPos, Services);
            if (!result.IsSuccess) Console.Write(result.ErrorReason);
        }
    }

这个事件是在这里分配的:

    public static async Task Main(string[] args) => await new Program().RunBotAsync();

    private DiscordSocketClient Client { get; set; }
    private CommandService Commands { get; set; }
    private IServiceProvider Services { get; set; }

    public async Task RunBotAsync()
    {
        Client = new DiscordSocketClient();
        Commands = new CommandService();
        Services = new ServiceCollection().AddSingleton(Client).AddSingleton(Commands).BuildServiceProvider();

        string token = "ODQ1MzE1OTY2OTcxODA1NzI3.YKfL1w.SPXi_0xXbbrMziZ9JWiqHFX4dto";

        Client.Log += ClientLog;

        await RegisterCommandsAsync();

        await Client.LoginAsync(TokenType.Bot, token);

        await Client.StartAsync();

        await Task.Delay(-1);
    }

    private Task ClientLog(LogMessage arg)
    {
        Console.WriteLine(arg);
        return Task.CompletedTask;
    }

    public async Task RegisterCommandsAsync()
    {
        Client.MessageReceived += HandleCommandAsync;
        await Commands.AddModulesAsync(Assembly.GetEntryAssembly(), Services);
    }

  

我已经尝试按照建议使用 async Main 但它不起作用

如果您不能使用async Main ,则在Main方法中阻塞:

public static void Main(string[] args) => new Program().RunBotAsync().GetAwaiter().GetResult();

暂无
暂无

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

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