【问题标题】:Azure Function - Send a message from an Event Hub Trigger to an Azure SignalR ServiceAzure 函数 - 从事件中心触发器向 Azure SignalR 服务发送消息
【发布时间】:2020-07-02 17:27:25
【问题描述】:

我正在尝试从 Azure Function EventHub 触发器向 Azure SignalR 服务发送消息。

我没有得到异常,甚至没有输出窗口上的信息,我做错了什么?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
using Microsoft.Extensions.Logging;

namespace AzureFunction_EventHubToSignalR
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task Run([EventHubTrigger("dio", Connection = "EventHubSharedAccessPolicyConnectionString")] EventData[] events, 
                                     [SignalR(HubName = "%AzureSignalRHubName%")] IAsyncCollector<SignalRMessage> signalRMessages, 
                                     ILogger log)
        {
            var exceptions = new List<Exception>();

            foreach (EventData eventData in events)
            {
                try
                {
                    string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);

                    await signalRMessages.AddAsync(
                       new SignalRMessage
                       {
                           Target = "BroadcastMessage",
                           Arguments = new[] { messageBody }
                       });

                    await signalRMessages.FlushAsync();

                    // Replace these two lines with your processing logic.
                    log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");
                    await Task.Yield();
                }
                catch (Exception e)
                {
                    log.LogError($"Event Hub trigger failed to process or send a message: {e}");
                    // We need to keep processing the rest of the batch - capture this exception and continue.
                    // Also, consider capturing details of the message that failed processing so it can be processed again later.
                    exceptions.Add(e);
                }
            }

            // Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure.

            if (exceptions.Count > 1)
                throw new AggregateException(exceptions);

            if (exceptions.Count == 1)
                throw exceptions.Single();
        }
    }
}

【问题讨论】:

    标签: c# azure .net-core azure-eventhub azure-signalr


    【解决方案1】:

    由于某种原因,我无法使用 SignalR 扩展使其工作,而是最终使用了原始 SignalR 包(Microsoft.AspNetCore.SignalR.Client、Microsoft.AspNetCore.SignalR.Client.Core) 向 Azure SignalR 服务发送消息..

     [FunctionName("Function1")]
     public static async Task Run([EventHubTrigger("dio", Connection = "EventHubSharedAccessPolicyConnectionString")] EventData[] events,
                                  [SignalRConnectionInfo(HubName = "%AzureSignalRHubName%")] SignalRConnectionInfo connectionInfo,
                                  ILogger log)
     {
     ....
         //Build connection with token
         HubConnection _connection = new HubConnectionBuilder()
         .WithUrl(connectionInfo.Url, option =>
         {
              option.Headers.Add("Authorization", $"Bearer {connectionInfo.AccessToken}");
         })
         .Build();
    
         //Start the hub connection
         await _connection.StartAsync();
    
         //My Hub has a BroadcastMessage method that receives 2 arguments
         await _connection.SendAsync("BroadcastMessage", "test", "test", default);
     ...
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-16
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多