/// <summary>
        /// Calls the server to get updated status of the long-running operation.
        /// </summary>
        /// <param name="async">When <c>true</c>, the method will be executed asynchronously; otherwise, it will execute synchronously.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the service call.</param>
        /// <returns>The HTTP response received from the server.</returns>
        private async ValueTask <Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
        {
            if (!_hasCompleted)
            {
                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(AnalyzeHealthcareEntitiesOperation)}.{nameof(UpdateStatus)}");
                scope.Start();

                try
                {
                    Response <HealthcareJobState> update = async
                        ? await _serviceClient.HealthStatusAsync(new Guid(Id), null, null, _showStats, cancellationToken).ConfigureAwait(false)
                        : _serviceClient.HealthStatus(new Guid(Id), null, null, _showStats, cancellationToken);

                    _response     = update.GetRawResponse();
                    _status       = update.Value.Status;
                    _createdOn    = update.Value.CreatedDateTime;
                    _expiresOn    = update.Value.ExpirationDateTime;
                    _lastModified = update.Value.LastUpdateDateTime;

                    if (_status == TextAnalyticsOperationStatus.Succeeded)
                    {
                        var nextLink = update.Value.NextLink;
                        var value    = Transforms.ConvertToAnalyzeHealthcareEntitiesResultCollection(update.Value.Results, _idToIndexMap);
                        _firstPage = Page.FromValues(new List <AnalyzeHealthcareEntitiesResultCollection>()
                        {
                            value
                        }, nextLink, _response);
                        _hasCompleted = true;
                    }
                    else if (_status == TextAnalyticsOperationStatus.Failed)
                    {
                        _requestFailedException = await ClientCommon.CreateExceptionForFailedOperationAsync(async, _diagnostics, _response, update.Value.Errors)
                                                  .ConfigureAwait(false);

                        _hasCompleted = true;
                        throw _requestFailedException;
                    }
                    else if (_status == TextAnalyticsOperationStatus.Cancelled)
                    {
                        _requestFailedException = new RequestFailedException("The operation was canceled so no value is available.");
                        _hasCompleted           = true;
                        throw _requestFailedException;
                    }
                }
                catch (Exception e)
                {
                    scope.Failed(e);
                    throw;
                }
            }

            return(GetRawResponse());
        }
Beispiel #2
0
        /// <summary>
        /// Calls the server to get updated status of the long-running operation.
        /// </summary>
        /// <param name="async">When <c>true</c>, the method will be executed asynchronously; otherwise, it will execute synchronously.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the service call.</param>
        /// <returns>The HTTP response received from the server.</returns>
        private async ValueTask <Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
        {
            if (!_hasCompleted)
            {
                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(AnalyzeHealthcareEntitiesOperation)}.{nameof(UpdateStatus)}");
                scope.Start();

                try
                {
                    Response <HealthcareJobState> update = async
                        ? await _serviceClient.HealthStatusAsync(new Guid(Id), _top, _skip, _showStats, cancellationToken).ConfigureAwait(false)
                        : _serviceClient.HealthStatus(new Guid(Id), _top, _skip, _showStats, cancellationToken);

                    _response = update.GetRawResponse();
                    _status   = update.Value.Status;

                    if (update.Value.Status == TextAnalyticsOperationStatus.Succeeded)
                    {
                        // we need to first assign a vaue and then mark the operation as completed to avoid race conditions
                        _value        = Transforms.ConvertToRecognizeHealthcareEntitiesResultCollection(update.Value.Results, _idToIndexMap);
                        NextLink      = update.Value.NextLink;
                        _hasCompleted = true;
                    }
                    else if (update.Value.Status == TextAnalyticsOperationStatus.Failed)
                    {
                        _requestFailedException = await ClientCommon.CreateExceptionForFailedOperationAsync(async, _diagnostics, _response, update.Value.Errors)
                                                  .ConfigureAwait(false);

                        _hasCompleted = true;
                        throw _requestFailedException;
                    }
                    else if (update.Value.Status == TextAnalyticsOperationStatus.Cancelled)
                    {
                        _requestFailedException = new RequestFailedException("The operation was canceled so no value is available.");
                        _hasCompleted           = true;
                        throw _requestFailedException;
                    }
                }
                catch (Exception e)
                {
                    scope.Failed(e);
                    throw;
                }
            }

            return(GetRawResponse());
        }
        async ValueTask <OperationState <AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> > > IOperation <AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> > .UpdateStateAsync(bool async, CancellationToken cancellationToken)
        {
            Response <HealthcareJobState> response = async
                ? await _serviceClient.HealthStatusAsync(new Guid(_jobId), null, null, _showStats, cancellationToken).ConfigureAwait(false)
                : _serviceClient.HealthStatus(new Guid(_jobId), null, null, _showStats, cancellationToken);

            // Add lock to avoid race condition?
            _status       = response.Value.Status;
            _createdOn    = response.Value.CreatedDateTime;
            _expiresOn    = response.Value.ExpirationDateTime;
            _lastModified = response.Value.LastUpdateDateTime;

            Response rawResponse = response.GetRawResponse();

            if (response.Value.Status == TextAnalyticsOperationStatus.Succeeded)
            {
                string nextLink = response.Value.NextLink;
                AnalyzeHealthcareEntitiesResultCollection value = Transforms.ConvertToAnalyzeHealthcareEntitiesResultCollection(response.Value.Results, _idToIndexMap);
                _firstPage = Page.FromValues(new List <AnalyzeHealthcareEntitiesResultCollection>()
                {
                    value
                }, nextLink, rawResponse);

                return(OperationState <AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> > .Success(rawResponse, CreateOperationValueAsync(CancellationToken.None)));
            }
            else if (response.Value.Status == TextAnalyticsOperationStatus.Failed)
            {
                RequestFailedException requestFailedException = await ClientCommon
                                                                .CreateExceptionForFailedOperationAsync(async, _diagnostics, rawResponse, response.Value.Errors)
                                                                .ConfigureAwait(false);

                return(OperationState <AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> > .Failure(rawResponse, requestFailedException));
            }
            else if (response.Value.Status == TextAnalyticsOperationStatus.Cancelled)
            {
                return(OperationState <AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> > .Failure(rawResponse,
                                                                                                            new RequestFailedException("The operation was canceled so no value is available.")));
            }

            return(OperationState <AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> > .Pending(rawResponse));
        }