private bool CheckCallIsInTransaction()
        {
            try
            {
                if (stream == null)
                {
                    return false;
                }

                if (stream.GetStatus().StatusCode == StatusCode.OK)
                {
                    return false;
                }
                return false;
            }
            catch (Exception ex)
            {
                if (ex.Message == "Status can only be accessed once the call has finished.")
                {
                    return true;
                }
                else
                    throw ex;
               
            }
        }
Пример #2
0
        /// <summary>
        /// Obtain a token per user (identity) for interacting with the remote API.
        /// </summary>
        /// <param name="publicKey"></param>
        /// <param name="VerifySignature"></param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        /// <returns></returns>
        public async Task <string> GetTokenChallenge(string publicKey, Func <byte[], Task <byte[]> > VerifySignature, CancellationToken cancellationToken = default)
        {
            string token = string.Empty;

            using AsyncDuplexStreamingCall <GetTokenRequest, GetTokenReply> call = _apiClient.GetToken(headers: _threadContext.Metadata, deadline: DateTime.UtcNow.AddSeconds(5), cancellationToken: cancellationToken);

            GetTokenRequest keyReq = new()
            {
                Key = publicKey
            };

            Task readTask = Task.Run(async() =>
            {
                await foreach (GetTokenReply message in call.ResponseStream.ReadAllAsync())
                {
                    if (!message.Challenge.IsEmpty)
                    {
                        byte[] challenge        = message.Challenge.ToByteArray();
                        byte[] signature        = await VerifySignature(challenge);
                        GetTokenRequest signReq = new()
                        {
                            Signature = ByteString.CopyFrom(signature)
                        };
                        await call.RequestStream.WriteAsync(signReq);
                        await call.RequestStream.CompleteAsync();
                    }
                    else if (message.Token != null)
                    {
                        token = message.Token;
                    }
                }
            }, cancellationToken);

            await call.RequestStream.WriteAsync(keyReq);

            await readTask;

            if (call.GetStatus().StatusCode == StatusCode.OK)
            {
                _threadContext.WithToken(token);
                return(token);
            }
            else
            {
                throw new InvalidOperationException(call.GetStatus().Detail);
            }
        }
Пример #3
0
 private bool CheckCallStatus()
 {
     try
     {
         if (stream.GetStatus().StatusCode == StatusCode.OK)
         {
             return(true);
         }
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
Пример #4
0
 public Status GetStatus()
 {
     return(m_Call.GetStatus());
 }