Example #1
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);
        }
Example #2
0
        public async Task <TValue> BindAsync <TValue>(Attribute attribute, CancellationToken cancellationToken)
        {
            IBinding binding = await _bindingSource.BindAsync <TValue>(attribute, cancellationToken);

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

            IValueProvider provider = await binding.BindAsync(new BindingContext(
                                                                  _bindingSource.AmbientBindingContext, cancellationToken));

            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.

            string value = provider.ToInvokeString();

            IWatchable watchable = provider as IWatchable;

            // Add even if watchable is null to show parameter descriptor in status.
            _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);
            }

            return((TValue)provider.GetValue());
        }
Example #3
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);
                }