我有一个带有TimerTrigger
的Azure功能,可以正常工作并生成400,000个Service Bus主题消息。我目前正在使用Service Bus API在我的Function中手动将消息推入其中。我现在想要用输出绑定替换代码,这简化了事情。
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:错误索引方法'Function1.Run'。 Microsoft.Azure.WebJobs.Host:无法将参数'output'绑定到类型IAsyncCollector`1。 确保绑定支持参数Type。
当reading the documentation时,IAsyncCollector
1`是支持的绑定。
这是我的.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();
}
}
}
}
我在这里做错了什么?
答案 0 :(得分:1)
截至今天(2018年3月),功能V2的服务总线支持仍在“正在建设中”。它有点像,但需要一些魔法才能应用。
问题是它已经从默认的绑定包中移出到扩展模型中,而且它仍然很粗糙。
请参阅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完整版功能。