0

我有2个项目。其中之一是 aspnet 核心 webapi,第二个是使用 api 的控制台应用程序。

Api 方法如下所示:

        [HttpPost]
        public async Task<IActionResult> CreateBillingInfo(BillingSummary 
        billingSummaryCreateDto)
        {
            var role = User.FindFirst(ClaimTypes.Role).Value;
            if (role != "admin")
            {
                return BadRequest("Available only for admin");
            }        

            ... other properties
            billingSummaryCreateDto.Price = icu * roc.Price;
            billingSummaryCreateDto.Project =
                await _context.Projects.FirstOrDefaultAsync(x => x.Id == 
            billingSummaryCreateDto.ProjectId);

            await _context.BillingSummaries.AddAsync(billingSummaryCreateDto);
            await _context.SaveChangesAsync();

            return StatusCode(201);
        }

使用 api 的控制台应用程序:

    public static async Task CreateBillingSummary(int projectId)
    {
        var json = JsonConvert.SerializeObject(new {projectId});
        var data = new StringContent(json, Encoding.UTF8, "application/json");

        using var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue("Bearer", await Token.GetToken());

        var loginResponse = await client.PostAsync(LibvirtUrls.createBillingSummaryUrl, 
        data);

        WriteLine("Response Status Code: " + (int) loginResponse.StatusCode);
        string result = loginResponse.Content.ReadAsStringAsync().Result; 
        WriteLine(result);
    }

Program.cs 主要方法如下所示:

    static async Task Main(string[] args)
    {
        if (Environment.GetEnvironmentVariable("TAIKUN_USER") == null ||
            Environment.GetEnvironmentVariable("TAIKUN_PASSWORD") == null ||
            Environment.GetEnvironmentVariable("TAIKUN_URL") == null)
        {
            Console.WriteLine("Please specify all credentials");
            Environment.Exit(0);
        }

        Timer timer = new Timer(1000); // show time every second
        timer.Elapsed += Timer_Elapsed;
        timer.Start();
        while (true)
        {
            Thread.Sleep(1000); // after 1 second begin
            await PollerRequests.CreateBillingSummary(60); // auto id
            await PollerRequests.CreateBillingSummary(59); // auto id
            Thread.Sleep(3600000); // 1hour wait again requests
        }

    }

是否可以找到所有 id 并自动粘贴而不是 59 和 60?来自项目表的 ID。_context.Projects

还尝试使用返回 id 的方法

    public static async Task<IEnumerable<int>> GetProjectIds2()
    {
        var json = await 
       Helpers.Transformer(LibvirtUrls.projectsUrl);
        List<ProjectListDto> vmList = 
        JsonConvert.DeserializeObject<List<ProjectListDto>>(json);

        return vmList.Select(x => x.Id).AsEnumerable(); // tried 
        ToList() as well
    }

并在使用的主要方法中:

foreach (var i in await PollerRequests.GetProjectIds2())
                     new List<int> { i }
                         .ForEach(async c => await 
             PollerRequests.CreateBillingSummary(c));

对于前 3 个 id,它工作但没有得到其他的,用控制台 writeline 方法测试返回所有 id

4

1 回答 1

1

首先获取所有ID:

var ids = await PollerRequests.GetProjectIds2();

然后创建任务列表并运行所有任务:

var taskList = new List<Task>();
foreach(var id in ids)
    taskList.Add(PollerRequests.CreateBillingSummary(id));

await Task.WhenAll(taskList);
于 2020-04-08T12:28:59.427 回答