如何从Azure功能向事件中心发送消息?

时间:2018-04-11 14:32:42

标签: azure-functions azure-eventhub

我有一个Azure功能,可以随时在IoT中心上发送消息。我希望该函数从消息中提取一些信息,然后将其放在另一个事件中心。这是我的代码:

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

public static void Run(EventData eventData, out string outputEventHubMessage, TraceWriter log)
{
    // Get some system properties from the SystemProperties dictionary
    var deviceId = eventData.SystemProperties["iothub-connection-device-id"].ToString();
    var messageSource = eventData.SystemProperties["iothub-message-source"].ToString();
    var enqueuedTime = eventData.SystemProperties["iothub-enqueuedtime"].ToString();
    var sequenceNumber = eventData.SystemProperties["SequenceNumber"].ToString();
    var offset = eventData.SystemProperties["Offset"].ToString();

    var data = Encoding.UTF8.GetString(eventData.GetBytes());
    var message = string.Format("Message Source: {0}; Enqueued Time: {1}; Sequence Number: {2}; Offset: {3}; DeviceId: {4}; Data: {5}", messageSource, enqueuedTime, sequenceNumber, offset, deviceId, data);

    outputEventHubMessage = message;

    log.Info($"{message}");
}

'outputEventHubMessage'被定义为一个输出参数,指向我为此目的设置的事件中心。当函数触发时,我收到以下错误:

2018-04-11T14:23:50.295 [Error] Exception while executing function: Functions.MonitorHub. Microsoft.Azure.WebJobs.Host: Error while handling parameter outputEventHubMessage after function returned:. 
    Microsoft.ServiceBus: Unauthorized access. 'Send' claim(s) are required to perform this operation. Resource: 'sb://iothub-ns-monitorhub-419050-c0a1f3eb71.servicebus.windows.net/monitorhub'. TrackingId:cd503b7c674e4806a64b592bfa3d51f2_G9, SystemTracker:gateway5, Timestamp:4/11/2018 2:23:50 PM.

我不知道这意味着什么或为什么Azure在我创建输出参数时不会为我设置

1 个答案:

答案 0 :(得分:2)

您使用的连接字符串似乎没有“发送”权限。

要检查这一点,请转到Event Hubs命名空间 - > Settings - > Shared access policies并确保根据您使用的政策列出Send

enter image description here