private async Task HandleResultPoll(HttpContext context, ServiceId serviceId, MethodId methodId, string intentId) { var rfc7240Preferences = context.Request.Headers.GetRFC7240Preferences(); ITaskResult taskResult = null; var waitTime = rfc7240Preferences.Wait; if (waitTime > MaxLongPollTime) { waitTime = MaxLongPollTime; } if (waitTime <= TimeSpan.Zero) { taskResult = await _routineCompletionNotifier.TryPollCompletionAsync(serviceId, methodId, intentId, default); } else { taskResult = await TryWaitForResultAsync(serviceId, methodId, intentId, waitTime); } if (taskResult == null) { context.Response.Headers.Add("Date", DateTimeOffset.Now.ToString("u")); context.Response.StatusCode = DasyncHttpCodes.Running; } else { var compress = context.Request.Headers.ContainsValue("Accept-Encoding", "gzip"); var useEnvelope = context.Request.Headers.TryGetValue(DasyncHttpHeaders.Envelope, out _); var serializer = GetSerializer(context.Request.GetContentType(), isQueryRequest: true); await RespondWithMethodResult(context.Response, taskResult, serializer, useEnvelope, compress); } }
private async Task HandleResultPoll(HttpContext context, string serviceName, string methodName, string intentId) { var rfc7240Preferences = GetPreferences(context.Request.Headers); var serviceId = new ServiceId { ServiceName = serviceName }; var methodId = new RoutineMethodId { MethodName = methodName }; TaskResult taskResult = null; var waitTime = rfc7240Preferences.Wait; if (waitTime > MaxLongPollTime) { waitTime = MaxLongPollTime; } if (waitTime <= TimeSpan.Zero) { taskResult = await _routineCompletionNotifier.TryPollCompletionAsync(serviceId, methodId, intentId, default); } else { var cts = new CancellationTokenSource(); try { var completionSink = new TaskCompletionSource <TaskResult>(); _routineCompletionNotifier.NotifyCompletion(serviceId, methodId, intentId, completionSink, cts.Token); taskResult = await completionSink.Task.WithTimeout(waitTime); } catch (TaskCanceledException) { cts.Cancel(); } } if (taskResult == null) { context.Response.Headers.Add("Date", DateTimeOffset.Now.ToString("u")); context.Response.StatusCode = 304; // Not Modified } else { var contentType = context.Request.GetContentType(); var isDasyncJsonRequest = string.Equals(contentType.MediaType, "application/dasync+json", StringComparison.OrdinalIgnoreCase); await RespondWithRoutineResult(context, taskResult, isDasyncJsonRequest); } }