【问题标题】:ExcelDNA RTD with SignalR带有 SignalR 的 ExcelDNA RTD
【发布时间】:2021-10-04 16:01:38
【问题描述】:

我正在尝试将 ExcelDNA RTD 与 ASP.NET SignalR 服务器连接起来。 每当服务器发生变化时,就会向连接的客户端推送一条消息,并且我的 ExcelDna 加载项正在获取新消息,但注册的函数没有更新。

我的 RTD 服务器:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using ExcelDna.Integration;
using ExcelDna.Integration.Rtd;

namespace DMT.Excel.AddIn
{
    [ComVisible(true)]
    public class SignalRServer : ExcelRtdServer
    {
        private HubConnection _connection;
        private List<Topic> _topics = new List<Topic>();

        public TradesRtdServer()
        {
            _connection = new HubConnectionBuilder()
                .WithUrl("http://localhost:5000/api/test/hub")
                .WithAutomaticReconnect()
                .Build();

            _connection.On<object>("Test", m =>
            {
                foreach (Topic topic in _topics)
                {
                    topic.UpdateValue(m);
                }
            });


            Task.Run(() => _connection.StartAsync());
        }

        protected override bool ServerStart()
        {
            DmtAddIn.Logger.Information("ServerStart");
            return true;
        }

        protected override void ServerTerminate()
        {
            DmtAddIn.Logger.Information("ServerTerminate");
        }

        protected override object ConnectData(Topic topic, IList<string> topicInfo, ref bool newValues)
        {
            DmtAddIn.Logger.Information("ConnectData: {0} - {{{1}}}", topic.TopicId, string.Join(", ", topicInfo));
            _topics.Add(topic);
            return ExcelErrorUtil.ToComError(ExcelError.ExcelErrorNA);
        }

        protected override void DisconnectData(Topic topic)
        {
            _topics.Remove(topic);
            DmtAddIn.Logger.Information("DisconnectData: {0}", topic.TopicId);
        }
    }
}

我的功能


        [ExcelFunction(Name = "SignalR.Test.RTD")]
        public static object GetSignalRMessages()
        {
            return XlCall.RTD("Excel.AddIn.Trading.SignalRServer", null, "Test");
        }

当我调试时,我可以看到每当从服务器推送消息时topic.UpdateValue(m); 会被命中,而不是GetSignalRMessages

我是否遗漏了将主题更改传播到函数的任何内容?

谢谢!

约瑟夫

【问题讨论】:

    标签: c# signalr excel-dna signalr-client


    【解决方案1】:

    我设法通过从 SignalR 服务器发送一个字符串,然后在客户端反序列化它来解决这个问题

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
    【解决方案2】:

    ExcelRtdServer 检查传递给 UpdateValue 的值是否与之前的值不同。您可能每次都传递相同的值,或者某些值被解释为相同的 Excel 数据类型(例如,某些对象类型每次都转换为相同的字符串)。

    您最好通过 IObservable 或 Rx 抽象来构建它,它们比 ExcelRtdServer 更高级别。在此处查看示例https://github.com/Excel-DNA/Samples/tree/master/RtdClocks

    也许像这个结合了 Rx 和 SignalR 的项目:https://github.com/jwooley/SignalrRxSamples

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 2012-09-24
      • 2017-01-27
      • 1970-01-01
      • 2015-04-14
      相关资源
      最近更新 更多