c# - 如何从 Visual studio 2017 预览版 2 指定 Azure Function 的输出绑定(bind)?

标签 c# azure visual-studio-2017 azure-functions

在 Azure 门户中,可以从 Azure 函数的“集成”页面轻松配置该函数的输出绑定(bind)。 这些设置最终进入function.json。

Specifying output bindings from Azure portal

我的问题是,如何从 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 指定要使用的队列和存储,以便我可以对我的代码和配置进行源控制。我检查过类似的问题、建议的问题等,但没有一个被证明是方便的。

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

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

最佳答案

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

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

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

    }
}

如果您只是从 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;
}

使用上面的代码,当函数成功时,Azure Functions 将自动返回 200,当/如果引发异常时,Azure Functions 将自动返回 500。

关于c# - 如何从 Visual studio 2017 预览版 2 指定 Azure Function 的输出绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44961482/

相关文章:

c - 为什么在此示例中我会收到带有 argv 的崩溃报告?

c# - 如何编写等待 selenium webElement 的通用方法

c# - 将十进制值格式化为带前导空格的字符串

c# - claim 类型不正确

azure - fatal error Visual Studio 2017 - Azure Functions

c# - 无效的 Intellisense 错误 CS0103 ASP.NET 网站 “The name does not exist in the current context”

c# - 如果捕获所有异常,是否需要在 try catch block 中使用 finally?

azure - 如何在 Azure Pipelines Pull 请求中重新部署版本?

linux - 将 JDK 安装到 Azure Batch 中池的计算节点

css - 向新的 API 管理门户添加表格样式