大佬教程收集整理的这篇文章主要介绍了如何从 Azure 函数 (HTTP) 返回数据流,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要从 Azure 函数进行一系列数据库调用,并在结果(文本块)可用时将其返回给 http 调用方。这可能会在一分钟左右的过程中偶尔出现。
我不想要“文件下载”响应只是通过响应流发送的数据。
有没有办法在 Azure 函数中写入响应正文流?
编辑:
在写入响应正文流 Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.
[FunctionName("Function1")]
public async Task<IActionResult> Run(
[httptrigger(AuthorizationLevel.Function,"get","post",Route = null)] httprequest req,ILogger log)
{
return new OkResult();
}
您可以使用 httprequest req
参数访问响应对象:
var response = req.httpContext.Response;
我们可以删除返回类型并简单地返回Task
,然后这样流式传输数据:
public class Function1
{
private async IAsyncEnumerable<String> GetDataAsync()
{
for (var i = 0; i < 100; ++i)
{
yield return "{\"Hello\":\"world\"}";
}
}
[FunctionName("Function1")]
public async Task Run(
[httptrigger(AuthorizationLevel.Function,ILogger log)
{
var response = req.httpContext.Response;
response.StatusCode = 200;
response.ContentType = "application/json-data-stream";
await using var sw = new StreamWriter(response.body);
await foreach (var msg in GetDataAsync())
{
await sw.WriteLineAsync(msg);
}
await sw.FlushAsync();
}
}
以上是大佬教程为你收集整理的如何从 Azure 函数 (HTTP) 返回数据流全部内容,希望文章能够帮你解决如何从 Azure 函数 (HTTP) 返回数据流所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。