如何从Visual Studio 2017预览版2指定Azure功能的输出绑定?

Sun*_*man 10 c# azure azure-functions visual-studio-2017

在Azure门户中,可以从该功能的"集成"页面轻松配置Azure功能的输出绑定.这些设置最终进入function.json.

从Azure门户指定输出绑定

我的问题是,如何从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);

    }
}
Run Code Online (Sandbox Code Playgroud)

我想指定从VS使用哪个队列和哪个存储,以便我可以控制我的代码和配置.我已经检查了类似的问题,建议的问题等,但没有一个被证明是方便的.

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

VS Extension:VS的Azure函数工具,版本0.2

Fab*_*nte 14

绑定被指定为触发器,使用它们应绑定的参数的属性.绑定配置(例如队列名称,连接等)作为属性参数/属性提供.

使用您的代码作为示例,队列输出绑定将如下所示:

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

    }
}
Run Code Online (Sandbox Code Playgroud)

如果您只是从HTTP函数返回200(Ok),则可以通过将该属性应用于方法的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;
}
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,当您的函数成功时,Azure Functions将自动返回200,如果抛出异常,则返回500.