Konzy262
Konzy262

Reputation: 3117

How can you programmatically trigger an azure function to run in C# code?

I have an integration test to write that involves manually running a function, equivalent to pressing this button in Azure on a timer trigger...

enter image description here

Can this be done programmatically in C# from a separate code base to the function code itself? At the moment I don't have access to the function code but do have access to azure.

Alternatively, can I update the schedule from code so that it triggers immediately?

So far I've struggled to find a way to do this via some sort of sdk, api call or powershell script. Hopefully I'm missing something obvious

Many thanks,

Upvotes: 1

Views: 4245

Answers (2)

Konzy262
Konzy262

Reputation: 3117

I found exactly what I was looking for - https://learn.microsoft.com/en-us/azure/azure-functions/functions-manually-run-non-http

This enables me to manually trigger non HTTP functions (e.g. a timer trigger) in Azure programmatically via an api call.

Upvotes: 2

Albondi
Albondi

Reputation: 1151

You can create an HTTP trigger and invoke it from c# code.

Example code:

[FunctionName("HttpTriggerCSharp")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, 
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    // parse query parameter
    string name = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
        .Value;

    // Get request body
    dynamic data = await req.Content.ReadAsAsync<object>();

    // Set name to query string or body data
    name = name ?? data?.name;

    return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}

Upvotes: 3

Related Questions