Esempio n. 1
0
        public async ValueTask <bool> EnsureCreatedAsync(CancellationToken cancellationToken = default)
        {
            Logger.LogTrace(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name);
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);
            try
            {
                using (Channel)
                {
                    var client = Channel.CreateGrpcService <IDatabaseControlAsync>();

                    var result = await client.EnsureCreatedAsync(CallContext).ConfigureAwait(false);

                    return(result.Result);
                }
            }
            catch (RpcException rex)
            {
                Logger.LogError(rex.Source + " - " + rex.Status.StatusCode + " " + rex.Status.Detail + " " + rex.Message + " " + rex.StackTrace);
            }
            catch (Exception ex)
            {
                Logger.LogError(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " - " + ex.Source + " " + ex.Message + " " + ex.StackTrace);
            }

            return(false);
        }
Esempio n. 2
0
        public string GetConnectionString(CancellationToken cancellationToken = default)
        {
            Logger.LogTrace(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name);
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);
            try
            {
                using (Channel)
                {
                    var client = Channel.CreateGrpcService <IDatabaseControl>();

                    return(client.GetConnectionString(CallContext).Result);
                }
            }
            catch (RpcException rex)
            {
                Logger.LogError(rex.Source + " - " + rex.Status.StatusCode + " " + rex.Status.Detail + " " + rex.Message + " " + rex.StackTrace);
            }
            catch (Exception ex)
            {
                Logger.LogError(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " - " + ex.Source + " " + ex.Message + " " + ex.StackTrace);
            }

            return(string.Empty);
        }
        private GrpcCall <TRequest, TResponse> CreateGrpcCall <TRequest, TResponse>(
            Method <TRequest, TResponse> method,
            CallOptions options,
            out Action disposeAction)
            where TRequest : class
            where TResponse : class
        {
            if (_client.BaseAddress == null)
            {
                throw new InvalidOperationException("Unable to send the gRPC call because no server address has been configured. " +
                                                    "Set HttpClient.BaseAddress on the HttpClient used to created to gRPC client.");
            }

            CancellationTokenSource?linkedCts = null;

            // Use propagated deadline if it is small than the specified deadline
            if (Deadline < options.Deadline)
            {
                options = options.WithDeadline(Deadline);
            }

            if (CancellationToken.CanBeCanceled)
            {
                if (options.CancellationToken.CanBeCanceled)
                {
                    // If both propagated and options cancellation token can be canceled
                    // then set a new linked token of both
                    linkedCts = CancellationTokenSource.CreateLinkedTokenSource(CancellationToken, options.CancellationToken);
                    options   = options.WithCancellationToken(linkedCts.Token);
                }
                else
                {
                    options = options.WithCancellationToken(CancellationToken);
                }
            }

            var call = new GrpcCall <TRequest, TResponse>(method, options, Clock, _loggerFactory);

            // Clean up linked cancellation token
            disposeAction = linkedCts != null
                ? () => { call.Dispose(); linkedCts !.Dispose(); }
                : (Action)call.Dispose;

            return(call);
        }
Esempio n. 4
0
        public IEnumerable <Dto.HeartBeat> SendHeartBeat(CancellationToken cancellationToken = default)
        {
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);

            using (Channel)
            {
                ITransaction client = Channel.CreateGrpcService <ITransaction>();

                return(client.SendHeartBeat(CallContext));
            }
        }
Esempio n. 5
0
        //public IEnumerable<TagIntegrationEvent> NotifyTagInvoke(Dto.Common.NotifyTagReq request, CancellationToken cancellationToken = default)
        //{
        //    //CallOptions = CallOptions.WithCancellationToken(cancellationToken);
        //    CallOptions = CallOptions.WithCancellationToken(cancellationToken);
        //    CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);
        //    using (Channel)
        //    {
        //        ITransaction client = Channel.CreateGrpcService<ITransaction>();

        //        return client.NotifyTagInvoke(request, CallContext);
        //    }
        //}

        public IAsyncEnumerable <TagIntegrationEvent> NotifyTagInvokeAsync(Dto.Biz.NotifyTagReq request, CancellationToken cancellationToken = default)
        {
            //CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);
            using (Channel)
            {
                ITransactionAsync client = Channel.CreateGrpcService <ITransactionAsync>();

                return(client.NotifyTagInvokeAsync(request, CallContext));
            }
        }
Esempio n. 6
0
        private GrpcCall <TRequest, TResponse> CreateGrpcCall <TRequest, TResponse>(
            Method <TRequest, TResponse> method,
            CallOptions options,
            out Action disposeAction)
            where TRequest : class
            where TResponse : class
        {
            CancellationTokenSource?linkedCts = null;

            // Use propagated deadline if it is small than the specified deadline
            if (Deadline < options.Deadline)
            {
                options = options.WithDeadline(Deadline);
            }

            if (CancellationToken.CanBeCanceled)
            {
                if (options.CancellationToken.CanBeCanceled)
                {
                    // If both propagated and options cancellation token can be canceled
                    // then set a new linked token of both
                    linkedCts = CancellationTokenSource.CreateLinkedTokenSource(CancellationToken, options.CancellationToken);
                    options   = options.WithCancellationToken(linkedCts.Token);
                }
                else
                {
                    options = options.WithCancellationToken(CancellationToken);
                }
            }

            var call = new GrpcCall <TRequest, TResponse>(method, options, Clock, _loggerFactory);

            // Clean up linked cancellation token
            disposeAction = linkedCts != null
                ? () => { call.Dispose(); linkedCts !.Dispose(); }
                : (Action)call.Dispose;

            return(call);
        }
