1+ // Copyright (c) .NET Foundation. All rights reserved.
2+ // Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+ using Azure . Messaging . ServiceBus ;
5+ using Microsoft . Azure . Functions . Worker ;
6+ using Microsoft . Extensions . Logging ;
7+
8+ namespace SampleApp
9+ {
10+ /// <summary>
11+ /// Samples demonstrating binding to the <see cref="ServiceBusReceivedMessage"/> type.
12+ /// </summary>
13+ public class ServiceBusReceivedMessageBindingSamples
14+ {
15+ private readonly ILogger < ServiceBusReceivedMessageBindingSamples > _logger ;
16+
17+ public ServiceBusReceivedMessageBindingSamples ( ILogger < ServiceBusReceivedMessageBindingSamples > logger )
18+ {
19+ _logger = logger ;
20+ }
21+
22+ /// <summary>
23+ /// This function demonstrates binding to a single <see cref="ServiceBusReceivedMessage"/>.
24+ /// </summary>
25+ [ Function ( nameof ( ServiceBusReceivedMessageFunction ) ) ]
26+ public void ServiceBusReceivedMessageFunction (
27+ [ ServiceBusTrigger ( "queue" , Connection = "ServiceBusConnection" ) ] ServiceBusReceivedMessage message )
28+ {
29+ _logger . LogInformation ( "Message ID: {id}" , message . MessageId ) ;
30+ _logger . LogInformation ( "Message Body: {body}" , message . Body ) ;
31+ _logger . LogInformation ( "Message Content-Type: {contentType}" , message . ContentType ) ;
32+ }
33+
34+ /// <summary>
35+ /// This function demonstrates binding to an array of <see cref="ServiceBusReceivedMessage"/>.
36+ /// Note that when doing so, you must also set the <see cref="ServiceBusTriggerAttribute.IsBatched"/> property
37+ /// to <value>true</value>.
38+ /// </summary>
39+ [ Function ( nameof ( ServiceBusReceivedMessageBatchFunction ) ) ]
40+ public void ServiceBusReceivedMessageBatchFunction (
41+ [ ServiceBusTrigger ( "queue" , Connection = "ServiceBusConnection" , IsBatched = true ) ] ServiceBusReceivedMessage [ ] messages )
42+ {
43+ foreach ( ServiceBusReceivedMessage message in messages )
44+ {
45+ _logger . LogInformation ( "Message ID: {id}" , message . MessageId ) ;
46+ _logger . LogInformation ( "Message Body: {body}" , message . Body ) ;
47+ _logger . LogInformation ( "Message Content-Type: {contentType}" , message . ContentType ) ;
48+ }
49+ }
50+
51+ /// <summary>
52+ /// This functions demonstrates that it is possible to bind to both the ServiceBusReceivedMessage and any of the supported binding contract
53+ /// properties at the same time. If attempting this, the ServiceBusReceivedMessage must be the first parameter. There is not
54+ /// much benefit to doing this as all of the binding contract properties are available as properties on the ServiceBusReceivedMessage.
55+ /// </summary>
56+ [ Function ( nameof ( ServiceBusReceivedMessageWithStringProperties ) ) ]
57+ public void ServiceBusReceivedMessageWithStringProperties (
58+ [ ServiceBusTrigger ( "queue" , Connection = "ServiceBusConnection" ) ]
59+ ServiceBusReceivedMessage message , string messageId , int deliveryCount )
60+ {
61+ // The MessageId property and the messageId parameter are the same.
62+ _logger . LogInformation ( "Message ID: {id}" , message . MessageId ) ;
63+ _logger . LogInformation ( "Message ID: {id}" , messageId ) ;
64+
65+ // Similarly the DeliveryCount property and the deliveryCount parameter are the same.
66+ _logger . LogInformation ( "Delivery Count: {count}" , message . DeliveryCount ) ;
67+ _logger . LogInformation ( "Delivery Count: {count}" , deliveryCount ) ;
68+ }
69+ }
70+ }
0 commit comments