在使用具有属性的预编译C#函数时,是否可以在Azure函数中具有多个输出绑定?
e.g。一个函数触发HTTP请求,该函数都响应HTTP响应和表存储
编辑:错误的目标,它是HTTP和Cosmos DB集合的文档
答案 0 :(得分:3)
这是一个带有两个输出绑定的函数的简单示例,使用最新的VS2017预览工具实现:
[FunctionName("MultipleOutBindings")]
public static HttpResponseMessage MultipleOutBindings(
[HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestMessage req,
[Queue("out-queue")] out string queueItem)
{
queueItem = "My new queue message";
return req.CreateResponse(HttpStatusCode.OK, "Hello");
}
答案 1 :(得分:2)
是的,以下代码段显示了此示例:
#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
using System.Net;
using Newtonsoft.Json;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Table;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, CloudTable table, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
// Set name to query string or body data
name = name ?? data?.name;
// insert to the table
table.ExecuteAsync(TableOperation.Insert(new Request {PartitionKey=name, RowKey=Guid.NewGuid().ToString(), Body = JsonConvert.SerializeObject(data) }));
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
public class Request : TableEntity
{
public string Body { get; set; }
}
和绑定:
{
"bindings": [
{
"authLevel": "function",
"name": "req",
"type": "httpTrigger",
"direction": "in"
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"name": "table",
"type": "table",
"connection": "myStorage",
"tableName": "myTable",
"direction": "out"
}
],
"disabled": false
}
答案 2 :(得分:1)
虽然完全可以在一个功能中执行此操作,但您可能还想查看Durable Functions Fan-In/Out mechanism。
最终会有多个小功能。每个人都做自己的事情。