Esempio n. 1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                await Task.Delay(1000, stoppingToken).ConfigureAwait(false);

                if (!(cluster.Leader?.IsRemote ?? true))
                {
                    var newValue = await valueProvider.GetValueAsync().ConfigureAwait(false) + 500L;

                    Console.WriteLine("Saving value {0} generated by the leader node", newValue);
                    try
                    {
                        await cluster.WriteAsync(new LogEntryProducer <IRaftLogEntry>(new Int64LogEntry(newValue)
                        {
                            Term = cluster.Term
                        }), WriteConcern.LeaderOnly, TimeSpan.FromSeconds(2));
                    }
                    catch (TimeoutException e)
                    {
                        Console.WriteLine("Unable to write value {0} because timeout is occurred: {1}", newValue, e);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Unexpected error {0}", e);
                    }
                }
            }
        }
            public async Task <ITriggerData> BindAsync(object value, ValueBindingContext context)
            {
                HttpRequest request = value as HttpRequest;

                if (request == null)
                {
                    throw new NotSupportedException("An HttpRequest is required");
                }

                IValueProvider valueProvider = null;
                object         poco          = null;
                IReadOnlyDictionary <string, object> userTypeBindingData = null;
                string invokeString = ToInvokeString(request);

                if (_isUserTypeBinding)
                {
                    valueProvider = await CreateUserTypeValueProvider(request, invokeString);

                    if (_bindingDataProvider != null)
                    {
                        // some binding data is defined by the user type
                        // the provider might be null if the Type is invalid, or if the Type
                        // has no public properties to bind to
                        poco = await valueProvider.GetValueAsync();

                        userTypeBindingData = _bindingDataProvider.GetBindingData(poco);
                    }
                }
                else
                {
                    valueProvider = new HttpRequestValueBinder(_parameter, request, invokeString);
                }

                // create a modifiable collection of binding data and
                // copy in any initial binding data from the poco
                var aggregateBindingData = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

                aggregateBindingData.AddRange(userTypeBindingData);

                aggregateBindingData[RequestBindingName] = request;

                // Apply additional binding data coming from request route, query params, etc.
                var requestBindingData = await GetRequestBindingDataAsync(request, _bindingDataContract);

                aggregateBindingData.AddRange(requestBindingData);

                // apply binding data to the user type
                if (poco != null && aggregateBindingData.Count > 0)
                {
                    ApplyBindingData(poco, aggregateBindingData);
                }

                IValueBinder returnProvider = new ResponseHandler(request, _responseHook);

                return(new TriggerData(valueProvider, aggregateBindingData)
                {
                    ReturnValueProvider = returnProvider
                });
            }
Esempio n. 3
0
        /// <summary>
        /// Binds the specified collection of attributes.
        /// </summary>
        /// <typeparam name="TValue">The type to bind to.</typeparam>
        /// <param name="attributes">The collection of attributes to bind. The first attribute in the
        /// collection should be the primary attribute.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <returns></returns>
        public virtual async Task <TValue> BindAsync <TValue>(Attribute[] attributes, CancellationToken cancellationToken = default(CancellationToken))
        {
            var attribute            = attributes.First();
            var additionalAttributes = attributes.Skip(1).ToArray();

            IBinding binding = await _bindingSource.BindAsync <TValue>(attribute, additionalAttributes, cancellationToken);

            if (binding == null)
            {
                throw new InvalidOperationException("No binding found for attribute '" + attribute.GetType() + "'.");
            }

            // Create a clone of the binding context, so any binding data that was added
            // will be applied to the binding
            var ambientBindingContext = new AmbientBindingContext(_bindingSource.AmbientBindingContext.FunctionContext, _bindingData);
            var bindingContext        = new BindingContext(ambientBindingContext, cancellationToken);

            IValueProvider provider = await binding.BindAsync(bindingContext);

            if (provider == null)
            {
                return(default(TValue));
            }

            Debug.Assert(provider.Type == typeof(TValue));

            ParameterDescriptor parameterDesciptor = binding.ToParameterDescriptor();

            parameterDesciptor.Name = null; // Remove the dummy name "?" used for runtime binding.

            // Add even if watchable is null to show parameter descriptor in status.
            string     value     = provider.ToInvokeString();
            IWatchable watchable = provider as IWatchable;

            _watcher.Add(parameterDesciptor, value, watchable);

            IValueBinder binder = provider as IValueBinder;

            if (binder != null)
            {
                _binders.Add(binder);
            }

            IDisposable disposableProvider = provider as IDisposable;

            if (disposableProvider != null)
            {
                _disposable.Add(disposableProvider);
            }

            object result = await provider.GetValueAsync();

            return((TValue)result);
        }
Esempio n. 4
0
        private static async Task <SingletonLock> GetSingletonLockAsync(IReadOnlyDictionary <string, IValueProvider> parameters)
        {
            IValueProvider singletonValueProvider = null;
            SingletonLock  singleton = null;

            if (parameters.TryGetValue(SingletonValueProvider.SingletonParameterName, out singletonValueProvider))
            {
                singleton = (SingletonLock)(await singletonValueProvider.GetValueAsync());
            }

            return(singleton);
        }
Esempio n. 5
0
                private async Task <IValueProvider> WrapAsync(IValueProvider result, ValueBindingContext context)
                {
                    var obj = await result.GetValueAsync();

                    if (obj is TraceWriter trace)
                    {
                        var shortName            = context.FunctionContext.MethodName;
                        FunctionDescriptor descr = _parent._funcLookup(shortName);
                        var functionLogger       = descr.Invoker.FunctionLogger;

                        // This is the critical call
                        trace = functionLogger.CreateUserTraceWriter(trace);

                        return(new SimpleValueProvider(typeof(TraceWriter), trace, result.ToInvokeString()));
                    }
                    return(result);
                }