Azure / azure-functions-servicebus-extension

Service Bus extension for Azure Functions
MIT License
65 stars 36 forks source link

Azure Function overload for Service Bus #210

Closed hgjura closed 3 months ago

hgjura commented 5 years ago

When creating an Azure Function with an Azure Service Bus trigger, something interesting happens. As a developer, you have access to the message, but you don't have the freedom to do anything with that message, like abandon it early, etc. because you don't have access to the topic or queue client. In the old SDK all you needed was access to the BrokeredMessage to manipulate the message, but with the new sdk you need access to the client to do anything.

Perhaps and oveloaded Run() with the Message and the accompanying Client object would be the answer to this. Because, as it is now it is partially useful. I also heard (not verified) that in case of error, the Functions platform tries 10 times before abandoning the message. Imagine the strain it add unnecessarily to the Service Bus, when the user knows on the first try that the message needs abandoning, by needlessly trying 9 more times!

Thanks

batizar commented 5 years ago

@hgjura I saw in another thread, in v3 you are able to do that. Check it out here, hope it helps with your issue.

https://github.com/Azure/azure-webjobs-sdk/issues/1986#issuecomment-433960534

Not really sure what is going on with the support here though as no documentations or samples has been updated for v3 even though it was released many month ago! Most of the questions are left unanswered and unresolved as well.

ncapito commented 5 years ago

@batizar i've tried what was mentioned in that post you reference and it fails with an error "Can't bind parameter 'messageReceiver' to type 'Microsoft.ServiceBus.Messaging.MessageReceiver'."

My use case is there are some messages I don't want to retry, and i want to send them to the DLQ (for future auditing)

batizar commented 5 years ago

@ncapito I have done the same and it works for me, do you have AddServiceBus in your ConfigureWebJobs? Here is my partial code:

.ConfigureWebJobs(b =>
{
    b.AddAzureStorageCoreServices();
    b.AddServiceBus(options =>
    {
        options.PrefetchCount = Configuration.GetValue<int>("extensions:serviceBus:prefetchCount");
        options.ConnectionString = Configuration.GetValue<string>("ConnectionStrings:AzureWebJobsServiceBus");
        options.MessageHandlerOptions.AutoComplete = Configuration.GetValue<bool>("extensions:serviceBus:messageHandlerOptions:autoComplete");
        options.MessageHandlerOptions.MaxConcurrentCalls = Configuration.GetValue<int>("extensions:serviceBus:messageHandlerOptions:maxConcurrentCalls");
        options.MessageHandlerOptions.MaxAutoRenewDuration = Configuration.GetValue<TimeSpan>("extensions:serviceBus:messageHandlerOptions:maxAutoRenewDuration");
    });
})
using Microsoft.Azure.ServiceBus;
using Microsoft.Azure.ServiceBus.Core;
...

public async Task ProcessServiceBusMessageAsync([ServiceBusTrigger("%AppConfig:ServiceBusTopic%", "%AppConfig:ServiceBusSubscription%")] Message message, MessageReceiver messageReceiver)
{
    ...
    await messageReceiver.DeadLetterAsync(message.SystemProperties.LockToken, properties);
    ...
}
ncapito commented 5 years ago

I needed to set "StorageConnectionString" and a couple of others (DashboardConnectionString and AzureWebJobsStorage). I eventually pulled it off using

 .ConfigureAppConfiguration((context, config) => {
      config.AddInMemoryCollection(new Dictionary<string, string>() {
         { "AzureWebJobsStorage",SecureSettings.StorageConnectionString },
         { "StorageConnectionString",SecureSettings.StorageConnectionString },
         { "DashboardConnectionString",SecureSettings.StorageConnectionString 
      }, });
  })

As for the message receiver stuff every time i tried to inject MessageReceiver it failed to start the job. I finally just changed the queue settings to retry 1 time and after that i just throw an exception to force it to automatically DLQ.

batizar commented 5 years ago

@ncapito do you have b.AddServiceBus() in your ConfigureWebJobs section?