Esempio n. 7
0
        public async ValueTask <ListKeyValuePair <Guid, string> > LoadDataAsync(CancellationToken cancellationToken = default)
        {
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);
            try
            {
                using (Channel)
                {
                    var client = Channel.CreateGrpcService <IQueryAsync>();

                    return(await client.ConnectionIdAsync(CallContext));
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " - " + ex.Source + " " + ex.Message + " " + ex.StackTrace);
            }
            return(null);
        }
Esempio n. 8
0
        public async Task <Srv.Dto.View.Joined.List.GridView.AllConnection> LoadAllRecordsAsync(CancellationToken cancellationToken = default)
        {
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);
            try
            {
                using (Channel)
                {
                    var client = Channel.CreateGrpcService <IQueryAsync>();

                    return(await client.AllConnectionAsync(CallContext));
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " - " + ex.Source + " " + ex.Message + " " + ex.StackTrace);
            }
            return(null);
        }
Esempio n. 9
0
        public void WithMethods()
        {
            var options = new CallOptions();

            var metadata = new Metadata();

            Assert.AreSame(metadata, options.WithHeaders(metadata).Headers);

            var deadline = DateTime.UtcNow;

            Assert.AreEqual(deadline, options.WithDeadline(deadline).Deadline.Value);

            var cancellationToken = new CancellationTokenSource().Token;

            Assert.AreEqual(cancellationToken, options.WithCancellationToken(cancellationToken).CancellationToken);

            var writeOptions = new WriteOptions();

            Assert.AreSame(writeOptions, options.WithWriteOptions(writeOptions).WriteOptions);

            var propagationToken = new ContextPropagationToken(CallSafeHandle.NullInstance, DateTime.UtcNow,
                                                               CancellationToken.None, ContextPropagationOptions.Default);

            Assert.AreSame(propagationToken, options.WithPropagationToken(propagationToken).PropagationToken);

            var credentials = new FakeCallCredentials();

            Assert.AreSame(credentials, options.WithCredentials(credentials).Credentials);

            var flags = CallFlags.WaitForReady | CallFlags.CacheableRequest;

            Assert.AreEqual(flags, options.WithFlags(flags).Flags);

            // Check that the original instance is unchanged.
            Assert.IsNull(options.Headers);
            Assert.IsNull(options.Deadline);
            Assert.AreEqual(CancellationToken.None, options.CancellationToken);
            Assert.IsNull(options.WriteOptions);
            Assert.IsNull(options.PropagationToken);
            Assert.IsNull(options.Credentials);
            Assert.AreEqual(default(CallFlags), options.Flags);
        }
Esempio n. 10
0
        public async ValueTask <Srv.Dto.S7.Model.S7Connection> FindAsync(IdObject idObject, CancellationToken cancellationToken = default)
        {
            Logger.LogTrace("Query Agent: " + GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " " + idObject.Id);
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);
            try
            {
                using (Channel)
                {
                    var client = Channel.CreateGrpcService <IQueryAsync>();

                    return(await client.GetS7ConnectionModelByIdAsync(idObject, CallContext));
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " - " + ex.Source + " " + ex.Message + " " + ex.StackTrace);
            }
            return(await new ValueTask <Dto.S7.Model.S7Connection>());
        }
Esempio n. 11
0
        public Srv.Dto.Common.TagGroup Find(IdObject idObject, CancellationToken cancellationToken = default)
        {
            Logger.LogTrace("Query Agent: " + GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " " + idObject.Id);
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);
            try
            {
                using (Channel)
                {
                    var client = Channel.CreateGrpcService <IQuery>();

                    return(client.GetTagGroupById(idObject, CallContext));
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " - " + ex.Source + " " + ex.Message + " " + ex.StackTrace);
            }
            return(new Dto.Common.TagGroup());
        }
        private CallOptions ApplyConfigToCallOptions(CallOptions callOptions)
        {
            if (callOptions.Headers == null)
            {
                // Add empty metadata to options:
                callOptions = callOptions.WithHeaders(new Metadata());
            }

            if (_configuration.WaitForReady && callOptions.IsWaitForReady != _configuration.WaitForReady)
            {
                callOptions = callOptions.WithWaitForReady();
            }

            if (_configuration.FallbackCancellationToken != default && callOptions.CancellationToken != _configuration.FallbackCancellationToken)
            {
                callOptions = callOptions.WithCancellationToken(_configuration.FallbackCancellationToken);
            }

            return(callOptions);
        }
