Exemplo n.º 1
0
        private async Task IncrementCount()
        {
            cts = new CancellationTokenSource();

            var headers = new Metadata();

            headers.Add("Authorization", $"Bearer {Token}");

            var client = new Count.Counter.CounterClient(Channel);
            var call   = client.StartCounter(new CounterRequest()
            {
                Start = currentCount
            }, headers, cancellationToken: cts.Token);

            try
            {
                Error = string.Empty;
                await foreach (var message in call.ResponseStream.ReadAllAsync())
                {
                    currentCount = message.Count;
                    StateHasChanged();
                }
            }
            catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled)
            {
                // Ignore exception from cancellation
            }
            catch (Exception e)
            {
                Error = e.Message;
            }
        }
Exemplo n.º 2
0
        private async Task IncrementCount()
        {
            cts = new CancellationTokenSource();

            var tokenResult = await TokenProvider.RequestAccessToken(new AccessTokenRequestOptions
            {
                Scopes = new[] { "821eb724-edb8-4dba-b425-3f953250c0ae/API.Access" }
            });

            if (tokenResult.TryGetToken(out var token))
            {
                Model.Token = token.Value;
            }
            else
            {
                Model.Token = "!!";
                _navigation.NavigateTo(tokenResult.RedirectUrl);
            }

            var headers = new Metadata
            {
                { "Authorization", $"Bearer {Model.Token}" }
            };

            var client = new Count.Counter.CounterClient(Channel);
            var call   = client.StartCounter(new CounterRequest()
            {
                Start = currentCount
            }, headers, cancellationToken: cts.Token);

            try
            {
                Error = string.Empty;
                await foreach (var message in call.ResponseStream.ReadAllAsync())
                {
                    currentCount = message.Count;
                    StateHasChanged();
                }
            }
            catch (RpcException rpcException) when(rpcException.StatusCode == StatusCode.Cancelled)
            {
                // Ignore exception from cancellation
                Error = rpcException.Message;
            }
            catch (RpcException rpcException) when(rpcException.StatusCode == StatusCode.Unauthenticated)
            {
                Error = rpcException.Message;
            }
            catch (Exception exception)
            {
                Error = exception.Message;
            }
        }
        private async Task IncrementCount()
        {
            cts = new CancellationTokenSource();

            try
            {
                Model.Token = await GrpcBearerTokenProvider.GetTokenAsync();
            }
            catch (AccessTokenNotAvailableException a)
            {
                a.Redirect();
            }

            var headers = new Metadata
            {
                { "Authorization", $"Bearer {Model.Token}" }
            };

            var client = new Count.Counter.CounterClient(Channel);
            var call   = client.StartCounter(new CounterRequest {
                Start = currentCount
            }, headers, cancellationToken: cts.Token);

            try
            {
                Error = string.Empty;
                await foreach (var message in call.ResponseStream.ReadAllAsync())
                {
                    currentCount = message.Count;
                    StateHasChanged();
                }
            }
            catch (RpcException rpcException) when(rpcException.StatusCode == StatusCode.Cancelled)
            {
                // Ignore exception from cancellation
                Error = rpcException.Message;
            }
            catch (RpcException rpcException) when(rpcException.StatusCode == StatusCode.Unauthenticated)
            {
                Error = rpcException.Message;
            }
            catch (Exception exception)
            {
                Error = exception.Message;
            }
        }