Azure functions 如何从Azure函数向事件中心发送消息?

Azure functions 如何从Azure函数向事件中心发送消息?,azure-functions,azure-eventhub,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,

我有一个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在我创建输出参数时不会为我设置此选项。看起来您使用的连接字符串没有发送权限

要检查这一点,请转到您的事件中心名称空间->
设置
->
共享访问策略
,并确保针对您使用的策略列出了
发送


您正在使用的连接字符串似乎没有发送权限

要检查这一点,请转到您的事件中心名称空间->
设置
->
共享访问策略
,并确保针对您使用的策略列出了
发送


看来您的连接字符串没有发送权限?@Mikhail-也许您可以将此转换为一个答案,并演示如何执行此操作?这对任何新手(比如我)都会有帮助。你到底在哪里向事件中心发送消息?必须有一个包含访问密钥名称的连接字符串,而且相应的访问策略似乎没有发送权限(正如@Mikhail指出的).看起来你的连接字符串没有发送权限?@Mikhail-也许你可以把它转换成一个答案,并演示如何完成?这对任何新手(比如我)都会有帮助。你实际上在哪里将消息发送到事件中心?必须有一个包含访问密钥名称的连接字符串,而且相应的访问策略似乎没有发送权限(正如@Mikhail指出的)。