Toke Breer-Mortensen
Toke Breer-Mortensen

Reputation: 287

Which "automatic parameters" (DI) are available in an Azure v1 function?

Which other "automatically resolved" parameters (if any) are available in a v1 Azure function other than ILogger (or tracewriter) and where to find documentation on them ?

Req is resolved/inserted by azure at runtime, so is log and context. Are there more? Off course, the trigger specific parameter(s) are obvious, but are there more ?(execution context was found by accident in another post :/ )

    [FunctionName("DoStuff")]
    public static async Task<HttpResponseMessage> DoMyStuff(
        [HttpTrigger( AuthorizationLevel.Function, "get", "post", Route = null)]
        HttpRequestMessage req,
        ILogger log, //Could also be a Tracewriter
        ExecutionContext context
        )
    {
        log.LogInformation($"Executing inside: {context.FunctionDirectory}");
        return req.CreateResponse(HttpStatusCode.OK, 999999);
    }

Upvotes: 0

Views: 61

Answers (1)

DixitArora-MSFT
DixitArora-MSFT

Reputation: 1811

A CancellationToken parameter for graceful shutdown.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#cancellation-tokens

public static class CancellationTokenExample
{
    public static void Run(
        [QueueTrigger("inputqueue")] string inputText,
        TextWriter logger,
        CancellationToken token)
    {
        for (int i = 0; i < 100; i++)
        {
            if (token.IsCancellationRequested)
            {
                logger.WriteLine("Function was cancelled at iteration {0}", i);
                break;
            }
            Thread.Sleep(5000);
            logger.WriteLine("Normal processing for queue message={0}", inputText);
        }
    }
}

ICollector or IAsyncCollector types.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#writing-multiple-output-values

public static class ICollectorExample
{
    [FunctionName("CopyQueueMessageICollector")]
    public static void Run(
        [QueueTrigger("myqueue-items-source-3")] string myQueueItem,
        [Queue("myqueue-items-destination")] ICollector<string> myDestinationQueue,
        ILogger log)
    {
        log.LogInformation($"C# function processed: {myQueueItem}");
        myDestinationQueue.Add($"Copy 1: {myQueueItem}");
        myDestinationQueue.Add($"Copy 2: {myQueueItem}");
    }
}

For more informations follow the doc.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#method-signature-parameters

Upvotes: 1

Related Questions