【问题标题】:EventHubTrigger C# with eventData object does not work带有 eventData 对象的 EventHubTrigger C# 不起作用
【发布时间】:2018-10-18 10:18:30
【问题描述】:

按照以下文章的说明进行操作后

https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-hubs

我创建了一个 EventHubTrigger,如下所示:

using System;

public static void Run(string myEventHubMessage, ILogger log)
{
log.LogInformation($'C# Event Hub trigger function processed a message: {myEventHubMessage}');
}

这确实没有任何问题,但由于我确实需要额外的元信息,我将代码更改为以下(在第二篇链接文章中描述):

#r 'Microsoft.ServiceBus'
using System.Text;
using System;
using Microsoft.ServiceBus.Messaging;

public static void Run(EventData myEventHubMessage, ILogger log)
{
log.LogInformation($'EnqueuedTimeUtc={myEventHubMessage.EnqueuedTimeUtc}');
log.LogInformation($'SequenceNumber={myEventHubMessage.SequenceNumber}');
log.LogInformation($'Offset={myEventHubMessage.Offset}');
} 

但是此代码会导致以下错误消息(顺便说一句,我还绑定使用已弃用的 TraceWriter 而不是 ILogger 来完全遵循文章,但这会导致相同的错误)

2018-10-11T14:22:24.814 [Error] run.csx(1,1): error CS0006: Metadata file 'Microsoft.ServiceBus' could not be found 
2018-10-11T14:22:24.903 [Error] run.csx(4,17): error CS0234: The type or namespace name 'ServiceBus' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) 

我现在的问题是,有没有人知道该怎么做才能让这段代码运行起来?

当然它必须与程序集有关,但文章指出,在在线门户编辑器中工作时,无需执行进一步的步骤。

提前致谢

菲利克斯

PS:

host.json:

{

  "version": "2.0"

}

extensions.csproj 的内容是:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>

    <TargetFramework>netstandard2.0</TargetFramework>

    <WarningsAsErrors />

  </PropertyGroup>

  <ItemGroup>

    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="3.0.0" />

    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="3.0.0" />

    <PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="1.0.1" />

  </ItemGroup>

</Project>

【问题讨论】:

    标签: c# azure azure-functions azure-eventhub


    【解决方案1】:

    嗯,示例是针对函数 1.x。在 2.x 普遍可用后,我们创建的函数默认在 ~2 运行时,我们可以在 host.json 中看到"version":"2.0"

    试试下面的代码,元数据存储在SystemPropertiesMicrosoft.Azure.EventHubs.EventData 中。

    #r "../bin/Microsoft.Azure.EventHubs.dll"
    
    using System;
    using Microsoft.Azure.EventHubs;
    
    public static void Run(EventData myEventHubMessage, ILogger log)
    {
        log.LogInformation($"EnqueuedTimeUtc={myEventHubMessage.SystemProperties.EnqueuedTimeUtc}");
        log.LogInformation($"SequenceNumber={myEventHubMessage.SystemProperties.SequenceNumber}");
        log.LogInformation($"Offset={myEventHubMessage.SystemProperties.Offset}");
    }
    

    另请注意,我们需要在 C# 中为字符串使用双引号 ",请参阅代码中的 '

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2018-06-18
    • 2021-03-21
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    相关资源
    最近更新 更多