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

适用于 .NET 的 Azure WebJobs EventGrid 客户端库 - 版本 3.3.0

此扩展提供用于在 Azure Functions 中接收事件网格 Webhook 调用的功能,使你能够轻松编写响应发布到事件网格的任何事件的函数。

入门

安装包

使用 NuGet 安装事件网格扩展:

dotnet add package Microsoft.Azure.WebJobs.Extensions.EventGrid

先决条件

必须具有 Azure 订阅 和具有自定义事件网格主题或域的 Azure 资源组。 按照此分步教程注册事件网格资源提供程序并使用Azure 门户创建事件网格主题。 有一个 类似的教程 使用 Azure CLI

对客户端进行身份验证

若要发布扩展事件,需要 endpoint 事件网格主题的 和 , credential可以使用主题的访问密钥创建 。

可以在 Azure 门户中 或使用下面的 Azure CLI 代码片段找到事件网格主题的终结点。

az eventgrid topic show --name <your-resource-name> --resource-group <your-resource-group-name> --query "endpoint"

还可以通过 门户或使用以下 Azure CLI 代码片段找到访问密钥:

az eventgrid topic key list --name <your-resource-name> --resource-group <your-resource-group-name> --query "key1"

关键概念

使用事件网格输出绑定

请按照 绑定教程 操作,了解如何使用此扩展发布 EventGrid 事件。

使用事件网格触发器

请按照 教程 操作,了解如何在发布事件时触发 Azure 函数。

示例

使用事件网格输出绑定的函数

如果为主题使用 EventGrid 架构,则可以输出 EventGridEvents。

public static class EventGridEventBindingFunction
{
    [FunctionName("EventGridEventBindingFunction")]
    public static async Task<IActionResult> RunAsync(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        [EventGrid(TopicEndpointUri = "EventGridEndpoint", TopicKeySetting = "EventGridKey")] IAsyncCollector<EventGridEvent> eventCollector)
    {
        EventGridEvent e = new EventGridEvent(await req.ReadAsStringAsync(), "IncomingRequest", "IncomingRequest", "1.0.0");
        await eventCollector.AddAsync(e);
        return new OkResult();
    }
}

如果为主题使用 CloudEvent 架构,则可以输出 CloudEvents。

public static class CloudEventBindingFunction
{
    [FunctionName("CloudEventBindingFunction")]
    public static async Task<IActionResult> RunAsync(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        [EventGrid(TopicEndpointUri = "EventGridEndpoint", TopicKeySetting = "EventGridKey")] IAsyncCollector<CloudEvent> eventCollector)
    {
        CloudEvent e = new CloudEvent("IncomingRequest", "IncomingRequest", await req.ReadAsStringAsync());
        await eventCollector.AddAsync(e);
        return new OkResult();
    }
}

还可以将 Azure 标识与输出绑定一起使用。 为此,请将 Connection 属性设置为包含事件网格主题终结点的应用设置的名称,以及 此处详细介绍的一组可选标识信息。 设置 Connection 属性时, TopicEndpointUri 不应设置 和 TopicKeySetting 属性。

public static class CloudEventOutputBindingWithIdentityFunction
{
    [FunctionName("CloudEventOutputBindingWithIdentityFunction")]
    public static async Task<IActionResult> RunAsync(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        [EventGrid(Connection = "MyConnection")] IAsyncCollector<CloudEvent> eventCollector)
    {
        CloudEvent e = new CloudEvent("IncomingRequest", "IncomingRequest", await req.ReadAsStringAsync());
        await eventCollector.AddAsync(e);
        return new OkResult();
    }
}

对于本地开发,请使用 local.settings.json 文件存储连接信息:

{
  "Values": {
    "myConnection__topicEndpointUri": "{topicEndpointUri}"
  }
}

部署后,使用 应用程序设置 来存储此信息。

还可以输出字符串或 JObject,扩展将尝试分析为正确的强类型事件。

使用事件网格触发器的函数

还可以创建一个函数,每当事件传递到主题时都会执行该函数。 根据为 Azure Function 事件订阅选择的架构,可以绑定到 EventGridEventCloudEvent

public static class EventGridEventTriggerFunction
{
    [FunctionName("EventGridEventTriggerFunction")]
    public static void Run(
        ILogger logger,
        [EventGridTrigger] EventGridEvent e)
    {
        logger.LogInformation("Event received {type} {subject}", e.EventType, e.Subject);
    }
}

如果订阅配置了 CloudEvent 架构:

public static class CloudEventTriggerFunction
{
    [FunctionName("CloudEventTriggerFunction")]
    public static void Run(
        ILogger logger,
        [EventGridTrigger] CloudEvent e)
    {
        logger.LogInformation("Event received {type} {subject}", e.Type, e.Subject);
    }
}

还可以绑定到事件数组。 如果为事件网格订阅 启用了批处理 ,这非常有用。

public static class EventGridEventBatchTriggerFunction
{
    [FunctionName("EventGridEventBatchTriggerFunction")]
    public static void Run(
        ILogger logger,
        [EventGridTrigger] EventGridEvent[] events)
    {
        foreach (EventGridEvent eventGridEvent in events)
        {
            logger.LogInformation("Event received {type} {subject}", eventGridEvent.EventType, eventGridEvent.Subject);
        }
    }
}

同样,对于使用 CloudEvent 架构配置的订阅:

public static class CloudEventBatchTriggerFunction
{
    [FunctionName("CloudEventBatchTriggerFunction")]
    public static void Run(
        ILogger logger,
        [EventGridTrigger] CloudEvent[] events)
    {
        foreach (CloudEvent cloudEvent in events)
        {
            logger.LogInformation("Event received {type} {subject}", cloudEvent.Type, cloudEvent.Subject);
        }
    }
}

疑难解答

有关故障排除指南,请参阅监视Azure Functions

后续步骤

阅读 Azure 函数简介创建 Azure 函数指南

供稿

有关构建、测试和参与此库的详细信息,请参阅我们的 CONTRIBUTING.md

本项目欢迎贡献和建议。 大多数贡献要求你同意贡献者许可协议 (CLA),并声明你有权(并且确实有权)授予我们使用你的贡献的权利。 有关详细信息,请访问 cla.microsoft.com

此项目采用了 Microsoft 开放源代码行为准则。 有关详细信息,请参阅行为准则常见问题解答,或如果有任何其他问题或意见,请与 联系。

曝光数