Enabling SSE in Linux Azure App Service

Jasper Baetslé 120 Reputation points
2023-11-23T07:59:34.3366667+00:00

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.

User's image

[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>
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.