问题描述
我现在有一个用于实时代理的SignalR集线器,我正在尝试将我的机器人框架与signalR集线器连接。为此,我已经为signalR客户端设置了。 然后,我在会话状态访问器中设置了集线器连接。这样我就可以与Signalr Hub建立连接。但是当我尝试访问状态访问器集线器连接时,出现以下错误: 找不到用于类型Microsoft.AspNetCore.SignalR.Client.HubConnection的构造函数。一个类应该具有一个默认构造函数,一个带有参数的构造函数或一个标有JsonConstructor属性的构造函数。路径“ LiveAgentModel.connection.ServerTimeout”。
这是模型的代码:'''公共类LiveAgentModel { public bool isConnected {get;组; } = false;
public string agentConnectionId { get; set; }
public string name { get; set; }
public HubConnection connection;
}'''
这是我尝试访问并遇到错误的地方。
public async Task OnTurnAsync(ITurnContext turnContext,NextDelegate next,CancellationToken cancellationToken = default)
{
var conversationStateAccessors = _conversationState.CreateProperty<LiveAgentModel>(nameof(LiveAgentModel));
var conversationData = await conversationStateAccessors.GetAsync(turnContext,() => new LiveAgentModel()).ConfigureAwait(false);
}
我试图获得如下值时,将值设置为connection后出现错误
private async Task Escalate(ITurnContext sendTurnContext,Activity handoffEvent)
{
var conversationStateAccessors = _conversationState.CreateProperty<LiveAgentModel>(nameof(LiveAgentModel));
var conversationData = await conversationStateAccessors.GetAsync(sendTurnContext,() => new LiveAgentModel()).ConfigureAwait(false);
conversationData.connection = new HubConnectionBuilder()
.WithUrl("https://localhost:44348/ChatHub")
.Build();
请帮助我解决这个问题。
解决方法
很显然,一旦将其存储在sessionState中,就不可能反序列化HubConnection对象并重新创建它,我不知道这是否是最好的方法,但是它将允许您拥有hubconnection对象活着:
创建一个包含字典的类,其中的键将是用户的“ conversationId”,值将是您的HubConnection对象
1.-创建一个类:
public class MyDictionaryClass{
//Initialize to prevent null exception
public Dictionary<string,HubConnection> diccionaryConnections= new Dictionary<string,HubConnection>();
}
2.-在Startup.cs中创建一个单例服务(该词典将对所有用户可用,并且将在应用程序运行时保留lls数据):
services.AddSingleton<MyDictionaryClass>();
3.-使用服务:
我们必须在对话框Dependency injection中使用依赖注入...示例:
public myDialog{
MyDictionaryClass _myDictionaryService;
//Constructor
public myDialog(MyDictionaryClass myDictionaryService){
_myDictionaryService = myDictionaryService;
}
}
4.-进入myDialog类添加方法
public MyMethod(WaterfallStepContext stepContext,CancellationToken cancellationToken){
//check that the dictionary has a valid connection
if (_myDictionaryService.diccionaryConnections.ContainsKey(stepContext.Context.Activity.Conversation.Id)) {
//Dictionary has user connection... Make something
var connection = _myDictionaryService.diccionaryConnections[dialogContext.Context.Activity.Conversation.Id]
await **conexion**.InvokeAsync("HubMethod","Hello someone");
}
else {
//the dictionary has no user connection... create connection to signalr
var conexion = new HubConnectionBuilder()
.WithUrl("https://localhost:[MYPORT]/ServerHub")
.Build();
//Add to dictionary
_myDictionaryService.diccionaryConnections.Add(conversationId,conexion);
//Send message to HubServerMethod
await conexion.InvokeAsync("HubMethod","hi my frend");
}
}
*您应该考虑可能的用户场景和signalR事件(已连接,已重新连接,已断开等)Events SignalR,以便您从字典中添加和删除元素,可能此解决方案将让您继续工作和调查
* 重要的是要记住,单例字典仅在应用程序运行时才存在,如果您想改进它,我想知道。