【问题标题】:error CS0234: Microsoft.Azure.EventHubs assembly/metadata file missing in azure function 2.0错误 CS0234:Azure 函数 2.0 中缺少 Microsoft.Azure.EventHubs 程序集/元数据文件
【发布时间】:2018-11-27 09:38:52
【问题描述】:

我正在尝试创建 EventHub 触发 azure 函数,该函数具有许多作为事件消息批处理的基数。

以下是run.csx中的代码

#r "Microsoft.Azure.EventHubs"

using System.Text;
using System;
using Microsoft.ServiceBus.Messaging;
using Microsoft.Azure.EventHubs;

public static void Run(EventData[] eventMessage, ILogger logger)
{
    logger.LogInformation("Event: {Encoding.UTF8.GetString(eventMessage.Body)}");
    logger.LogInformation("EnqueuedTimeUtc={eventMessage.SystemProperties.EnqueuedTimeUtc}");
    logger.LogInformation("SequenceNumber={eventMessage.SystemProperties.SequenceNumber}");
    logger.LogInformation("Offset={eventMessage.SystemProperties.Offset}");

}

沿着运行时配置 host.json 是

{
  "version": "2.0"
}

和函数的配置文件function.json

{
  "bindings": [
    {
    "type": "eventHubTrigger",
    "name": "eventMessage",
    "direction": "in",
    "eventHubName": "myEventHub",
    "connection": "consumer_hub",
    "consumerGroup": "$Default",
    "cardinality": "many"
    }
  ]
}

在编译时出现有关 assemply 的错误后丢失

2018-11-27T09:36:54.325 [Error] run.csx(1,1): error CS0006: Metadata file 'Microsoft.Azure.EventHubs' could not be found
2018-11-27T09:36:54.389 [Error] run.csx(5,17): error CS0234: The type or namespace name 'ServiceBus' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
2018-11-27T09:36:54.425 [Error] run.csx(6,23): error CS0234: The type or namespace name 'EventHubs' does not exist in the namespace 'Microsoft.Azure' (are you missing an assembly reference?)
2018-11-27T09:36:54.522 [Error] run.csx(9,24): error CS0246: The type or namespace name 'EventData' could not be found (are you missing a using directive or an assembly reference?)

您能帮我解释一下为什么 azure 函数中没有依赖项吗?甚至 azure 文档对此都有参考。这意味着该组件应该已经存在于 azure 提供的运行时环境中。

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

【问题讨论】:

    标签: azure azure-functions


    【解决方案1】:

    文档有点不一致,试试下面的代码。

    #r "../bin/Microsoft.Azure.EventHubs.dll"
    
    using System.Text;
    using System;
    using Microsoft.Azure.EventHubs;
    
    public static void Run(EventData[] eventMessage, ILogger logger)
    {
        foreach (var message in eventMessage)
        {
            logger.LogInformation($"Event: {Encoding.UTF8.GetString(message.Body)}");
            logger.LogInformation($"EnqueuedTimeUtc={message.SystemProperties.EnqueuedTimeUtc}");
            logger.LogInformation($"SequenceNumber={message.SystemProperties.SequenceNumber}");
            logger.LogInformation($"Offset={message.SystemProperties.Offset}");
        }
    }
    

    另外,function.json中cardinality是js函数批量接收事件。对于 c#,EventData[] 就足够了。

    【讨论】:

    • 它的工作。所以我们需要为此添加外部程序集。这很有帮助。
    • @OomphFortuity 我们应该使用#r "Microsoft.Azure.EventHubs",但是有一个issue,我们必须安装存储扩展以使引用指令工作,或者我们需要添加我发现的相对路径.
    • 这是很棒的信息。
    猜你喜欢
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 2017-12-03
    • 2020-03-31
    相关资源
    最近更新 更多