我已经尝试了本文档提供的所有变体,以创建一个接受事件网格事件并输出和事件网格事件的天蓝色函数:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-grid-output
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using Microsoft.Azure.EventGrid.Models;
using Microsoft.Azure.WebJobs.Extensions.EventGrid;
namespace Azure.Extensions.WebJobs.Sample
{
public static class CloudEventBindingFunction
{
[FunctionName("TestFunctionName")]
public static async Task RunAsync(
[EventGridTrigger] EventGridEvent eventGridEvent, [EventGrid(TopicEndpointUri = "TopicEndpointUri", TopicKeySetting = "TopicKeySetting")] IAsyncCollector<EventGridEvent> eventCollector, ILogger log)
{
var e = new EventGridEvent("message-id", "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0");
await eventCollector.AddAsync(e);
}
}
}
无论我使用什么版本,我都会在天蓝色中收到错误消息:
Microsoft.Azure.EventGrid.Models.EventGridEvent is not a valid event type.
我也尝试过使用此函数的 [return: ...] 变体,并且遇到了同样的问题。
我的host.json
Azure Functions runtime is 2.0
Function runtime version is ~4
Azure 功能托管在应用服务计划(高级)上
该函数已用
func new dotnet
初始化
为了排除某些情况,这是我的 csproj 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.10.0" />
<PackageReference Include="Microsoft.Azure.EventGrid" Version="3.2.1" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.11.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventGrid" Version="3.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
我在将 Microsoft.Azure.WebJobs.Extensions.EventGrid 从版本 2 更新到版本 3 时遇到了同样的问题。此处的文档显示提供给 EventGrid 输出绑定的类型在 Microsoft.Azure 的扩展版本 2 和版本 3 之间发生了变化.EventGrid.Models.EventGridEvent 到 Azure.Messaging.EventGrid,因此如果要更新该包,还需要更新数据类型。