From a7fdbfcf748ce5caf49bcf8b9695888748bd5f91 Mon Sep 17 00:00:00 2001 From: ShunXian Cai Date: Wed, 28 Jun 2017 21:54:40 -0700 Subject: [PATCH] allow default attribute constructor to take no args --- EventGridBinding/Function.cs | 2 +- .../EventGridTriggerAttribute.cs | 5 +++++ ...ventGridTriggerAttributeBindingProvider.cs | 20 ++++++++++--------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/EventGridBinding/Function.cs b/EventGridBinding/Function.cs index 9604ec4..329b9dc 100644 --- a/EventGridBinding/Function.cs +++ b/EventGridBinding/Function.cs @@ -6,7 +6,7 @@ namespace EventGridBinding { public static class Function { - public static void testEventGrid([EventGridTrigger("eventhubarchive")] EventGridEvent value) + public static void testEventGrid([EventGridTrigger] EventGridEvent value) { Console.WriteLine(value); } diff --git a/EventGridExtension/EventGridTriggerAttribute.cs b/EventGridExtension/EventGridTriggerAttribute.cs index c228085..0829538 100644 --- a/EventGridExtension/EventGridTriggerAttribute.cs +++ b/EventGridExtension/EventGridTriggerAttribute.cs @@ -11,6 +11,11 @@ namespace Microsoft.Azure.WebJobs public sealed class EventGridTriggerAttribute : Attribute { public const string eventHubArchive = "eventHubArchive"; + public EventGridTriggerAttribute() + { + Publisher = null; // if this is not provided, only EventGridEvent can be parsed + } + public EventGridTriggerAttribute(string publisher) { // FIXME diff --git a/EventGridExtension/EventGridTriggerAttributeBindingProvider.cs b/EventGridExtension/EventGridTriggerAttributeBindingProvider.cs index b99bd12..38b9305 100644 --- a/EventGridExtension/EventGridTriggerAttributeBindingProvider.cs +++ b/EventGridExtension/EventGridTriggerAttributeBindingProvider.cs @@ -40,18 +40,20 @@ namespace Microsoft.Azure.WebJobs return Task.FromResult(null); } - if (parameter.ParameterType == typeof(EventGridEvent)) - { - // universally supported - return Task.FromResult(new EventGridTriggerBinding(context.Parameter, _extensionConfigProvider, context.Parameter.Member.Name)); - } - // depends on the publisher, we could have different expectation for paramter string publisher = attribute.Publisher; - if (publisher == EventGridTriggerAttribute.eventHubArchive && parameter.ParameterType != typeof(Stream)) + if (String.IsNullOrEmpty(publisher) && !(parameter.ParameterType == typeof(EventGridEvent))) + { + throw new InvalidOperationException($"Can only bind EventGridTriggerAttribute to type 'EventGridEvent' when no publisher is specified."); + } + else if (parameter.ParameterType == typeof(EventGridEvent)) + { + // always valid, no need to check for publisher and what the specify parameter publisher can parse to + } + else if (publisher == EventGridTriggerAttribute.eventHubArchive && parameter.ParameterType != typeof(Stream)) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, - "Can't bind EventGridTriggerAttribute to type '{0}'.", parameter.ParameterType)); + "Can't bind EventGridTriggerAttribute with publisher '{0}' to type '{1}'.", publisher, parameter.ParameterType)); } // unsupported publisher is caught in attribute constrcutor return Task.FromResult(new EventGridTriggerBinding(context.Parameter, _extensionConfigProvider, context.Parameter.Member.Name, publisher)); @@ -68,7 +70,7 @@ namespace Microsoft.Azure.WebJobs private object _value; private readonly string _publisher; - public EventGridTriggerBinding(ParameterInfo parameter, EventGridExtensionConfig listenersStore, string functionName, string publisher = null) + public EventGridTriggerBinding(ParameterInfo parameter, EventGridExtensionConfig listenersStore, string functionName, string publisher) { _publisher = publisher; _listenersStore = listenersStore;