Enabling SSE in Linux Azure App Service
Hi,
I am trying to get Server Sent Events to work on my Linux Azure App Service but I cannot figure out how, I followed a lot of tutorials but they are all doing stuff on the programming side while I thing some server configuration needs to change. The following call is what I am using in my API. I added:
-
HttpContext IHttpResponseBodyFeature DisableBuffering() - Used the
IHttpResponseBodyFeature.Writer - Returning correct headers
The thing is that everything is and has been working without these modification with a local ASP.NET Environment and a VUE frontend. When looking at postman I can also see that the server is sending a ECONNRESET response after the first write.
I am using using a B1 Tier for my app service, so SSE should be allowed, I tried configuring web.config (I think Linux App Service is not using iis?) like this (added responseBufferLimit). Is there anything I am missing? I really don't need the overhead of SignalR so I need to do it like this.
[HttpGet("ask/{question}")]
public async Task<IActionResult> Post(string question)
{
var g = Response.HttpContext.Features.Get<Microsoft.AspNetCore.Http.Features.IHttpResponseBodyFeature>();
g.DisableBuffering();
Response.StatusCode = 200;
Response.Headers.Add("Content-Type", "text/event-stream");
Response.Headers.Add("Cache-Control", "no-cache");
Response.Headers.Add("Connection", "keep-alive");
var receivedBytes = Encoding.UTF8.GetBytes($"event: received\ndata: started processing\n\n");
await g.Writer.WriteAsync(receivedBytes);
await g.Writer.FlushAsync();
//await Response.Body.WriteAsync(receivedBytes, 0, receivedBytes.Length);
//await Response.Body.FlushAsync();
var graphClient = new SimpleGraphClient(_graphUserService.GetToken());
await foreach (var evDataTuple in _questionService.AskQuestion(question, graphClient, PlatformType.Web))
{
var eventData = $"event: {evDataTuple.ev}\ndata: {evDataTuple.data}\n\n";
var bytes = Encoding.UTF8.GetBytes(eventData);
//await Response.Body.WriteAsync(bytes, 0, bytes.Length);
//await Response.Body.FlushAsync();
await g.Writer.WriteAsync(bytes);
await g.Writer.FlushAsync();
}
return new EmptyResult();
}
web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" responseBufferLimit="0" />
</handlers>
<aspNetCore processPath="dotnet" arguments="" stdoutLogEnabled="false" stdoutLogFile="" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>