Esempio n. 13
0
        public async ValueTask <WriteLog> WriteLogsAsync(App.Dto.Req.Biz.List.WriteLog request, CancellationToken cancellationToken = default)
        {
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);

            try
            {
                using (Channel)
                {
                    IBizAsync client = Channel.CreateGrpcService <IBizAsync>();

                    return(await client.WriteLogsAsync(request, CallContext));
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " - " + ex.Source + " " + ex.Message + " " + ex.StackTrace);
            }

            return(await new ValueTask <WriteLog>());
        }
Esempio n. 14
0
        public async ValueTask <App.Dto.Res.Add.Common.TagGroup> AddAsync(App.Dto.Req.Add.Common.TagGroup request, CancellationToken cancellationToken = default)
        {
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);
            App.Dto.Res.Add.Common.TagGroup output = new App.Dto.Res.Add.Common.TagGroup();
            try
            {
                using (Channel)
                {
                    var client = Channel.CreateGrpcService <ICommandAsync>();

                    var result = await client.AddTagGroupAsync(request, CallContext);

                    output = result;
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " - " + ex.Source + " " + ex.Message + " " + ex.StackTrace);
            }
            return(output);
        }
Esempio n. 15
0
        public App.Dto.Res.Transaction.GetTagValue GetTagValue(GetTagValue request, CancellationToken cancellationToken = default)
        {
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);
            App.Dto.Res.Transaction.GetTagValue output = new App.Dto.Res.Transaction.GetTagValue();
            try
            {
                using (Channel)
                {
                    ITransaction client = Channel.CreateGrpcService <ITransaction>();

                    var result = client.GetTagValue(request, CallContext);

                    output = result;
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " - " + ex.Source + " " + ex.Message + " " + ex.StackTrace);
            }
            return(output);
        }
Esempio n. 16
0
        public bool ModifyField(App.Dto.Req.ModifyField.Common.TagGroup request, CancellationToken cancellationToken = default)
        {
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);
            var output = false;

            try
            {
                using (Channel)
                {
                    var client = Channel.CreateGrpcService <ICommand>();

                    var result = client.ModifyTagGroupField(request, CallContext);

                    output = result.Result;
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " - " + ex.Source + " " + ex.Message + " " + ex.StackTrace);
            }
            return(output);
        }
Esempio n. 17
0
        public WriteLog WriteLogs(App.Dto.Req.Biz.List.WriteLog request, CancellationToken cancellationToken = default)
        {
            CallOptions = CallOptions.WithCancellationToken(cancellationToken);
            CallContext = new CallContext(CallOptions, CallContextFlags.IgnoreStreamTermination);
            WriteLog output = new WriteLog();

            try
            {
                using (Channel)
                {
                    IBiz client = Channel.CreateGrpcService <IBiz>();

                    var result = client.WriteLogs(request, CallContext);

                    output = result;
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod()?.Name + " - " + ex.Source + " " + ex.Message + " " + ex.StackTrace);
            }
            return(output);
        }
Esempio n. 18
0
        /// <summary>
        /// Creates a populated call options instance based on the provided parameters
        /// </summary>
        /// <param name="metadata">The metadata to be added for the call.</param>
        /// <param name="credentials">The credentials associated with the call</param>
        /// <param name="timeout">The timespan to add to the current UTC time to act as the call deadline</param>
        /// <param name="cancellationToken">A best effort cancellation token</param>
        /// <returns>A call options instace</returns>
        public static CallOptions GetCallOptions(Metadata metadata = null, CallCredentials credentials = null, TimeSpan?timeout = null, CancellationToken cancellationToken = default)
        {
            var options = new CallOptions();

            if (metadata != null)
            {
                options = options.WithHeaders(metadata);
            }
            if (cancellationToken != default)
            {
                options = options.WithCancellationToken(cancellationToken);
            }
            if (timeout.HasValue)
            {
                options = options.WithDeadline(DateTime.UtcNow.Add(timeout.Value));
            }
            if (credentials != null)
            {
                options = options.WithCredentials(credentials);
            }

            return(options);
        }
Esempio n. 19
0
        public void WithMethods()
        {
            var options = new CallOptions();

            var metadata = new Metadata();

            Assert.AreSame(metadata, options.WithHeaders(metadata).Headers);

            var deadline = DateTime.UtcNow;

            Assert.AreEqual(deadline, options.WithDeadline(deadline).Deadline.Value);

            var token = new CancellationTokenSource().Token;

            Assert.AreEqual(token, options.WithCancellationToken(token).CancellationToken);

            // Change original instance is unchanged.
            Assert.IsNull(options.Headers);
            Assert.IsNull(options.Deadline);
            Assert.AreEqual(CancellationToken.None, options.CancellationToken);
            Assert.IsNull(options.WriteOptions);
            Assert.IsNull(options.PropagationToken);
            Assert.IsNull(options.Credentials);
        }