必须先调用SignalR控制台应用程序启动才能发送数据

时间:2012-11-30 10:17:27

标签: c# asp.net-mvc signalr

我正在尝试创建一个客户端信号器应用程序。目前,我能够让我的MVC客户端向Hub发送消息,但是,我的.NET客户端应用程序遇到了一些困难

服务器中心代码:

namespace ServerHub
{
    public class ChatterBox : Hub
    {

        public void Send(string message)
        {
            Clients.All.addMessage(message);
        }
    }
}

控制台应用程序代码(几乎直接从github取消)

using Microsoft.AspNet.SignalR.Client.Hubs;
using ServerHub;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
          Say("hello from console");
        }
        public static void Say(string message)
        {
            //var connection = new HubConnection("http://localhost/");

            //IHubProxy myHub = connection.CreateHubProxy("ChatterBox");

            var connection = new HubConnection("http://localhost/");
            //Make proxy to hub based on hub name on server
            var myHub = connection.CreateHubProxy("ChatterBox");
            //Start connection
            connection.Start().ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
                }
                else
                {
                    Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
                    // Do more stuff here
                }
            });

            connection.Send("Hello").ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("Send failed {0}", task.Exception.GetBaseException());
                }
                else
                {
                    Console.WriteLine("Success");
                }
            });
        }
    }
}

我在connection.Send()

期间收到以下错误消息
  

必须先调用Start才能发送数据。

我哪里出错了?

编辑:

第二次尝试:

    var connection = new HubConnection("http://localhost/");
    //Make proxy to hub based on hub name on server
    var myHub = connection.CreateHubProxy("ChatterBox");
    //Start connection
    connection.Start().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
            }
            else
            {
                Console.WriteLine("Success! Connected with client connection id {0}",
                                  connection.ConnectionId);

                // Do more stuff here

                connection.Send("Hello");
            }
        });


    myHub.Invoke("Send", "lol");

仍然给我一个错误

编辑:第三次尝试

            var connection = new HubConnection("http://localhost:60610/");
            //Make proxy to hub based on hub name on server
            var myHub = connection.CreateHubProxy("ChatterBox");
            //Start connection
            connection.Start().ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
                }
                else
                {
                    //Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);

                    myHub.Invoke("Send", "lol");
                }
            });

似乎正在运作

我还需要设置端口号 - 例如,如果我正在浏览我的MVC网站:http://localhost:60610/,这是我需要在我的控制台应用中设置的地址。

我可以说在部署期间,这不会是一个问题,因为它默认是端口80吗?

1 个答案:

答案 0 :(得分:5)

阅读此http://msdn.microsoft.com/en-us/library/dd460717.aspx

TL; DR Start是异步的,而不是你编写顺序异步代码的方式:

    var connection = new HubConnection("http://localhost/");
    //Make proxy to hub based on hub name on server
    var myHub = connection.CreateHubProxy("ChatterBox");
    //Start connection
    connection.Start().ContinueWith(task =>
    {
        if (task.IsFaulted)
        {
            Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
        }
        else
        {
            Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);

            // Do more stuff here

            connection.Send("Hello").ContinueWith(task =>
            {
                 .......
            });
        }
    });

如果您可以使用.NET 4.5,它将使您的生活更轻松:

var connection = new HubConnection("http://localhost/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
try 
{
    await connection.Start();
    Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
}
catch(Exception ex) 
{
    Console.WriteLine("Failed to start: {0}", ex.GetBaseException());
}

await connection.Send("Hello");

更新:让我们再试一次。下面的代码可以工作,但您应该阅读该链接以了解如何编写异步代码。否则你会一遍又一遍地遇到这些问题。

var connection = new HubConnection("http://localhost/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
connection.Start().ContinueWith(task =>
    {
        if (task.IsFaulted)
        {
            Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
        }
        else
        {
            Console.WriteLine("Success! Connected with client connection id {0}",
                              connection.ConnectionId);

            myHub.Invoke("Send", "lol");
        }
    });