public void GenerateServiceBusTriggerFunction() { ServiceBusBindingMetadata trigger = new ServiceBusBindingMetadata { Type = BindingType.ServiceBusTrigger, TopicName = "testTopic", SubscriptionName = "testSubscription", AccessRights = AccessRights.Listen }; MethodInfo method = GenerateMethod(trigger); VerifyCommonProperties(method); // verify trigger parameter ParameterInfo parameter = method.GetParameters()[0]; Assert.Equal("input", parameter.Name); Assert.Equal(typeof(string), parameter.ParameterType); ServiceBusTriggerAttribute attribute = parameter.GetCustomAttribute <ServiceBusTriggerAttribute>(); Assert.Equal(null, attribute.QueueName); Assert.Equal("testTopic", attribute.TopicName); Assert.Equal("testSubscription", attribute.SubscriptionName); Assert.Equal(AccessRights.Listen, attribute.Access); }
public void GenerateServiceBusTriggerFunction() { BindingMetadata trigger = BindingMetadata.Create(new JObject { { "type", "ServiceBusTrigger" }, { "name", "input" }, { "direction", "in" }, { "topicName", "testTopic" }, { "subscriptionName", "testSubscription" }, { "accessRights", "Listen" } }); MethodInfo method = GenerateMethod(trigger); VerifyCommonProperties(method); // verify trigger parameter ParameterInfo parameter = method.GetParameters()[0]; Assert.Equal("input", parameter.Name); Assert.Equal(typeof(string), parameter.ParameterType); ServiceBusTriggerAttribute attribute = parameter.GetCustomAttribute <ServiceBusTriggerAttribute>(); Assert.Equal(null, attribute.QueueName); Assert.Equal("testTopic", attribute.TopicName); Assert.Equal("testSubscription", attribute.SubscriptionName); Assert.Equal(AccessRights.Listen, attribute.Access); }
public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context) { if (context == null) { throw new ArgumentNullException("context"); } ParameterInfo parameter = context.Parameter; ServiceBusTriggerAttribute attribute = parameter.GetCustomAttribute <ServiceBusTriggerAttribute>(inherit: false); if (attribute == null) { return(Task.FromResult <ITriggerBinding>(null)); } string queueName = null; string topicName = null; string subscriptionName = null; string entityPath = null; if (attribute.QueueName != null) { queueName = Resolve(attribute.QueueName); entityPath = queueName; } else { topicName = Resolve(attribute.TopicName); subscriptionName = Resolve(attribute.SubscriptionName); entityPath = SubscriptionClient.FormatSubscriptionPath(topicName, subscriptionName); } ITriggerDataArgumentBinding <BrokeredMessage> argumentBinding = InnerProvider.TryCreate(parameter); if (argumentBinding == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Can't bind ServiceBusTrigger to type '{0}'.", parameter.ParameterType)); } string connectionName = ServiceBusAccount.GetAccountOverrideOrNull(context.Parameter); ServiceBusAccount account = new ServiceBusAccount { MessagingFactory = _config.MessagingProvider.CreateMessagingFactory(entityPath, connectionName), NamespaceManager = _config.MessagingProvider.CreateNamespaceManager(connectionName) }; ITriggerBinding binding; if (queueName != null) { binding = new ServiceBusTriggerBinding(parameter.Name, parameter.ParameterType, argumentBinding, account, queueName, attribute.Access, _config); } else { binding = new ServiceBusTriggerBinding(parameter.Name, argumentBinding, account, topicName, subscriptionName, attribute.Access, _config); } return(Task.FromResult <ITriggerBinding>(binding)); }
// https://github.com/Azure/azure-functions-vs-build-sdk/issues/1 public void ServiceBusTriggerShouldHaveStringEnumForAccessRights() { var attribute = new ServiceBusTriggerAttribute("queue1", AccessRights.Manage); var jObject = attribute.ToJObject(); jObject.Should().HaveElement("access"); jObject["access"].Should().Be("manage"); }
/// <summary> /// Gets 'AutoCompleteMessages' option value, either from the trigger attribute if it is set or from host options. /// </summary> /// <param name="attribute">The trigger attribute.</param> /// <param name="functionName">The function name.</param> private bool GetAutoCompleteMessagesOptionToUse(ServiceBusTriggerAttribute attribute, string functionName) { if (attribute.IsAutoCompleteMessagesOptionSet) { _logger.LogInformation($"The 'AutoCompleteMessages' option has been overriden to '{attribute.AutoCompleteMessages}' value for '{functionName}' function."); return(attribute.AutoCompleteMessages); } return(_options.AutoCompleteMessages); }
public void GetConnectionString_ReturnsExpectedConnectionString() { string defaultConnection = "Endpoint=sb://default.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=abc123="; var options = new ServiceBusOptions() { ConnectionString = defaultConnection }; var attribute = new ServiceBusTriggerAttribute("entity-name"); var account = new ServiceBusAccount(options, _configuration, attribute); Assert.True(defaultConnection == account.ConnectionString); }
public void GetAttributeBuilderInfo_ServiceBusTriggerAttribute_Queue() { ServiceBusTriggerAttribute attribute = new ServiceBusTriggerAttribute("myQueue", Microsoft.ServiceBus.Messaging.AccessRights.Listen); var builderInfo = ExtensionBinding.GetAttributeBuilderInfo(attribute); ServiceBusTriggerAttribute result = (ServiceBusTriggerAttribute)builderInfo.Constructor.Invoke(builderInfo.ConstructorArgs); Assert.Equal(attribute.QueueName, result.QueueName); Assert.Equal(attribute.Access, result.Access); Assert.Null(result.TopicName); Assert.Null(result.SubscriptionName); Assert.Equal(0, builderInfo.Properties.Count); }
public void Constructor_Topic_SetsExpectedValues() { ServiceBusTriggerAttribute attribute = new ServiceBusTriggerAttribute("testtopic", "testsubscription"); Assert.Null(attribute.QueueName); Assert.Equal("testtopic", attribute.TopicName); Assert.Equal("testsubscription", attribute.SubscriptionName); attribute = new ServiceBusTriggerAttribute("testtopic", "testsubscription"); Assert.Null(attribute.QueueName); Assert.Equal("testtopic", attribute.TopicName); Assert.Equal("testsubscription", attribute.SubscriptionName); }
public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context) { if (context == null) { throw new ArgumentNullException("context"); } ParameterInfo parameter = context.Parameter; ServiceBusTriggerAttribute attribute = parameter.GetCustomAttribute <ServiceBusTriggerAttribute>(inherit: false); if (attribute == null) { return(Task.FromResult <ITriggerBinding>(null)); } string queueName = null; string topicName = null; string subscriptionName = null; if (attribute.QueueName != null) { queueName = Resolve(attribute.QueueName); } else { topicName = Resolve(attribute.TopicName); subscriptionName = Resolve(attribute.SubscriptionName); } ITriggerDataArgumentBinding <BrokeredMessage> argumentBinding = InnerProvider.TryCreate(parameter); if (argumentBinding == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Can't bind ServiceBusTrigger to type '{0}'.", parameter.ParameterType)); } ServiceBusAccount account = ServiceBusAccount.CreateFromConnectionString(_config.ConnectionString); ITriggerBinding binding; if (queueName != null) { binding = new ServiceBusTriggerBinding(parameter.Name, parameter.ParameterType, argumentBinding, account, queueName, attribute.Access); } else { binding = new ServiceBusTriggerBinding(parameter.Name, argumentBinding, account, topicName, subscriptionName, attribute.Access); } return(Task.FromResult(binding)); }
public void Constructor_Queue_SetsExpectedValues() { ServiceBusTriggerAttribute attribute = new ServiceBusTriggerAttribute("testqueue"); Assert.Equal("testqueue", attribute.QueueName); Assert.Null(attribute.SubscriptionName); Assert.Null(attribute.TopicName); Assert.Equal(AccessRights.Manage, attribute.Access); attribute = new ServiceBusTriggerAttribute("testqueue", AccessRights.Listen); Assert.Equal("testqueue", attribute.QueueName); Assert.Null(attribute.SubscriptionName); Assert.Null(attribute.TopicName); Assert.Equal(AccessRights.Listen, attribute.Access); }
public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context) { ParameterInfo parameter = context.Parameter; ServiceBusTriggerAttribute serviceBusTrigger = parameter.GetCustomAttribute <ServiceBusTriggerAttribute>(inherit: false); if (serviceBusTrigger == null) { return(Task.FromResult <ITriggerBinding>(null)); } string queueName = null; string topicName = null; string subscriptionName = null; if (serviceBusTrigger.QueueName != null) { queueName = Resolve(serviceBusTrigger.QueueName); } else { topicName = Resolve(serviceBusTrigger.TopicName); subscriptionName = Resolve(serviceBusTrigger.SubscriptionName); } ITriggerDataArgumentBinding <BrokeredMessage> argumentBinding = _innerProvider.TryCreate(parameter); if (argumentBinding == null) { throw new InvalidOperationException("Can't bind ServiceBusTrigger to type '" + parameter.ParameterType + "'."); } string connectionString = _accountProvider.ConnectionString; ServiceBusAccount account = ServiceBusAccount.CreateFromConnectionString(connectionString); ITriggerBinding binding; if (queueName != null) { binding = new ServiceBusTriggerBinding(parameter.Name, parameter.ParameterType, argumentBinding, account, queueName); } else { binding = new ServiceBusTriggerBinding(parameter.Name, argumentBinding, account, topicName, subscriptionName); } return(Task.FromResult(binding)); }
/// <summary> /// Gets service bus options after applying function level options if needed. /// </summary> /// <param name="attribute">The trigger attribute.</param> /// <param name="functionName">The function name.</param> private ServiceBusOptions GetServiceBusOptions(ServiceBusTriggerAttribute attribute, string functionName) { var options = ServiceBusOptions.DeepClone(_options); options.ExceptionHandler += (e) => { LogExceptionReceivedEvent(e, functionName, _loggerFactory); }; if (attribute.IsAutoCompleteOptionSet) { _logger.LogInformation($"The 'AutoComplete' option has been overrriden to '{attribute.AutoComplete}' value for '{functionName}' function."); options.BatchOptions.AutoComplete = options.MessageHandlerOptions.AutoComplete = options.SessionHandlerOptions.AutoComplete = attribute.AutoComplete; } return(options); }
public override Collection <Attribute> GetAttributes() { Collection <Attribute> attributes = new Collection <Attribute>(); string queueName = Context.GetMetadataValue <string>("queueName"); string topicName = Context.GetMetadataValue <string>("topicName"); string subscriptionName = Context.GetMetadataValue <string>("subscriptionName"); var accessRights = Context.GetMetadataEnumValue <Microsoft.ServiceBus.Messaging.AccessRights>("accessRights"); Attribute attribute = null; if (Context.IsTrigger) { if (!string.IsNullOrEmpty(topicName) && !string.IsNullOrEmpty(subscriptionName)) { attribute = new ServiceBusTriggerAttribute(topicName, subscriptionName, accessRights); } else if (!string.IsNullOrEmpty(queueName)) { attribute = new ServiceBusTriggerAttribute(queueName, accessRights); } } else { attribute = new ServiceBusAttribute(queueName ?? topicName, accessRights) { EntityType = string.IsNullOrEmpty(topicName) ? EntityType.Queue : EntityType.Topic }; } if (attribute == null) { throw new InvalidOperationException("Invalid ServiceBus trigger configuration."); } attributes.Add(attribute); var connectionProvider = (IConnectionProvider)attribute; string connection = Context.GetMetadataValue <string>("connection"); if (!string.IsNullOrEmpty(connection)) { connectionProvider.Connection = connection; } return(attributes); }
public void GetConnectionString_ThrowsIfConnectionStringNullOrEmpty() { var config = new ServiceBusOptions(); var attribute = new ServiceBusTriggerAttribute("testqueue"); attribute.Connection = "MissingConnection"; var ex = Assert.Throws <InvalidOperationException>(() => { var account = new ServiceBusAccount(config, _configuration, attribute); var cs = account.ConnectionString; }); Assert.AreEqual("Microsoft Azure WebJobs SDK ServiceBus connection string 'MissingConnection' is missing or empty.", ex.Message); attribute.Connection = null; config.ConnectionString = null; ex = Assert.Throws <InvalidOperationException>(() => { var account = new ServiceBusAccount(config, _configuration, attribute); var cs = account.ConnectionString; }); Assert.AreEqual("Microsoft Azure WebJobs SDK ServiceBus connection string 'AzureWebJobsServiceBus' is missing or empty.", ex.Message); }
public void GetAttributeBuilderInfo_ServiceBusTriggerAttribute_Queue() { ServiceBusTriggerAttribute attribute = new ServiceBusTriggerAttribute("myQueue", Microsoft.ServiceBus.Messaging.AccessRights.Listen); var builderInfo = ExtensionBinding.GetAttributeBuilderInfo(attribute); ServiceBusTriggerAttribute result = (ServiceBusTriggerAttribute)builderInfo.Constructor.Invoke(builderInfo.ConstructorArgs); Assert.Equal(attribute.QueueName, result.QueueName); Assert.Equal(attribute.Access, result.Access); Assert.Null(result.TopicName); Assert.Null(result.SubscriptionName); Assert.Equal(0, builderInfo.Properties.Count); }
public void Constructor_Topic_SetsExpectedValues() { ServiceBusTriggerAttribute attribute = new ServiceBusTriggerAttribute("testtopic", "testsubscription"); Assert.Null(attribute.QueueName); Assert.Equal("testtopic", attribute.TopicName); Assert.Equal("testsubscription", attribute.SubscriptionName); Assert.Equal(AccessRights.Manage, attribute.Access); attribute = new ServiceBusTriggerAttribute("testtopic", "testsubscription", AccessRights.Listen); Assert.Null(attribute.QueueName); Assert.Equal("testtopic", attribute.TopicName); Assert.Equal("testsubscription", attribute.SubscriptionName); Assert.Equal(AccessRights.Listen, attribute.Access); }