Azure Service Bus Triggered Function - Bind to MessageReceiver - c#

I'm trying to bind to MessageReceiver in an Azure Service Bus Triggered Function.
My goal is to handle dead letter queue messages and complete them.
public static class Function1
{
[FunctionName("Function1")]
public static async Task Run(
[ServiceBusTrigger(
"<topicName>",
"<subscriptionName>/$DeadLetterQueue",
Connection = "connectionstring")]
Message message,
ILogger logger,
MessageReceiver messageReceiver)
{
// TODO: Perform some actions
await messageReceiver.CompleteAsync(message.SystemProperties.LockToken);
}
The problem is that it fails to bind to the MessageReceiver class.
Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'receiver' to type MessageReceiver. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
Any ideas why the binding fails?

I figured out what was wrong. I was using 'receiver' as parameter name for MessageReceiver. It turned out that the parameter name has to be 'messageReceiver'. The example I was looking at first used 'receiver', so is this maybe something that has changed?

Related

How to get message metadata in isolated Azure functions v4 - Queue Trigger in c#

I am following on a possible upgrade of azure function runtime from v3 to v4 with dotnet. While doing so, I am testing out the isolated option for the project. However I am unable to get message metadata such as DequeueCount, MessageId etc in the queue trigger.
Previously with in-process option, I used to bind CloudQueueMessage but that doesn't seem to work in the isolated mode. Doing so, throws and error -
Cannot convert input parameter 'myQueueItem' to type 'Microsoft.WindowsAzure.Storage.Queue.CloudQueueMessage' from type 'System.String'
This was my isolated queue function binding
[Function("TestApp")]
public void Run([QueueTrigger("sample-queue", Connection = "")] CloudQueueMessage myQueueItem, FunctionContext context)
After looking for a while, I think here it says that, in isolated process we can only bind string. Simple JSON - Object also works.
Is there any way to get these message metadata (members of the CloudQueueMessage) in the isolated azure function?
Thanks.
For DequeueCount property I use this:
[Function("MyQueueTrigger")]
public void Run([QueueTrigger("my-queue-name")] string myQueueItem, int dequeueCount)
{
_logger.LogInformation("Queue item: {item}. Dequeue count: {dequeueCount}.", myQueueItem, dequeueCount);
}

Azure Functions - get parameter value in middleware

I am running a v4 Azure Function in an isolated process. It is triggered by a message coming from a Service Bus queue. I have created a simple middleware and would like to get my hands on the incoming data (a simple string). How can I do that from the middleware itself? It doesn't seem FunctionContext is of use in this case.
public class SimpleMiddleware : IFunctionsWorkerMiddleware
{
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
await next(context);
}
}
The service bus message data and metadata is extracted and placed into the BindingData dictionary. Try looking at context.BindingContext.BindingData and find the key that exposes your message's data.

How to Mock IMessageReceiver?

Currently i have scenario where i need to unit test a Service Bus Trigger Function. Fa code as below
public async Task Run([ServiceBusTrigger("sample", Connection = "sample", IsSessionsEnabled = false)] Message message, IMessageReceiver messageReceiver, ILogger _log)
{
//Some code
}
I was primarily using MessageReceiver, but it's hard to unit test as it's not much flexible to Mock,so i switched to IMessageReceiver. Getting below Error
Microsoft.Azure.WebJobs.Host: Can't bind parameter 'messageReceiver' to type 'Microsoft.Azure.ServiceBus.Core.IMessageReceiver'.
NB:- It have a Weird issue that the variable name should be messageReceiver, while using MessageReceiver .
Is there anything that i need to follow for IMessageReceiver as well?
As of today, IMessageReceiver is not supported via dependency injection. You can only get MessageReceiver. You can upvote the request to add the support here.
Meanwhile, there's a workaround that you could use, showed here. The workaround is to have an additional, internal method, accepting IMessageReceiver and Function call that is injected MessageReceiver to pass the parameter to the internal method.

Azure function - how to pass custom input parameter

I have a problem with passing class object as an input parameter to azure function, what is the proper way of doing it? I have function like posted below.
public async Task<IActionResult> GetClinics(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "clinics")] HttpRequest req, ILogger log, PersonDocument thePerson)
{
//do something
}
Error that i got:
The 'GetClinics' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'GetClinics'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'thePerson' to type PersonDocument. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
what is the simplest way to solve it? Do i have to prepare my own custom binding? PersonDocument is just a class with some properties that i would like to extract from body. The only information that i found show how to add custom binding with custom attribute, but I am curious if it is really required to add them to solve such an easy problem?
Method signatures developed by the azure function C # class library can only include these:
ILogger or TraceWriter for logging (v1 version only)
A CancellationToken parameter for graceful shutdown
Mark input and output bindings by using attribute decoration
Binding expressions parameters to get trigger metadata
For your question, what you need seems a input binding, so have a look of this doc:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings#supported-bindings
You can see that only a few input bindings is support by azure function by default. So I think if you want to achieve what you want, custom binding is needed.

Cannot bind parameter 'orchestrationContext' to type DurableOrchestrationContext while using DurableOrchestration with Azure Functions

I am trying to use the new Durable Functions extension in Azure Functions I installed this Nuget package on my Function project:
Microsoft.Azure.WebJobs.Extensions.DurableTask
And then used the DurableOrchestrationContext in my function like that:
[FunctionName("StopVM")]
public static void StopVM([TimerTrigger("0 */2 * * * *")]TimerInfo myTimer, ILogger log, ExecutionContext context, DurableOrchestrationContext orchestrationContext)
{
....
}
but when i run the function this error shown:
Error indexing method 'FuncApp.StopVM' [20/11/2018
17:09:01] Microsoft.Azure.WebJobs.Host: Error indexing method
'FuncApp.StopVM'. Microsoft.Azure.WebJobs.Host: Cannot
bind parameter 'orchestrationContext' to type
DurableOrchestrationContext. Make sure the parameter Type is supported
by the binding. If you're using binding extensions (e.g. Azure
Storage, ServiceBus, Timers, etc.) make sure you've called the
registration method for the extension(s) in your startup code (e.g.
builder.AddAzureStorage(), builder.AddServiceBus(),
builder.AddTimers(), etc.).
Do i missing some steps like adding any middle-ware to startup class or etc. cause the documentation not shown clearly how to use it?
I got it. You should wrap your parameter of type DurableOrchestrationClient with this attribute[OrchestrationClient] if you want it to start the Orchestration itself or wrap the parameter of type DurableOrchestrationContext with this attribute [OrchestrationTrigger] to use the context and here there are more details (link)

Categories