using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using
Microsoft.Azure.WebJobs.Extensions.Http; using
Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using
Newtonsoft.Json; using System.Collections.Generic;
namespace FunctionApp10 {
public class Function1
{
private async IAsyncEnumerable<string> GetDataAsync()
{
for (var i = 0; i < 100; ++i)
{
yield return "{\"hello\":\"world\"}";
}
}
[FunctionName("Function1")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
var response = req.HttpContext.Response;
await using var sw = new StreamWriter(response.Body);
await foreach (var msg in GetDataAsync())
{
await sw.WriteLineAsync(msg);
}
await sw.FlushAsync();
response.StatusCode = 200;
response.ContentType = "application/json-data-stream";
return new EmptyResult();
}
}
}
1条答案
按热度按时间jqjz2hbq1#
为了将HTTP触发器中的数据作为流进行流式传输:
根据这个Github文档,***你可以编辑你的函数。HTTP触发器的json文件,数据类型为二进制或流,以流式传输数据***如下:-
function.json代码:-
另外,根据Andy的SO Thread回答,通过引用Andy的代码,我在运行HTTP触发器后在浏览器中得到了以下输出:-Here response。ContentType设置为“application/json-data-stream”;
***Andy's Code:-*函数。cs:-
输出:-触发器已执行:-
在浏览器中得到如下流式响应:-