【问题标题】:C# Console Application to test SignalR Groups用于测试 SignalR 组的 C# 控制台应用程序
【发布时间】:2018-01-03 16:02:39
【问题描述】:

我有一个 SignalR 集线器来发送组通知,我想测试 SignalR 组。 以下是 Hub 服务器代码:

[HubName("MyHubServer")]
public class HubServer : Hub
{
    public override Task OnConnected()
    {
        // My code OnConnected
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        // My code OnDisconnected
        return base.OnDisconnected(stopCalled);
    }

    public string JoinGroup(string connectionId, string groupName)
    {
        Groups.Add(connectionId, groupName).Wait();
        return connectionId + " joined " + groupName;
    }

    public string LeaveGroup(string connectionId, string groupName)
    {
        Groups.Remove(connectionId, groupName).Wait();
        return connectionId + " removed " + groupName;
    }

    public string LeaveGroup(string connectionId, string groupName, string customerId = "0")
    {
        Groups.Remove(connectionId, customerId).Wait();
        return connectionId + " removed " + groupName;
    }
}

向群组发送消息的服务器代码(加入群组后我将调用此代码向群组发送消息):

Hub.Clients.Group("Associate").BroadcastAssociateNotification(JsonConvert.SerializeObject(notifications));

现在我想测试 SignalR 服务器组以加入组并在服务器向该组发送消息时接收消息。 下面是我用来测试相同的代码,使用此代码,我在 Groups.Add(connectionId, groupName).Wait(); 行出现“任务已取消”错误

static void Main(string[] args)
{
    MainAsync().Wait();
    Console.ReadLine();
}

static async Task MainAsync()
{
    try
    {
        var hubConnection = new HubConnection("http://localhost:12923/");
        IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("MyHubServer");
        stockTickerHubProxy.On<string>("Associate", data => Console.WriteLine("Notification received : ", data));
        hubConnection.Start().Wait();

        await stockTickerHubProxy.Invoke("JoinGroup", "123456", "Associate");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        Console.ReadLine();
    }
}

【问题讨论】:

  • 您正在混合异步调用和阻塞调用 (.Wait),这可能导致方法中的死锁。你应该等待而不是阻止它。
  • 这个问题解决了吗?

标签: c# asp.net signalr signalr-hub signalr.client


【解决方案1】:

您正在混合异步调用和阻塞调用 (.Wait),这可能导致方法内出现死锁。你应该一直等待而不是阻塞它。

第一个更新 Hub 一直是异步的

[HubName("MyHubServer")]
public class HubServer : Hub {
    public override Task OnConnected() {
        // My code OnConnected
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled) {
        // My code OnDisconnected
        return base.OnDisconnected(stopCalled);
    }

    public async Task<string> JoinGroup(string connectionId, string groupName) {
        await Groups.Add(connectionId, groupName);
        return connectionId + " joined " + groupName;
    }

    public async Task<string> LeaveGroup(string connectionId, string groupName) {
        await Groups.Remove(connectionId, groupName);
        return connectionId + " removed " + groupName;
    }

    public async Task<string> LeaveGroup(string connectionId, string groupName, string customerId = "0") {
        await Groups.Remove(connectionId, customerId);
        return connectionId + " removed " + groupName;
    }
}

接下来,在控制台中执行相同的操作,但主调用除外。

static async Task MainAsync() {
    try {
        var hubConnection = new HubConnection("http://localhost:12923/");
        IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("MyHubServer");
        stockTickerHubProxy.On<string>("Associate", data => Console.WriteLine("Notification received : ", data));

        await hubConnection.Start();

        await stockTickerHubProxy.Invoke("JoinGroup", "123456", "Associate");
    } catch (Exception ex) {
        Console.WriteLine(ex.Message);
        Console.ReadLine();
    }
}

一旦客户端能够建立连接,一切都会按预期运行。

以下自托管示例经过测试表明客户端和集线器之间存在通信。

[HubName("MyHubServer")]
public class HubServer : Hub {
    public override Task OnConnected() {
        // My code OnConnected
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled) {
        // My code OnDisconnected
        return base.OnDisconnected(stopCalled);
    }

    public async Task<string> JoinGroup(string connectionId, string groupName) {
        await Groups.Add(connectionId, groupName);
        var message= connectionId + " joined " + groupName;
        Clients.Group("Associate").Notify(message);
        return message;
    }

    public async Task<string> LeaveGroup(string connectionId, string groupName) {
        await Groups.Remove(connectionId, groupName);
        return connectionId + " removed " + groupName;
    }

    public async Task<string> LeaveGroup(string connectionId, string groupName, string customerId = "0") {
        await Groups.Remove(connectionId, customerId);
        return connectionId + " removed " + groupName;
    }
}

class Program {
    static void Main(string[] args) {
        string url = "http://localhost:8080";
        using (WebApp.Start(url)) {
            Console.WriteLine("Server running on {0}", url);
            MainAsync(url).Wait();
            Console.ReadLine();
        }
    }

    static async Task MainAsync(string url) {
        try {
            var hubConnection = new HubConnection(url);
            IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("MyHubServer");
            stockTickerHubProxy.On<string>("Notify",
                data => Console.WriteLine("Notification received : {0}", data));

            await hubConnection.Start();

            var connectionId = hubConnection.ConnectionId;

            var response = await stockTickerHubProxy.Invoke<string>("JoinGroup", connectionId, "Associate");
            Console.WriteLine("Response received : {0}", response);
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }
    }
}

class Startup {
    public void Configuration(IAppBuilder app) {
        //app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR();
    }
}

【讨论】:

    猜你喜欢
    • 2011-05-12
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 2010-12-25
    相关资源
    最近更新 更多