c# - 如何调用异步任务服务中的方法?
问题描述
我有一个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;
}
}
解决方案
您必须创建返回 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
}
}
}
推荐阅读
- postgresql - HikariCP:为什么连接泄漏和释放导致新数据库连接的“连接尝试超时”?
- python - 带有静音片段的 Pyaudio
- java - 如何在 Android 中处理 Firebase 的异步特性
- math - 这个lua函数如何将位转换为32位
- javascript - 如何从 MySQL 多对多关系中获取 2 个字段?
- c++ - 我无法在 XCode 上成功编译 XIB 文件
- node.js - 使用 RSA 验证并签署从 NodeJS 到 Golang 的 JSON Web 令牌,反之亦然?
- node.js - 为 MongoDB 中的每个新对象创建多个集合是否错误?
- java - 使用与服务器时区不同的另一个时区来节省时间
- python - 网页抓取网站不返回正确的值