使用带有属性的预编译 C# 时,是否可以在 Azure 函数中进行多个输出绑定?
Is it possible to have multiple out bindings in an Azure function when using the pre-compiled C# with attributes?
使用带有属性的预编译 C# 函数时,是否可以在 Azure 函数中进行多个输出绑定?
例如一个函数在 HTTP 请求上触发,并且该函数既响应 HTTP 响应又响应 table 存储
编辑:目标错误,它是 HTTP 和 Cosmos DB 集合的文档
是的,下面的代码片段展示了这个例子:
#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
}
虽然完全可以在单个函数中执行此操作,但您可能还想查看 Durable Functions Fan-In/Out mechanism。
你最终会得到多个小的函数。各司其职。
这是一个带有两个输出绑定的函数的简单示例,使用最新的 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");
}
使用带有属性的预编译 C# 函数时,是否可以在 Azure 函数中进行多个输出绑定?
例如一个函数在 HTTP 请求上触发,并且该函数既响应 HTTP 响应又响应 table 存储
编辑:目标错误,它是 HTTP 和 Cosmos DB 集合的文档
是的,下面的代码片段展示了这个例子:
#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
}
虽然完全可以在单个函数中执行此操作,但您可能还想查看 Durable Functions Fan-In/Out mechanism。
你最终会得到多个小的函数。各司其职。
这是一个带有两个输出绑定的函数的简单示例,使用最新的 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");
}