【问题标题】:Connecting signalr client with bot framework将信号器客户端与机器人框架连接
【发布时间】:2020-08-21 12:40:06
【问题描述】:

我有一个用于实时代理的 signalR 集线器,现在我正在尝试将我的机器人框架与 signalR 集线器连接起来。为此,我已经完成了 signalR 客户端的设置。 然后我在对话状态访问器中给出了集线器连接。然后我可以与信号器集线器建立连接。但是当我尝试访问状态访问器集线器连接时,出现以下错误: 找不到用于类型 Microsoft.AspNetCore.SignalR.Client.HubConnection 的构造函数。一个类应该有一个默认构造函数、一个带参数的构造函数或一个标有 JsonConstructor 属性的构造函数。路径“LiveAgentModel.connection.ServerTimeout”。

这是模型的代码:''' public class LiveAgentModel { 公共布尔 isConnected { 获取;放; } = 假;

    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);
}

当我试图获取如下值时,将值设置为连接后出现错误

 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();

请帮我解决这个问题。

【问题讨论】:

    标签: signalr botframework signalr.client


    【解决方案1】:

    显然,一旦将 HubConnection 对象存储在 conversationState 中,就无法反序列化并重新创建它,我不知道这是否是最好的方法,但它可以让您拥有 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,以便您在字典中添加和删除元素,这个解决方案可能会让您继续工作和调查

    *请务必记住,单例字典仅在应用程序运行时存在,如果您找到改进方法,我想知道。

    【讨论】:

      猜你喜欢
      • 2013-03-31
      • 1970-01-01
      • 2014-03-11
      • 2018-12-29
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 2020-04-26
      • 2015-11-21
      相关资源
      最近更新 更多