简体   繁体   中英

Code stops when using HttpContent.ReadAsAsync

I am trying to read async from HttpContent but my code exists on this method. It used to work but I recently had to switch to a console app because I wanted to make a discord bot. Does anyone know why the code "stops" when I use this in a console app?

I've tried using an async Main as suggested but it does not work

I am using this code:

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;
            }
        }
    }
}

I call it from here:

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;
    }
}

And this:

 [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());
        
    }

And then here with a discord chat event:

    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);
        }
    }

And this event is assigned here:

    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);
    }

  

I've tried using an async Main as suggested but it does not work

If you can't use async Main , then block in the Main method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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