简体   繁体   English

SignalR C#客户端未调用方法

[英]SignalR C# Client not calling a method

I created a server with SignalR and SQLTableDependency. 我用SignalR和SQLTableDependency创建了一个服务器。 After that, I created a project with Vue and SignalR Javascript Client and everything works, the notification subscription in the server execute a SignalR method to send the object to all the Clients 之后,我使用Vue和SignalR Javascript Client创建了一个项目,并且一切正常,服务器中的通知订阅执行SignalR方法将对象发送给所有客户端

private void Changed(object sender, RecordChangedEventArgs<Todo> eventArgs)
    {
        if(eventArgs.ChangeType != TableDependency.SqlClient.Base.Enums.ChangeType.None)
        {
            var changedEntity = eventArgs.Entity;
            var mensaje = TipoCambios(eventArgs);
            _hubContext.Clients.All.SendAsync("RegistrarTarea", changedEntity);
        }
    }

In JavaScript Client I made this: 在JavaScript客户端中,我这样做了:

coneccionTodo.on("RegistrarTarea", todos => {
    this.$refs.alerta.Abrir(todos.cambio, "info", "Alerta");
    console.log(todos);
  });
  coneccionTodo
    .start()
    .then(response => {
      this.sinConexion = false;
    })
    .catch(error => {
      console.log("Error Todo SignalR", error.toString());
    });

The result of that is this: 结果是:

在此处输入图片说明

And finally my C# Client made with .Net Core 2.1. 最后,我的C#客户端使用.Net Core 2.1。 This is not working 这不行

public static async Task Ejecutar() {
    connection.On<List<dynamic>>("RegistrarTarea", (objects) => {
        Console.WriteLine(objects);
    });
    try
    {                                
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine($"Conexión exitosa a {url}");
        await connection.StartAsync();
        //await connection.InvokeAsync("RegistrarTarea", "Consola", true);
    }
    catch (Exception ex)
    {
        SignalR_Exception(ex);
    }
}

In void main Console app I call the Ejecutar method: void main控制台应用程序中,我调用Ejecutar方法:

    connection = new HubConnectionBuilder().WithUrl(url).Build();
    connection.Closed += async (error) => {
        await Task.Delay(new Random().Next(0, 5) * 1000);
        await connection.StartAsync();
    };
    Task.Run(() => Ejecutar());
    Console.ReadLine();

NOTE: In the server, the CORS are activated to allow anything. 注:在服务器中,CORS被激活以允许任何操作。

Are you using Direct mode ? 您正在使用直接模式吗? The direct mode does not function with this. 直接模式不适用于此功能。 Turn the Direct mode off. 关闭直接模式。

Ok, in connection.on I use a List, but instead of that, I used a Class with the properties like the server send. 好的,在connection.on我使用列表,但取而代之的是,我使用具有服务器发送等属性的Class。 So now, it's working: 所以现在,它正在工作:

    connection.On<Result>("RegistrarTarea", (result) => {                
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine(result.Cambio);
    });

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM