你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用绑定将函数连接到 Azure 服务

创建函数时,会从一组触发器模板将特定于语言的触发器代码添加到项目中。 如果要使用输入或输出绑定将函数连接到其他服务,则必须在函数中添加特定的绑定定义。 有关绑定的详细信息,请参阅 Azure Functions 触发器和绑定的概念

本地开发

在本地开发函数时,需要更新函数代码以添加绑定。 对于使用 function.json的语言, Visual Studio Code 提供了向函数添加绑定的工具。

基于示例手动添加绑定

向现有函数添加绑定时,需要向代码中的函数定义添加特定于绑定的属性。

向现有函数添加绑定时,需要向代码中的函数定义添加特定于绑定的批注。

将绑定添加到现有函数时,需要更新函数代码并将定义添加到 function.json 配置文件。

将绑定添加到现有函数时,需要根据模型更新函数定义:

需要向代码中的函数定义添加特定于绑定的注释。

以下示例展示了将队列存储输出绑定添加到 HTTP 触发的函数之后的函数定义:

由于 HTTP 触发的函数也会返回 HTTP 响应,因此该函数将返回一个 MultiResponse 对象,表示 HTTP 和队列输出。

[Function("HttpExample")]
public static MultiResponse Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req,
    FunctionContext executionContext)
{

此示例是包含输出绑定的 MultiResponse 对象的定义:

public class MultiResponse
{
    [QueueOutput("outqueue",Connection = "AzureWebJobsStorage")]
    public string[] Messages { get; set; }
    public IActionResult HttpResponse { get; set; }
}

向你自己的项目应用该示例时,可能需要将 HttpRequest 更改为 HttpRequestData,将 IActionResult 更改为 HttpResponseData,具体取决于你是否使用 ASP.NET Core 集成

函数完成后,这些消息被发送到队列。 定义输出绑定的方式取决于你的流程模型。 有关详细信息,包括你可以参考的示例绑定代码的链接,请参阅向函数添加绑定

@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 模型的版本。 有关详细信息,包括你可以参考的示例绑定代码的链接,请参阅向函数添加绑定

使用下表查找可用于指导更新现有函数的特定绑定类型的示例。 首先,选择与你的项目相对应的语言选项卡。

C# 的绑定代码取决于特定的进程模型

服务 例子 示例
Blob 存储 触发器
输入
输出
链接
Azure Cosmos DB(Azure 宇宙数据库) 触发器
输入
输出
链接
Azure 数据资源管理器 输入
输出
链接
Azure SQL 触发器
输入
输出
链接
Event Grid (事件网格) 触发器
输出
链接
事件中心 触发器
输出
IoT 中心 触发器
输出
HTTP 触发器 链接
队列存储 触发器
输出
链接
RabbitMQ 触发器
输出
SendGrid 输出
服务总线 触发器
输出
链接
SignalR 触发器
输入
输出
表存储 输入
输出
定时器 触发器 链接
Twilio 输出 链接
服务 例子 示例
Blob 存储 触发器
输入
输出
链接
Azure Cosmos DB(Azure 宇宙数据库) 触发器
输入
输出
链接
Azure 数据资源管理器 输入
输出
链接
Azure SQL 触发器
输入
输出
Event Grid (事件网格) 触发器
输出
链接
事件中心 触发器
输出
IoT 中心 触发器
输出
HTTP 触发器 链接
队列存储 触发器
输出
链接
RabbitMQ 触发器
输出
SendGrid 输出
服务总线 触发器
输出
链接
SignalR 触发器
输入
输出
表存储 输入
输出
定时器 触发器 链接
Twilio 输出 链接
服务 例子 示例
Blob 存储 触发器
输入
输出
链接
Azure Cosmos DB(Azure 宇宙数据库) 触发器
输入
输出
链接
Azure 数据资源管理器 输入
输出
Azure SQL 触发器
输入
输出
链接
Event Grid (事件网格) 触发器
输出
链接
事件中心 触发器
输出
IoT 中心 触发器
输出
HTTP 触发器 链接
队列存储 触发器
输出
链接
RabbitMQ 触发器
输出
SendGrid 输出
服务总线 触发器
输出
链接
SignalR 触发器
输入
输出
表存储 输入
输出
定时器 触发器 链接
Twilio 输出 链接
服务 例子 示例
Blob 存储 触发器
输入
输出
链接
Azure Cosmos DB(Azure 宇宙数据库) 触发器
输入
输出
链接
Azure SQL 触发器
输入
输出
Event Grid (事件网格) 触发器
输出
链接
事件中心 触发器
输出
IoT 中心 触发器
输出
HTTP 触发器 链接
队列存储 触发器
输出
链接
RabbitMQ 触发器
输出
SendGrid 输出
服务总线 触发器
输出
链接
SignalR 触发器
输入
输出
表存储 输入
输出
定时器 触发器 链接
Twilio 输出 链接

Python 的绑定代码取决于 Python 模型版本。

服务 例子 示例
Blob 存储 触发器
输入
输出
链接
Azure Cosmos DB(Azure 宇宙数据库) 触发器
输入
输出
链接
Azure 数据资源管理器 输入
输出
Azure SQL 触发器
输入
输出
链接
Event Grid (事件网格) 触发器
输出
链接
事件中心 触发器
输出
IoT 中心 触发器
输出
HTTP 触发器 链接
队列存储 触发器
输出
链接
RabbitMQ 触发器
输出
SendGrid 输出
服务总线 触发器
输出
链接
SignalR 触发器
输入
输出
表存储 输入
输出
定时器 触发器 链接
Twilio 输出 链接

Visual Studio Code

使用 Visual Studio Code 开发函数,函数使用 function.json 文件时,Azure Functions 扩展可以自动将绑定添加到现有 function.json 文件。 若要了解详细信息,请参阅 添加输入和输出绑定

Azure 门户

Azure 门户中开发函数时,请在给定函数的 “集成 ”选项卡中添加输入和输出绑定。 新绑定将添加到 function.json 文件或方法属性,具体取决于你的语言。 以下文章演示了如何在门户中向现有函数添加绑定的示例:

后续步骤