Skip to content

Commit 42ce7cd

Browse files
Add sessions example to Service Bus Extension README (Azure#27657)
* Add sessions example to Service Bus Extension README * PR fb
1 parent 330958b commit 42ce7cd

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,35 @@ public static async Task Run(
201201
}
202202
```
203203

204+
### Session triggers
205+
206+
To receive messages from a session enabled queue or topic, you can set the `IsSessionsEnabled`
207+
property on the `ServiceBusTrigger` attribute. When working with sessions, you can bind to the `SessionMessageActions` to get access to the message settlement methods in addition to session-specific functionality.
208+
209+
```C# Snippet:ServiceBusBindingToSessionMessageActions
210+
[FunctionName("BindingToSessionMessageActions")]
211+
public static async Task Run(
212+
[ServiceBusTrigger("<queue_name>", Connection = "<connection_name>", IsSessionsEnabled = true)]
213+
ServiceBusReceivedMessage[] messages,
214+
ServiceBusSessionMessageActions sessionActions)
215+
{
216+
foreach (ServiceBusReceivedMessage message in messages)
217+
{
218+
if (message.MessageId == "1")
219+
{
220+
await sessionActions.DeadLetterMessageAsync(message);
221+
}
222+
else
223+
{
224+
await sessionActions.CompleteMessageAsync(message);
225+
}
226+
}
227+
228+
// We can also perform session-specific operations using the actions, such as setting state that is specific to this session.
229+
await sessionActions.SetSessionStateAsync(new BinaryData("<session state>"));
230+
}
231+
```
232+
204233
### Binding to ServiceBusClient
205234

206235
There may be times when you want to bind to the same `ServiceBusClient` that the trigger is using. This can be useful if you need to dynamically create a sender based on the message that is received.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using Azure.Messaging.ServiceBus;
6+
using Microsoft.Azure.WebJobs.ServiceBus;
7+
using System.Threading.Tasks;
8+
9+
namespace Microsoft.Azure.WebJobs.Extensions.ServiceBus.Tests.Samples
10+
{
11+
public static class BindingToSessionMessageActions
12+
{
13+
#region Snippet:ServiceBusBindingToSessionMessageActions
14+
[FunctionName("BindingToSessionMessageActions")]
15+
public static async Task Run(
16+
[ServiceBusTrigger("<queue_name>", Connection = "<connection_name>", IsSessionsEnabled = true)]
17+
ServiceBusReceivedMessage[] messages,
18+
ServiceBusSessionMessageActions sessionActions)
19+
{
20+
foreach (ServiceBusReceivedMessage message in messages)
21+
{
22+
if (message.MessageId == "1")
23+
{
24+
await sessionActions.DeadLetterMessageAsync(message);
25+
}
26+
else
27+
{
28+
await sessionActions.CompleteMessageAsync(message);
29+
}
30+
}
31+
32+
// We can also perform session-specific operations using the actions, such as setting state that is specific to this session.
33+
await sessionActions.SetSessionStateAsync(new BinaryData("<session state>"));
34+
}
35+
#endregion
36+
}
37+
}

0 commit comments

Comments
 (0)