public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            ParameterInfo        parameter = context.Parameter;
            HttpTriggerAttribute attribute = parameter.GetCustomAttribute <HttpTriggerAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <ITriggerBinding>(null));
            }

            // Can bind to user types, HttpRequestMessage, object (for dynamic binding support) and all the Read
            // Types supported by StreamValueBinder
            IEnumerable <Type> supportedTypes = StreamValueBinder.GetSupportedTypes(FileAccess.Read)
                                                .Union(new Type[] { typeof(HttpRequest), typeof(object), typeof(HttpRequestMessage) });
            bool isSupportedTypeBinding = ValueBinder.MatchParameterType(parameter, supportedTypes);
            bool isUserTypeBinding      = !isSupportedTypeBinding && IsValidUserType(parameter.ParameterType);

            if (!isSupportedTypeBinding && !isUserTypeBinding)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                  "Can't bind HttpTriggerAttribute to type '{0}'.", parameter.ParameterType));
            }

            return(Task.FromResult <ITriggerBinding>(new HttpTriggerBinding(attribute, context.Parameter, isUserTypeBinding, _responseHook)));
        }
        public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            ParameterInfo          parameter = context.Parameter;
            ManualTriggerAttribute attribute = parameter.GetCustomAttribute <ManualTriggerAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <ITriggerBinding>(null));
            }

            // Can bind to user types, and all the Read Types supported by StreamValueBinder
            IEnumerable <Type> supportedTypes = StreamValueBinder.GetSupportedTypes(FileAccess.Read);
            bool isSupportedTypeBinding       = ValueBinder.MatchParameterType(parameter, supportedTypes);
            bool isUserTypeBinding            = !isSupportedTypeBinding && Utility.IsValidUserType(parameter.ParameterType);

            if (!isSupportedTypeBinding && !isUserTypeBinding)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                  "Can't bind ManualTriggerAttribute to type '{0}'.", parameter.ParameterType));
            }

            return(Task.FromResult <ITriggerBinding>(new ManualTriggerBinding(context.Parameter, isUserTypeBinding)));
        }
Exemple #3
0
        /// <inheritdoc/>
        public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            ParameterInfo        parameter = context.Parameter;
            FileTriggerAttribute attribute = parameter.GetCustomAttribute <FileTriggerAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <ITriggerBinding>(null));
            }

            // next, verify that the type is one of the types we support
            IEnumerable <Type> types = StreamValueBinder.GetSupportedTypes(FileAccess.Read)
                                       .Union(new Type[] { typeof(FileStream), typeof(FileSystemEventArgs), typeof(FileInfo) });

            if (!ValueBinder.MatchParameterType(context.Parameter, types))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                  "Can't bind FileTriggerAttribute to type '{0}'.", parameter.ParameterType));
            }

            return(Task.FromResult <ITriggerBinding>(new FileTriggerBinding(_config, parameter, _logger)));
        }
        public Task <IBinding> TryCreateAsync(BindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            ParameterInfo parameter = context.Parameter;
            FtpAttribute  attribute = parameter.GetCustomAttribute <FtpAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <IBinding>(null));
            }

            string path = attribute.Path;

            if (_nameResolver != null)
            {
                path = _nameResolver.ResolveWholeString(path);
            }
            BindingTemplate bindingTemplate = BindingTemplate.FromString(path);

            bindingTemplate.ValidateContractCompatibility(context.BindingDataContract);

            IEnumerable <Type> types = StreamValueBinder.GetSupportedTypes(FileAccess.Read);

            if (!ValueBinder.MatchParameterType(context.Parameter, types))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                  "Can't bind FtpAttribute to type '{0}'.", parameter.ParameterType));
            }

            return(Task.FromResult <IBinding>(new FtpBinding(_config, parameter, bindingTemplate)));
        }
Exemple #5
0
        public Task <IBinding> TryCreateAsync(BindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // Determine whether we should bind to the current parameter
            ParameterInfo   parameter = context.Parameter;
            SampleAttribute attribute = parameter.GetCustomAttribute <SampleAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <IBinding>(null));
            }

            // TODO: Include any other parameter types this binding supports in this check
            IEnumerable <Type> supportedTypes = StreamValueBinder.GetSupportedTypes(FileAccess.ReadWrite);

            if (!ValueBinder.MatchParameterType(context.Parameter, supportedTypes))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                  "Can't bind SampleAttribute to type '{0}'.", parameter.ParameterType));
            }

            return(Task.FromResult <IBinding>(new SampleBinding(parameter)));
        }
Exemple #6
0
        public void GetSupportedTypes_FileAccessWrite_ReturnsExpectedTypes()
        {
            Type[] expected = new Type[]
            {
                typeof(Stream),
                typeof(TextWriter),
                typeof(StreamWriter),
                typeof(string),
                typeof(byte[])
            };

            IEnumerable <Type> result = StreamValueBinder.GetSupportedTypes(FileAccess.Write);

            Assert.True(expected.SequenceEqual(result));
        }
Exemple #7
0
        public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            ParameterInfo           parameter = context.Parameter;
            WebHookTriggerAttribute attribute = parameter.GetCustomAttribute <WebHookTriggerAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <ITriggerBinding>(null));
            }

            // Can bind to user types, HttpRequestMessage, WebHookContext, and all the Read
            // Types supported by StreamValueBinder
            IEnumerable <Type> supportedTypes = StreamValueBinder.GetSupportedTypes(FileAccess.Read)
                                                .Union(new Type[] { typeof(HttpRequestMessage), typeof(WebHookContext) });
            bool isSupportedTypeBinding = ValueBinder.MatchParameterType(parameter, supportedTypes);
            bool isUserTypeBinding      = !isSupportedTypeBinding && WebHookTriggerBinding.IsValidUserType(parameter.ParameterType);

            if (!isSupportedTypeBinding && !isUserTypeBinding)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                  "Can't bind WebHookTriggerAttribute to type '{0}'.", parameter.ParameterType));
            }

            if (!isUserTypeBinding && attribute.FromUri)
            {
                throw new InvalidOperationException("'FromUri' can only be set to True when binding to custom Types.");
            }

            // Validate route format
            if (!string.IsNullOrEmpty(attribute.Route))
            {
                string[] routeSegements = attribute.Route.Split('/');
                if (routeSegements.Length > 2)
                {
                    throw new InvalidOperationException("WebHook routes can only have a maximum of two segments.");
                }
            }

            return(Task.FromResult <ITriggerBinding>(new WebHookTriggerBinding(_dispatcher, context.Parameter, isUserTypeBinding, attribute)));
        }
        /// <inheritdoc/>
        public Task <IBinding> TryCreateAsync(BindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // Determine whether we should bind to the current parameter
            ParameterInfo parameter = context.Parameter;
            FileAttribute attribute = parameter.GetCustomAttribute <FileAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <IBinding>(null));
            }

            // first, verify the file path binding (if it contains binding parameters)
            string path = attribute.Path;

            if (_nameResolver != null)
            {
                path = _nameResolver.ResolveWholeString(path);
            }
            BindingTemplate bindingTemplate = BindingTemplate.FromString(path);

            bindingTemplate.ValidateContractCompatibility(context.BindingDataContract);

            IEnumerable <Type> types = StreamValueBinder.GetSupportedTypes(attribute.Access)
                                       .Union(new Type[] { typeof(FileStream), typeof(FileInfo) });

            if (!ValueBinder.MatchParameterType(context.Parameter, types))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                  "Can't bind FileAttribute to type '{0}'.", parameter.ParameterType));
            }

            return(Task.FromResult <IBinding>(new FileBinding(_options, parameter, bindingTemplate)));
        }