azure - 函数无法绑定(bind)到输出参数

标签 azure azure-functions azureservicebus azure-servicebus-topics

我有一个带有 TimerTrigger 的 Azure 函数,它可以正常工作并生成 400,000 条服务总线主题消息。我目前正在使用服务总线 API 在我的函数中手动将消息推送到其中。我现在想用输出绑定(bind)替换该代码,这样可以简化事情。

public static class Function1
{
    [FunctionName("ExportProcessor")]
    public static async Task Run(
        [TimerTrigger("0 30 5 */1 * *", RunOnStartup = true)]TimerInfo myTimer, 
        ILogger logger, 
        [ServiceBus("new-movie-publish", EntityType = EntityType.Topic)]IAsyncCollector<string> output)
    {
        await output.AddAsync("Foo");
        await output.AddAsync("Bar");
    }
}

问题是,当我运行此测试代码时,它失败并出现以下错误:

MicrosoftAzure.WebJobs.Host: Error indexing method 'Function1.Run'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'output' to type IAsyncCollector`1. Make sure the parameter Type is supported by the binding.

何时 reading the documentation , IAsyncCollector1` 是受支持的绑定(bind)。

已更新以显示配置文件和依赖项。

这是我的 .csproj 文件:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Dapper" Version="1.50.4" />
    <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta4" />
    <PackageReference Include="Microsoft.Azure.WebJobs.ServiceBus" Version="3.0.0-beta4" />
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.9" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Properties\" />
  </ItemGroup>
</Project>

我的 host.json 文件:

{
  "logger": {
    "categoryFilter": {
      "defaultLevel": "Information",
      "categoryLevels": {
        "Host": "Information",
        "Function": "Information"
      }
    }
  }
}

和我的 local.settings.json 配置

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "...",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "...",
    "ExportUrl": "...",
    "ExportUtcHour": 5,
    "DefaultConnection": "...",
    "NewMovieTopic": "new-movie-publish",
    "AzureWebJobsServiceBus": "..."
  }
}

当我使用此代码发布消息时,这些设置工作正常:

public class MoviePublisherConfig
{
    public MoviePublisherConfig()
    {
        this.AzureWebJobsServiceBus = Environment.GetEnvironmentVariable(nameof(AzureWebJobsServiceBus), EnvironmentVariableTarget.Process);
        this.NewMovieTopic= Environment.GetEnvironmentVariable(nameof(NewMovieTopic), EnvironmentVariableTarget.Process);
    }

    public string AzureWebJobsServiceBus { get; set; }
    public string NewMovieTopic { get; set; }
}

public class MoviePublisher
{
    private readonly MoviePublisherConfig config;
    private readonly ILogger logger;

    public MoviePublisher(MoviePublisherConfig config, ILogger logger)
    {
        this.config = config ?? throw new ArgumentNullException(nameof(config));
        this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public async Task PublishMovies(Movie[] movies)
    {
        var topicClient = new TopicClient(config.AzureWebJobsServiceBus, config.NewMovieTopic);
        var pendingTasks = new List<Task>();

        for(int index = 0; index < movies.Length; index++)
        {
            string movieJson = JsonConvert.SerializeObject(movies[index]);
            byte[] messageBuffer = Encoding.UTF8.GetBytes(movieJson);
            var message = new Message(messageBuffer);

            Task sendTask = topicClient.SendAsync(message);
            pendingTasks.Add(sendTask);

            if (pendingTasks.Count >= 1000)
            {
                await Task.WhenAll(pendingTasks);
                this.logger.LogInformation($"Processed {pendingTasks.Count} new movies.");
                pendingTasks.Clear();
            }
        }
    }
}

我在这里做错了什么?

最佳答案

截至今天(2018 年 3 月),V2 Functions 上的服务总线支持仍处于“ build 中”。它就在那里,但需要一些魔法来应用。

问题在于它已从默认的绑定(bind)包移至扩展模型中,而且仍然很粗糙。

请参阅 github 上的以下问题:

Migrate ServiceBus Extension to .NET Core - 已关闭,但请参阅评论

Build failure after installing ExtensionsMetadatGenerator into empty v2 app

工作版本尚未在 NuGet 中提供,但如果您确实需要,可以在 MyGet 中获取它。 ,版本3.0.0-beta4-11250。

其他选项包括坚持手动发送,或使用 V1/.NET 完整版本的 Functions。

关于azure - 函数无法绑定(bind)到输出参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49437148/

相关文章:

将消息放入服务总线中的队列后不会触发 Azure Function

azure - Azure 服务总线中的竞争消费者消息传递模式

spring - 在 Spring 应用程序中更新 Azure 总线上的消息锁定

azure - 未加载运行时堆栈 - Azure - Github Actions

azure - 使用数据流的联合事件组合 azure 数据工厂中的多个文件

sql-server - Azure 混合连接不适用于 SQL Server 命名实例

c# - Azure 服务总线中的队列生命周期是多少?

azure - 获取有关我的租户中的用户的更多信息

c# - 使用 Azure Functions 将多个代理消息输出到 Azure 服务总线主题

azure - 对于每个事件,azure 是否在消费计划中创建 Azure 函数的新实例