Azure function - how to pass custom input parameter - c#

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.

Related

How to handle build events/progress using Microsoft.Build.Evaluation.Project?

I need a way to send build events/progress during the build process.
All I have is roughly:
var proj = Microsoft.Build.Evaluation.Project(csprojDir);
var consoleLogger = new Microsoft.Build.Logging.ConsoleLogger();
proj.Build(consoleLogger);
But I can't find a way to handle events (e.g. some Action or delegate of any kind that receives events).
Any idea how catch those?
Write your own custom logger and pass an instance of your logger to the Build method. There are overloads of Build that accept an IEnumerable<ILogger> so you can pass both the consoleLogger and your custom logger.
ILogger defines an Initialize(IEventSource) method. IEventSource has the events you are probably looking for.
There is example code (the same example) in both the IEventSource Interface and ILogger Interface documentation.

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.

Can QueueTrigger function have response as controller

is it possible to have from QueueTrigger function response as controller? When I call a get method from controller response body could be something like
{
"error": {
"message": "The resource does not exist",
...
Is there a way to do similar response from the function? I found only output binding where it can return string to another queue, but this is not what I am looking for:
[FunctionName("QueueOutput")]
[return: Queue("myqueue-items")]
public static string Run([HttpTrigger] dynamic input, ILogger log)
{
...
}
If I do this:
[FunctionName("QueueOutput")]
public static Task<string> Run([HttpTrigger] dynamic input, ILogger log)
{
...
return "...";
}
I have following error:
The 'CleanDataFunction' function is in error:
Microsoft.Azure.WebJobs.Host: Error indexing method 'CleanDataFunction'.
Microsoft.Azure.WebJobs.Host: Cannot bind parameter '$return' to type
String&. 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.).
Probably it is not possible. Found answer here:
Azure Function doesn't work with QueueTrigger and non-void return value?
You could simply create the json you need, either manually as a string or using JSON.NET, and return it serialized.

Azure Service Bus Triggered Function - Bind to MessageReceiver

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?

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