-
Notifications
You must be signed in to change notification settings - Fork 201
Add ability to bind to SBReceivedMessage #1313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
liliankasem
merged 16 commits into
Azure:feature/sdk-type-binding
from
JoshLove-msft:sb-received-message
May 22, 2023
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2647f1f
Add ability to bind to SBReceivedMessage
JoshLove-msft f00735c
Add assembly attribute
JoshLove-msft 948e4ae
Merge branch 'main' of https://github.com/Azure/azure-functions-dotne…
JoshLove-msft 5478413
PR fb
JoshLove-msft 2a41aed
save
JoshLove-msft 557d6fd
Merge branch 'feature/sdk-type-binding' of https://github.com/Azure/a…
JoshLove-msft a552f69
Use new attributes
JoshLove-msft ab0d1ee
Remove unintended changes and get ready for preview
JoshLove-msft 28147e5
Add sample
JoshLove-msft 0b2a210
Advertise more collection types
JoshLove-msft a38cd49
Add samples and remove IEnumerable support since it is not supported …
JoshLove-msft 3385c32
PR fb
JoshLove-msft b0bc62d
Merge branch 'feature/sdk-type-binding' of https://github.com/Azure/a…
JoshLove-msft 54efd0e
PR fb
JoshLove-msft 41f51ba
fix
JoshLove-msft 7e57462
Remove unnecessary registration
JoshLove-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| ## Release notes | ||
| <!-- Please add your release notes in the following format: | ||
| - My change description (#PR/#issue) | ||
| --> | ||
| --> | ||
|
|
||
| - Support binding to ServiceBusReceivedMessage (#1313) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
|
|
||
| namespace Microsoft.Azure.Functions.Worker.Extensions.ServiceBus | ||
| { | ||
| public static class Constants | ||
| { | ||
| internal const string BinaryContentType = "application/octet-stream"; | ||
| } | ||
| } |
7 changes: 5 additions & 2 deletions
7
extensions/Worker.Extensions.ServiceBus/src/Properties/AssemblyInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using Microsoft.Azure.Functions.Worker.Extensions.Abstractions; | ||
| using System.Runtime.CompilerServices; | ||
| using Microsoft.Azure.Functions.Worker.Extensions.Abstractions; | ||
|
|
||
| [assembly: ExtensionInformation("Microsoft.Azure.WebJobs.Extensions.ServiceBus", "5.10.0")] | ||
| [assembly: InternalsVisibleTo("Microsoft.Azure.Functions.WorkerExtension.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001005148be37ac1d9f58bd40a2e472c9d380d635b6048278f7d47480b08c928858f0f7fe17a6e4ce98da0e7a7f0b8c308aecd9e9b02d7e9680a5b5b75ac7773cec096fbbc64aebd429e77cb5f89a569a79b28e9c76426783f624b6b70327eb37341eb498a2c3918af97c4860db6cdca4732787150841e395a29cfacb959c1fd971c1")] | ||
|
|
||
| [assembly: ExtensionInformation("Microsoft.Azure.WebJobs.Extensions.ServiceBus", "5.7.0")] |
32 changes: 32 additions & 0 deletions
32
extensions/Worker.Extensions.ServiceBus/src/ServiceBusExtensionStartup.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // 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 Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Azure.Functions.Worker.Core; | ||
| using Microsoft.Extensions.Azure; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| [assembly: WorkerExtensionStartup(typeof(ServiceBusExtensionStartup))] | ||
|
|
||
| namespace Microsoft.Azure.Functions.Worker | ||
| { | ||
| public class ServiceBusExtensionStartup : WorkerExtensionStartup | ||
| { | ||
| public override void Configure(IFunctionsWorkerApplicationBuilder applicationBuilder) | ||
| { | ||
| if (applicationBuilder == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(applicationBuilder)); | ||
| } | ||
|
|
||
| applicationBuilder.Services.AddAzureClientsCore(); // Adds AzureComponentFactory | ||
|
|
||
| applicationBuilder.Services.Configure<WorkerOptions>((workerOption) => | ||
| { | ||
| workerOption.InputConverters.RegisterAt<ServiceBusReceivedMessageConverter>(0); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
extensions/Worker.Extensions.ServiceBus/src/ServiceBusReceivedMessageConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // 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.Linq; | ||
| using System.Threading.Tasks; | ||
| using Azure.Core.Amqp; | ||
| using Azure.Messaging.ServiceBus; | ||
| using Microsoft.Azure.Functions.Worker.Converters; | ||
| using Microsoft.Azure.Functions.Worker.Core; | ||
| using Microsoft.Azure.Functions.Worker.Extensions.Abstractions; | ||
| using Microsoft.Azure.Functions.Worker.Extensions.ServiceBus; | ||
|
|
||
| namespace Microsoft.Azure.Functions.Worker | ||
| { | ||
|
|
||
| [SupportsDeferredBinding] | ||
| [SupportedConverterType(typeof(ServiceBusReceivedMessage))] | ||
| [SupportedConverterType(typeof(ServiceBusReceivedMessage[]))] | ||
| internal class ServiceBusReceivedMessageConverter : IInputConverter | ||
| { | ||
| public ValueTask<ConversionResult> ConvertAsync(ConverterContext context) | ||
| { | ||
| ConversionResult result = context?.Source switch | ||
| { | ||
| ModelBindingData binding => ConversionResult.Success(ConvertToServiceBusReceivedMessage(binding)), | ||
| // Only array collections are currently supported, which matches the behavior of the in-proc extension. | ||
| CollectionModelBindingData collection => ConversionResult.Success(collection.ModelBindingDataArray | ||
| .Select(ConvertToServiceBusReceivedMessage).ToArray()), | ||
| _ => ConversionResult.Unhandled() | ||
| }; | ||
| return new ValueTask<ConversionResult>(result); | ||
| } | ||
|
|
||
| private ServiceBusReceivedMessage ConvertToServiceBusReceivedMessage(ModelBindingData binding) | ||
| { | ||
| // The lock token is a 16 byte GUID | ||
| const int lockTokenLength = 16; | ||
|
|
||
| if (binding.ContentType != Constants.BinaryContentType) | ||
| { | ||
| throw new InvalidOperationException( | ||
| $"Unexpected content-type. Only '{Constants.BinaryContentType}' is supported."); | ||
| } | ||
|
|
||
| ReadOnlyMemory<byte> bytes = binding.Content.ToMemory(); | ||
| ReadOnlyMemory<byte> lockTokenBytes = bytes.Slice(0, lockTokenLength); | ||
| ReadOnlyMemory<byte> messageBytes = bytes.Slice(lockTokenLength, bytes.Length - lockTokenLength); | ||
| return ServiceBusReceivedMessage.FromAmqpMessage(AmqpAnnotatedMessage.FromBytes(BinaryData.FromBytes(messageBytes)), | ||
| BinaryData.FromBytes(lockTokenBytes)); | ||
| } | ||
| } | ||
JoshLove-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
7 changes: 6 additions & 1 deletion
7
extensions/Worker.Extensions.ServiceBus/src/ServiceBusTriggerAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
samples/WorkerBindingSamples/ServiceBus/ServiceBusReceivedMessageBindingSamples.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using Azure.Messaging.ServiceBus; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace SampleApp | ||
| { | ||
| /// <summary> | ||
| /// Samples demonstrating binding to the <see cref="ServiceBusReceivedMessage"/> type. | ||
| /// </summary> | ||
| public class ServiceBusReceivedMessageBindingSamples | ||
| { | ||
| private readonly ILogger<ServiceBusReceivedMessageBindingSamples> _logger; | ||
|
|
||
| public ServiceBusReceivedMessageBindingSamples(ILogger<ServiceBusReceivedMessageBindingSamples> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This function demonstrates binding to a single <see cref="ServiceBusReceivedMessage"/>. | ||
| /// </summary> | ||
| [Function(nameof(ServiceBusReceivedMessageFunction))] | ||
| public void ServiceBusReceivedMessageFunction( | ||
| [ServiceBusTrigger("queue", Connection = "ServiceBusConnection")] ServiceBusReceivedMessage message) | ||
| { | ||
| _logger.LogInformation("Message ID: {id}", message.MessageId); | ||
| _logger.LogInformation("Message Body: {body}", message.Body); | ||
| _logger.LogInformation("Message Content-Type: {contentType}", message.ContentType); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This function demonstrates binding to an array of <see cref="ServiceBusReceivedMessage"/>. | ||
| /// Note that when doing so, you must also set the <see cref="ServiceBusTriggerAttribute.IsBatched"/> property | ||
| /// to <value>true</value>. | ||
| /// </summary> | ||
| [Function(nameof(ServiceBusReceivedMessageBatchFunction))] | ||
| public void ServiceBusReceivedMessageBatchFunction( | ||
| [ServiceBusTrigger("queue", Connection = "ServiceBusConnection", IsBatched = true)] ServiceBusReceivedMessage[] messages) | ||
| { | ||
| foreach (ServiceBusReceivedMessage message in messages) | ||
| { | ||
| _logger.LogInformation("Message ID: {id}", message.MessageId); | ||
| _logger.LogInformation("Message Body: {body}", message.Body); | ||
| _logger.LogInformation("Message Content-Type: {contentType}", message.ContentType); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This functions demonstrates that it is possible to bind to both the ServiceBusReceivedMessage and any of the supported binding contract | ||
| /// properties at the same time. If attempting this, the ServiceBusReceivedMessage must be the first parameter. There is not | ||
| /// much benefit to doing this as all of the binding contract properties are available as properties on the ServiceBusReceivedMessage. | ||
| /// </summary> | ||
| [Function(nameof(ServiceBusReceivedMessageWithStringProperties))] | ||
| public void ServiceBusReceivedMessageWithStringProperties( | ||
| [ServiceBusTrigger("queue", Connection = "ServiceBusConnection")] | ||
| ServiceBusReceivedMessage message, string messageId, int deliveryCount) | ||
| { | ||
| // The MessageId property and the messageId parameter are the same. | ||
| _logger.LogInformation("Message ID: {id}", message.MessageId); | ||
| _logger.LogInformation("Message ID: {id}", messageId); | ||
|
|
||
JoshLove-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Similarly the DeliveryCount property and the deliveryCount parameter are the same. | ||
| _logger.LogInformation("Delivery Count: {count}", message.DeliveryCount); | ||
| _logger.LogInformation("Delivery Count: {count}", deliveryCount); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.