Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 244
Sunil Purushothaman Avatar asked Jul 07 '17 02:07

Sunil Purushothaman


People also ask

Where can you define the trigger bindings for an azure function?

For triggers, the direction is always in. Input and output bindings use in and out. Some bindings support a special direction inout . If you use inout , only the Advanced editor is available via the Integrate tab in the portal.

What is input and output binding in Azure functions?

Binding is the connection to data within your Azure Functions. There are two types of Bindings. Input Binding: A input binding is the data that your function receives. Output Binding: A output binding is the data that your function sends.

How to specify output bindings of azure function from Visual Studio 2017?

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. My question is, how can I set these values from Visual studio ?

How do I create an azure function in Visual Studio 2017?

From the Visual Studio menu, select File > New > Project. In Create a new project, enter functions in the search box, choose the Azure Functions template, and then select Next.

What is the Azure Functions tool in Visual Studio 2019?

In Visual Studio 2019 and later, the Azure Functions tools extension is updated as part of Visual Studio. The Azure Functions project template in Visual Studio creates a C# class library project that you can publish to a function app in Azure.

How do I publish a Visual Studio project to Azure?

To publish a Function project to Azure directly from Visual Studio, right click the project and choose “Publish”. On the publish page, you can either create a new Function App in Azure or publish to an existing one. Note: even though the Folder option is currently appears, it’s not intended for use with Azure Functions at this time.


1 Answers

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.

like image 173
Fabio Cavalcante Avatar answered Sep 30 '22 18:09

Fabio Cavalcante