你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
创建函数时,会在项目中从一组触发器模板中添加特定于编程语言的触发器代码。 如果要使用输入或输出绑定将函数连接到其他服务,则必须在函数中添加特定的绑定定义。 若要了解有关绑定的详细信息,请参阅 Azure Functions 触发器和绑定概念。
本地开发
在本地开发函数时,需要更新函数代码以添加绑定。 对于使用 function.json的语言,Visual Studio Code提供了向函数添加绑定的工具。
基于示例手动添加绑定
向现有函数添加绑定时,需要向代码中的函数定义添加特定于绑定的属性。
向现有函数添加绑定时,需要向代码中的函数定义添加特定于绑定的批注。
将绑定添加到现有函数时,需要更新函数代码并将定义添加到 function.json 配置文件。
在 Go 中,你可以在 main() 函数中使用流畅式注册 API 来配置受支持的触发器。 每个触发器类型都有一个专用的注册方法,其中包含用于配置的功能选项。 不需要单独的绑定配置文件。
以下示例显示了 HTTP 触发的函数。 如果你需要从 Go 函数向 Azure 队列存储写入,请直接使用适用于 Go 的 Azure SDK,因为 Go 工作器当前不支持队列存储输出绑定:
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/azure/azure-functions-golang-worker/sdk"
"github.com/azure/azure-functions-golang-worker/worker"
)
func main() {
app := sdk.FunctionApp()
app.HTTP("HttpExample", httpHandler,
sdk.WithMethods("GET", "POST"),
sdk.WithAuth("anonymous"),
)
worker.Start(app)
}
func httpHandler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
if name == "" {
var body struct{ Name string }
json.NewDecoder(r.Body).Decode(&body)
name = body.Name
}
if name == "" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Please pass a name on the query string or in the request body.")
return
}
// Queue output bindings are not yet supported in the Go worker.
// Use the Azure SDK for Go to write to Queue Storage directly.
fmt.Fprintf(w, "Hello, %s!", name)
}
注释
Go 工作程序目前仅支持触发器。 输入和输出绑定(如队列存储输出绑定)尚不可用。 使用 Azure SDK for Go直接从函数代码与其他Azure服务进行交互。
Go 工作器当前支持以下触发器类型:
| 触发器类型 | 注册方法 | 例子 |
|---|---|---|
| HTTP | app.HTTP() |
HTTP 示例 |
| 定时器 | app.Timer() |
计时器示例 |
| Azure Cosmos DB | app.CosmosDB() |
Cosmos DB 示例 |
| Azure 服务总线 (Queue) | app.ServiceBusQueue() |
服务总线队列示例 |
| Azure 服务总线 (主题) | app.ServiceBusTopic() |
服务总线主题示例 |
| 事件中心 | app.EventHub() |
事件中心示例 |
| 事件网格 | app.EventGrid() |
事件网格示例 |
| Blob 存储 | app.Blob() |
Blob 示例 |
有关详细信息,请参阅 Go 开发人员参考。
以下示例展示在
由于 HTTP 触发的函数也会返回 HTTP 响应,因此该函数将返回一个 MultiResponse 对象,表示 HTTP 和队列输出。
[Function("HttpExample")]
public MultiResponse Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
此示例是包含输出绑定的 MultiResponse 对象的定义:
public class MultiResponse
{
[QueueOutput("outqueue",Connection = "AzureWebJobsStorage")]
public string[] Messages { get; set; }
public IActionResult HttpResponse { get; set; }
}
此示例使用 ASP.NET Core 集成。 如果不使用 ASP.NET Core集成,则需要将 HttpRequest 更改为 HttpRequestData,并将 IActionResult 更改为 HttpResponseData。
函数完成后,这些消息被发送到队列。 定义输出绑定的方式取决于你的流程模型。 有关详细信息,包括你可以参考的示例绑定代码的链接,请参阅向函数添加绑定。
@FunctionName("HttpExample")
public HttpResponseMessage run(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
@QueueOutput(name = "msg", queueName = "outqueue",
connection = "AzureWebJobsStorage") OutputBinding<String> msg,
final ExecutionContext context) {
有关详细信息,包括你可以参考的示例绑定代码的链接,请参阅向函数添加绑定。
const { app, output } = require('@azure/functions');
const sendToQueue = output.storageQueue({
queueName: 'outqueue',
connection: 'AzureWebJobsStorage',
});
app.http('HttpExample', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
extraOutputs: [sendToQueue],
handler: async (request, context) => {
try {
context.log(`Http function processed request for url "${request.url}"`);
const name = request.query.get('name') || (await request.text());
context.log(`Name: ${name}`);
if (name) {
const msg = `Name passed to the function ${name}`;
context.extraOutputs.set(sendToQueue, [msg]);
return { body: msg };
} else {
context.log('Missing required data');
return { status: 404, body: 'Missing required data' };
}
} catch (error) {
context.log(`Error: ${error}`);
return { status: 500, body: 'Internal Server Error' };
}
},
});
定义输出绑定的方式取决于 Node.js 模型的版本。 有关详细信息,包括你可以参考的示例绑定代码的链接,请参阅向函数添加绑定。
$outputMsg = $name
Push-OutputBinding -name msg -Value $outputMsg
有关详细信息,包括你可以参考的示例绑定代码的链接,请参阅向函数添加绑定。
@app.route(route="HttpExample")
@app.queue_output(arg_name="msg", queue_name="outqueue", connection="AzureWebJobsStorage")
def HttpExample(req: func.HttpRequest, msg: func.Out [func.QueueMessage]) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
定义输出绑定的方式取决于 Python 模型的版本。 有关详细信息,包括你可以参考的示例绑定代码的链接,请参阅向函数添加绑定。
import {
app,
output,
HttpRequest,
HttpResponseInit,
InvocationContext,
StorageQueueOutput,
} from '@azure/functions';
const sendToQueue: StorageQueueOutput = output.storageQueue({
queueName: 'outqueue',
connection: 'AzureWebJobsStorage',
});
export async function HttpExample(
request: HttpRequest,
context: InvocationContext,
): Promise<HttpResponseInit> {
try {
context.log(`Http function processed request for url "${request.url}"`);
const name = request.query.get('name') || (await request.text());
context.log(`Name: ${name}`);
if (name) {
const msg = `Name passed to the function ${name}`;
context.extraOutputs.set(sendToQueue, [msg]);
return { body: msg };
} else {
context.log('Missing required data');
return { status: 404, body: 'Missing required data' };
}
} catch (error) {
context.log(`Error: ${error}`);
return { status: 500, body: 'Internal Server Error' };
}
}
app.http('HttpExample', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
handler: HttpExample,
});
定义输出绑定的方式取决于 Node.js 模型的版本。 有关详细信息,包括你可以参考的示例绑定代码的链接,请参阅向函数添加绑定。
使用下表查找可用于指导更新现有函数的特定绑定类型的示例。 首先,选择与project对应的语言选项卡。
C# 的绑定代码取决于特定的进程模型。
| 服务 | 例子 | 示例 |
|---|---|---|
| Blob 存储 |
触发器 Input Output |
Link |
| Azure Cosmos DB |
触发器 Input Output |
Link |
| Azure 数据资源管理器 |
Input Output |
Link |
| Azure SQL |
触发器 Input Output |
Link |
| 事件网格 |
触发器 Output |
Link |
| 事件中心 |
触发器 Output |
|
| IoT 中心 |
触发器 Output |
|
| HTTP | 触发器 | Link |
| 队列存储 |
触发器 Output |
Link |
| RabbitMQ |
触发器 Output |
|
| SendGrid | Output | |
| 服务总线 |
触发器 Output |
Link |
| Azure SignalR 服务 |
触发器 Input Output |
|
| 表存储 |
Input Output |
|
| 定时器 | 触发器 | Link |
| Twilio | Output | Link |
| 服务 | 例子 | 示例 |
|---|---|---|
| Blob 存储 |
触发器 Input Output |
Link |
| Azure Cosmos DB |
触发器 Input Output |
Link |
| Azure 数据资源管理器 |
Input Output |
Link |
| Azure SQL |
触发器 Input Output |
|
| 事件网格 |
触发器 Output |
Link |
| 事件中心 |
触发器 Output |
|
| IoT 中心 |
触发器 Output |
|
| HTTP | 触发器 | Link |
| 队列存储 |
触发器 Output |
Link |
| RabbitMQ |
触发器 Output |
|
| SendGrid | Output | |
| 服务总线 |
触发器 Output |
Link |
| Azure SignalR 服务 |
触发器 Input Output |
|
| 表存储 |
Input Output |
|
| 定时器 | 触发器 | Link |
| Twilio | Output |
| 服务 | 例子 | 示例 |
|---|---|---|
| Blob 存储 |
触发器 Input Output |
Link |
| Azure Cosmos DB |
触发器 Input Output |
Link |
| Azure 数据资源管理器 |
Input Output |
|
| Azure SQL |
触发器 Input Output |
Link |
| 事件网格 |
触发器 Output |
|
| 事件中心 |
触发器 Output |
|
| IoT 中心 |
触发器 Output |
|
| HTTP | 触发器 | Link |
| 队列存储 |
触发器 Output |
Link |
| RabbitMQ |
触发器 Output |
|
| SendGrid | Output | |
| 服务总线 |
触发器 Output |
Link |
| Azure SignalR 服务 |
触发器 Input Output |
|
| 表存储 |
Input Output |
|
| 定时器 | 触发器 | |
| Twilio | Output |
| 服务 | 例子 | 示例 |
|---|---|---|
| Blob 存储 |
触发器 Input Output |
|
| Azure Cosmos DB |
触发器 Input Output |
|
| Azure SQL |
触发器 Input Output |
|
| 事件网格 |
触发器 Output |
|
| 事件中心 |
触发器 Output |
|
| IoT 中心 |
触发器 Output |
|
| HTTP | 触发器 | Link |
| 队列存储 |
触发器 Output |
|
| RabbitMQ |
触发器 Output |
|
| SendGrid | Output | |
| 服务总线 |
触发器 Output |
|
| Azure SignalR 服务 |
触发器 Input Output |
|
| 表存储 |
Input Output |
|
| 定时器 | 触发器 | |
| Twilio | Output |
Python 的绑定代码取决于 Python 模型版本。
| 服务 | 例子 | 示例 |
|---|---|---|
| Blob 存储 |
触发器 Input Output |
Link |
| Azure Cosmos DB |
触发器 Input Output |
Link |
| Azure 数据资源管理器 |
Input Output |
|
| Azure SQL |
触发器 Input Output |
Link |
| 事件网格 |
触发器 Output |
|
| 事件中心 |
触发器 Output |
|
| IoT 中心 |
触发器 Output |
|
| HTTP | 触发器 | Link |
| 队列存储 |
触发器 Output |
|
| RabbitMQ |
触发器 Output |
|
| SendGrid | Output | |
| 服务总线 |
触发器 Output |
Link |
| Azure SignalR 服务 |
触发器 Input Output |
|
| 表存储 |
Input Output |
|
| 定时器 | 触发器 | |
| Twilio | Output |
Visual Studio Code
使用 Visual Studio Code 开发函数时,函数使用 function.json 文件,Azure Functions扩展可以自动将绑定添加到现有 function.json 文件。 若要了解详细信息,请参阅 添加输入和输出绑定。
Azure 门户
在 Azure portal 中开发函数时,可以在给定函数的 Integrate 选项卡中添加输入和输出绑定。 新绑定将添加到 function.json 文件或方法属性,具体取决于你的语言。 以下文章演示了如何在门户中向现有函数添加绑定的示例: