如何从 Visual studio 2017 预览版 2 指定 Azure 函数的输出绑定?

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

在 Azure 门户中,可以从该函数的 'Integrate' 页面轻松配置 Azure 函数的输出绑定。 这些设置最终进入function.json。

我的问题是,如何从 Visual studio 设置这些值? 代码如下所示:

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);

    }
}

我想从 VS 指定要使用的队列和存储,以便我可以源代码控制我的代码和配置。我检查了类似的问题、建议的问题等,但 none 证明很方便。

我正在使用 Visual studio 2017 预览,版本 15.3.0 预览 3

VS 扩展:适用于 VS 的 Azure 函数工具,版本 0.2

绑定就像您的触发器一样指定,使用它们应该绑定到的参数的属性。绑定配置(例如队列名称、连接等)作为属性 parameters/properties.

提供

以您的代码为例,队列输出绑定如下所示:

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);

    }
}

如果您只是 return 从您的 HTTP 函数 (Ok) 获取 200,您可以通过将属性应用到方法的 return 值来进一步简化您的代码,再次以您的代码为例,它看起来像这样:

[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;
}

使用上面的代码,Azure Functions 会自动 return 一个 200 当你的函数成功并且一个 500 when/if 一个异常被抛出。