在 Azure 门户中,可以从 Azure 函数的“集成”页面轻松配置该函数的输出绑定(bind)。 这些设置最终进入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 指定要使用的队列和存储,以便我可以对我的代码和配置进行源控制。我检查过类似的问题、建议的问题等,但没有一个被证明是方便的。
我正在使用 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/