C# Long-Polling Api Call

Date: 2020-08-10

IsCancellationRequested => Request cancelled (eg when closing browser tab)
NoContent => No Status change
HttpStatusCode.OK => Status changed
SignalWaiter => Search for SignalWaiter in posts

[HttpGet]
[Route("api/freightregistrations/{freightRegistrationId}/watch/{appStatusId}")]
//[ApiCallRight("api.freightregistrations.getById")]
[AllowAnonymous]
public async Task<HttpResponseMessage> WatchById(Guid freightRegistrationId, int appStatusId)
{
	var cancellationToken = Request.GetOwinContext()?.Request?.CallCancelled ?? default(CancellationToken);
	var freightRegistration = await DomainPorts.FreightRegistrationStore.GetById(freightRegistrationId);
	if (freightRegistration == null)
		return Request.CreateResponse(HttpStatusCode.NotFound);
	var appStatus = freightRegistration.GetAppStatus();
	if ((int)appStatus != appStatusId)
	{
		return Request.CreateResponse(HttpStatusCode.OK, new VmStatusChange((int)appStatus, appStatus.ToString()));
	}

	try
	{
		var key = $"registration|{freightRegistrationId}";
		using (var signalWaiter = new SignalWaiter(key, TimeSpan.FromSeconds(60), cancellationToken))
		{
			await signalWaiter.Wait();
			if (cancellationToken.IsCancellationRequested)
				return Request.CreateResponse(HttpStatusCode.NoContent);
			freightRegistration = await DomainPorts.FreightRegistrationStore.GetById(freightRegistrationId);
			if (freightRegistration == null)
				return Request.CreateResponse(HttpStatusCode.NotFound);
			appStatus = freightRegistration.GetAppStatus();
			if ((int)appStatus != appStatusId)
			{
				return Request.CreateResponse(HttpStatusCode.OK, new VmStatusChange((int)appStatus, appStatus.ToString()));
			}
		}
	}
	catch (TimeoutException)
	{
		// ignore
	}
	return Request.CreateResponse(HttpStatusCode.NoContent);
}

// Somewhere: 
SignalWaiter.SendSignal($"registration|{freightRegistrationId}");
38760cookie-checkC# Long-Polling Api Call