-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
ServiceBusTriggerAttribute.cs
153 lines (139 loc) · 5.69 KB
/
ServiceBusTriggerAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.WebJobs.Description;
using Microsoft.Azure.WebJobs.ServiceBus;
namespace Microsoft.Azure.WebJobs
{
/// <summary>
/// Attribute used to bind a parameter to a ServiceBus Queue message, causing the function to run when a
/// message is enqueued.
/// </summary>
/// <remarks>
/// The method parameter type can be one of the following:
/// <list type="bullet">
/// <item><description><see cref="ServiceBusReceivedMessage"/></description></item>
/// <item><description><see cref="string"/></description></item>
/// <item><description><see cref="byte"/>array</description></item>
/// <item><description><see cref="BinaryData"/></description></item>
/// <item><description>A user-defined type (serialized as JSON)</description></item>
/// </list>
/// </remarks>
[AttributeUsage(AttributeTargets.Parameter)]
[DebuggerDisplay("{DebuggerDisplay,nq}")]
[ConnectionProvider(typeof(ServiceBusAccountAttribute))]
[Binding]
public sealed class ServiceBusTriggerAttribute : Attribute, IConnectionProvider
{
private readonly string _queueName;
private readonly string _topicName;
private readonly string _subscriptionName;
private bool? _autoCompleteMessages;
private int? _maxMessageBatchSize;
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusTriggerAttribute"/> class.
/// </summary>
/// <param name="queueName">The name of the queue to which to bind.</param>
public ServiceBusTriggerAttribute(string queueName)
{
_queueName = queueName;
}
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusTriggerAttribute"/> class.
/// </summary>
/// <param name="topicName">The name of the topic to bind to.</param>
/// <param name="subscriptionName">The name of the subscription in <paramref name="topicName"/> to bind to.</param>
public ServiceBusTriggerAttribute(string topicName, string subscriptionName)
{
_topicName = topicName;
_subscriptionName = subscriptionName;
}
/// <summary>
/// Gets or sets the app setting name that contains the Service Bus connection string.
/// </summary>
public string Connection { get; set; }
/// <summary>
/// Gets the name of the queue to bind to.
/// </summary>
/// <remarks>When binding to a subscription in a topic, returns <see langword="null"/>.</remarks>
public string QueueName
{
get { return _queueName; }
}
/// <summary>
/// Gets the name of the topic to bind to.
/// </summary>
/// <remarks>When binding to a queue, returns <see langword="null"/>.</remarks>
public string TopicName
{
get { return _topicName; }
}
/// <summary>
/// Gets the name of the subscription in <see cref="TopicName"/> to bind to.
/// </summary>
/// <remarks>When binding to a queue, returns <see langword="null"/>.</remarks>
public string SubscriptionName
{
get { return _subscriptionName; }
}
/// <summary>
/// Gets or sets a value indicating whether sessions are enabled.
/// </summary>
public bool IsSessionsEnabled { get; set; }
/// <summary>
/// Gets or sets a value indicating whether trigger should automatically complete the message after successful processing. If not explicitly set, the behavior will be based on the <see cref="ServiceBusOptions.AutoCompleteMessages"/> value.
/// </summary>
public bool AutoCompleteMessages
{
get
{
return _autoCompleteMessages.HasValue ? _autoCompleteMessages.Value : true;
}
set
{
_autoCompleteMessages = value;
}
}
/// <summary>
/// Gets or sets the maximum number of messages that will be passed to each function call. This only applies for functions that receive
/// a batch of messages.
/// </summary>
public int MaxMessageBatchSize
{
get
{
return _maxMessageBatchSize.HasValue ? _maxMessageBatchSize.Value : 0;
}
set
{
_maxMessageBatchSize = value;
}
}
/// <summary>
/// Gets a boolean to check if auto complete messages option was set on the trigger.
/// Since a nullable property can't be used in the attribute. This is a work around for it.
/// </summary>
internal bool IsAutoCompleteMessagesOptionSet { get { return _autoCompleteMessages.HasValue; } }
/// <summary>
/// Gets a boolean to check if maximum message batch option was set on the trigger.
/// Since a nullable property can't be used in the attribute. This is a work around for it.
/// </summary>
internal bool IsMaxMessageBatchSizeOptionSet { get { return _maxMessageBatchSize.HasValue && _maxMessageBatchSize != 0; } }
private string DebuggerDisplay
{
get
{
if (_queueName != null)
{
return _queueName;
}
else
{
return _topicName + "/" + _subscriptionName;
}
}
}
}
}