Sunil Purushothaman
Sunil Purushothaman

Reputation: 9521

How to specify output bindings of Azure Function from Visual studio 2017 preview 2?

In Azure portal, one can easily configure the output bindings of an Azure function, from the 'Integrate' page of that function. These settings Eventually go into the function.json.

Specifying output bindings from Azure portal

My question is, how can I set these values from Visual studio ? The code looks like this:

public static class SomeEventProcessor
{
    [FunctionName("SomeEventProcessor")]

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

        EventInfo eventInfo = new EventInfo(); //Just a container
        eventInfo.SomeID = req.Headers.Contains("SomeID") ? req.Headers.GetValues("SomeID").First() : null;

        //Write to a queue and promptly return
        await outputQueue.AddAsync(eventInfo);

        return req.CreateResponse(HttpStatusCode.OK);

    }
}

I want to specify which queue and which storage to use, from VS, so that I can source control my code and config. I have checked out similar questions, suggested questions etc, but none proved handy.

I am using Visual studio 2017 preview, Version 15.3.0 Preview 3

VS Extension: Azure Function tools for VS, version 0.2

Upvotes: 11

Views: 5994

Answers (1)

Fabio Cavalcante
Fabio Cavalcante

Reputation: 12538

The bindings are specified just as your trigger, using attributes on the parameters they should be bound to. The binding configuration (e.g. the queue name, connection, etc.) is provided as attribute parameters/properties.

Using your code as an example, a queue output binding would look like this:

public static class SomeEventProcessor
{
    [FunctionName("SomeEventProcessor")]

    public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post")]HttpRequestMessage req,
        TraceWriter log,
        [Queue("myQueueName", Connection = "myconnection")] IAsyncCollector<EventInfo> outputQueue)
    {
        log.Info("C# HTTP trigger function processed a request.");

        EventInfo eventInfo = new EventInfo(); //Just a container
        eventInfo.SomeID = req.Headers.Contains("SomeID") ? req.Headers.GetValues("SomeID").First() : null;

        //Write to a queue and promptly return
        await outputQueue.AddAsync(eventInfo);

        return req.CreateResponse(HttpStatusCode.OK);

    }
}

If you're just returning a 200 from your HTTP function (Ok), you can furtner simplify your code by applying the attribute to the method's return value, which, again using your code as an example, would look like this:

[FunctionName("SomeEventProcessor")]
[return: Queue("myQueueName", Connection = "myconnection")]
public static EventInfo Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post")]HttpRequestMessage req,
    TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    EventInfo eventInfo = new EventInfo(); //Just a container
    eventInfo.SomeID = req.Headers.Contains("SomeID") ? req.Headers.GetValues("SomeID").First() : null;

    return eventInfo;
}

Using the code above, Azure Functions will automatically return a 200 when your functions succeeds and a 500 when/if an exception is thrown.

Upvotes: 15

Related Questions