首页 > 解决方案 > 如何调用异步任务服务中的方法?

问题描述

我有一个async Task<ActionResult>方法CompanyInfoController,我想在服务类 ( ProductService) 中调用这个方法。如何调用此方法并companyInfo在服务方法中使用结果列表 ( )?

    [HttpPost]
    [Route("GetCompanyInfo")]
    public async Task<ActionResult> GetCompanyInfo(List<int> companyId)
    {
        var section = this._configuration.GetSection("Company");
        string uri = section.GetValue<string>("Host");
        string endpoint = section.GetValue<string>("CompanyInfo");

        using (var httpClient = new HttpClient())
        {
            string idContent = JsonConvert.SerializeObject(companyId);

            httpClient.BaseAddress = new Uri(uri);
            HttpResponseMessage responseMessage = await httpClient.PostAsync(endpoint, new StringContent(idContent, Encoding.UTF8, "application/json"));

            if (responseMessage.IsSuccessStatusCode)
            {
                var content = await responseMessage.Content.ReadAsStringAsync();
                var operationResult = JsonConvert.DeserializeObject<CompanyInfoResultJSON>(content);

                var companyInfo = operationResult.CompanyInfo.SelectMany(x => x.CompanyInfoList).ToList();

                return Ok(companyInfo);
            }
            else
            {
                return BadRequest();
            }
        }
    }

我的ProductService

public class ProductService : IProductService
{
    private readonly DBContext _dbContext;
    public ProductService(DBContext dbContext)
    {
        this._ dbContext = dbContext;
    }
    public IEnumerable<Products> GetCompanyProductList(List<int> companyId)
    {
        DateTime today = DateTime.Today;
        var products =_ dbContext.Products.Where(x => x.ProductionDate < today).ToList();
        foreach (item in companyId)
        {
            if (products.Any(x => x.companyId == item.companyId)
            {
                foreach (product in products)
                {
                    products.CompanyName =??
                  // In here, I want to get the companyInfoList elements.
                 }
            }
        }
      return products;
    }
}

标签: c#asp.net-mvcentity-frameworkasp.net-coreentity-framework-core

解决方案


您必须创建返回 API 结果的特殊方法并将其放在 Common 类中,例如:

    public static async Task<List<CompanyInfo>> GetCompanyInfo(List<int> companyId)
    {
       if (responseMessage.IsSuccessStatusCode)
            {
                

                companyInfo;
            }
            else
            {
                return null;
            }
      
    }

在此之后你可以从你的控制器调用它

     HttpPost]
    [Route("GetCompanyInfo")]
    public async Task<ActionResult> GetCompanyInfo(List<int> companyId)
    {
         var companyInfo=Common.GetCompanyInfo(companyId);
         if (responseMessage.IsSuccessStatusCode)
            {
                  return Ok(companyInfo);
            }
            else
            {
                return BadRequest("Not found");
            }
    }

和你可以从你的服务中调用它一样的方式

还有另一种可能更适合您的方法。在这种情况下,您不需要 Common

public class ProductService:IProductService
{
public  async Task<List<CompanyInfo>> GetCompanyInfo(List<int> companyId)
   .... the same code as it in  Common above
}

和你的控制器

public class CompanyController : Controller
{
          private readonly IProductService _service
        public CompanyController(IProductService service)
        {
            _service=service;
         } 
       HttpPost]
    [Route("GetCompanyInfo")]
    public async Task<ActionResult> GetCompanyInfo(List<int> companyId)
    {
          .... _service.GetCompanyInfo
     }
}
}

推荐阅读