3

I am unable to invoke my hub method in my asp.net core backend ,

this is my CommentHub in flutter:

import 'package:signalr_client/signalr_client.dart';
import 'package:logging/logging.dart';   
import 'package:xperience/models/global.dart';

final serverUrl = "http://" + base + ":8070/CommentHub/";

final hubConnection = HubConnectionBuilder().withUrl(serverUrl).build();

class HubService {
static final hubProtLogger = Logger("SignalR - hub");
static final  transportProtLogger = Logger("SignalR - transport");
static final connectionOptions = HttpConnectionOptions;

static final httpOptions = new HttpConnectionOptions(logger: transportProtLogger);
final hubConnection = HubConnectionBuilder().withUrl(serverUrl, options: h 
httpOptions).configureLogging(hubProtLogger).build();

Future addUser(String UserId,String PostId) async {
Logger.root.level = Level.ALL;
 Logger.root.onRecord.listen((LogRecord rec) {
  print('${rec.level.name}: ${rec.time}: ${rec.message}');
});

hubConnection.start().then((result) async {
  final result=await hubConnection.invoke("AddUser",args:<Object>[UserId,PostId]);
  print(result);
});
}

Future removeUser() async {
final result = await hubConnection.invoke("RemoveUser",args: <Object>[]);
print(result);
hubConnection.stop();

}
}

This is my CommentHub.cs class in asp.net core:

      using System;
  using Microsoft.AspNetCore.SignalR;
  using Microsoft.AspNet.SignalR.Hubs;
  using System.Collections.Generic;
  using System.Threading.Tasks;
  using System.Linq;
  using Xperience.Data.Entities.Posts;

 namespace Xperience.Hub
  {
  public class CommentHub : Microsoft.AspNetCore.SignalR.Hub
   {
    public static List<Connection> Connections = new List<Connection>();

    public void AddUser(string UserId,string PostId) {
        Connections.Add(new Connection
        {
            ConnectionId = Context.ConnectionId,
            UserId = UserId,
            PostId = int.Parse(PostId)
        });
    }
    public void RemoveUser() {
        var user = Connections.Find(x => x.ConnectionId == Context.ConnectionId);
        Connections.Remove(user);
    }

    }

    public class Connection { 
    public string ConnectionId { get; set; }
    public string UserId { get; set; }
    public int PostId { get; set; }
    }
     }

I want to call AddUser and RemoveUser whenever my user enters and leaves a page,but whenever i invoke addUser i get this error:

**[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Invalid argument(s)
and Sometimes : 'GeneralError' is not a subtype of type 'Error' in type cast**

Though the connection is being achieved successfully:

HTTP send: url 'http://ipAddress:8070/CommentHub/?id=XAekJZiTdIqswThHnPHWRQ', method: 'POST' content: '{"type":6}'
I/flutter (17474): FINEST: 2020-04-15 15:54:48.840894: (SSE transport) request complete. Response status: 200.
DCodes
  • 789
  • 1
  • 11
  • 27

1 Answers1

0

This seems to be a problem with the signalR flutter client plugin. I am getting a similar error. I am having minimal code. The error on my side comes from the start function

void startConnection() async {
    hubConnection.start();
}

Therefore, after analyzing the error,i am convinced it is a plugin error. You can check this Issue incase you figure it out.

Hoppo
  • 1,130
  • 1
  • 13
  • 32