我正在尝试将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
在将主题更改传播到函数时,我是否遗漏了什么?
谢谢!
约瑟夫
发布于 2021-10-04 21:33:48
我设法解决了这个问题,方法是从SignalR服务器发送一个字符串,然后在客户端反序列化它
发布于 2021-10-04 21:43:10
ExcelRtdServer检查传入UpdateValue的值是否与之前的值不同。您可能每次都传递相同的值,或者传递一些被解释为Excel数据类型的值作为相同的值(例如,某些对象类型每次都转换为相同的字符串)。
您最好通过比ExcelRtdServer更高级别的IObservable或Rx抽象来构建它。请参阅此处的示例https://github.com/Excel-DNA/Samples/tree/master/RtdClocks
也许像这个结合了Rx和SignalR的项目:https://github.com/jwooley/SignalrRxSamples
https://stackoverflow.com/questions/69439032
复制相似问题