SignalR客户端连接尚未建立

时间:2015-09-18 20:34:20

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

我创建了一个SignalR Selfhosted应用程序但无法通过浏览器或Windows Phone(我的客户端)访问。我遵循了几个教程,他们几乎说同样的事情,我相信我的网络配置上的错误也注意到当你运行项目时,IIS不会触发(不确定是否也是必需的)。

SignalRServer:

class Program : Hub
    {
        private static HubConnection Connection { get; set; }
        private static IHubProxy HubProxy { get; set; }

        const string Url = "http://*:8080";

        static void Main(string[] args)
        {
            // This will *ONLY* bind to localhost, if you want to bind to all addresses
            // use http://*:8080 to bind to all addresses. 
            // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx 
            // for more information.

            using (WebApp.Start(Url))
            {
                Console.WriteLine("Servidor rodando em {0}", Url);
                Console.ReadLine();
            }
        }

        private static void Connection_Closed()
        {
            Connection = null;
        }
    }

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

错误是:

  

尚未建立连接。

我的客户端类:

public class SignalRService : PhoneApplicationPage
    {
        public String UserName { get; set; }

        public IHubProxy HubProxy { get; set; }

        private const string ServerURI = "http://localhost:8080";

        public HubConnection Connection { get; set; }

        public SignalRService()
        {
            UserName = "Luizaooo";

            ConnectAsync();
        }

        public void ObterAtualizacoesProdutos()
        {
            ProdutoService produtoService = new ProdutoService();
            var dataHora = produtoService.ObterDataHoraUltimaAtualizacao();
            HubProxy.Invoke("ObterAtualizacoes", dataHora);
            MessageBox.Show("passou");
        }

        private async void ConnectAsync()
        {
            Connection = new HubConnection(ServerURI);
            Connection.Closed += Connection_Closed;
            HubProxy = Connection.CreateHubProxy("ProdutoHub");

            HubProxy.On<List<Produto>>("AtualizarProdutos", (ListaDeProdutos) =>
                this.Dispatcher.BeginInvoke(() =>
                {
                    var a = ListaDeProdutos;
                }));

            try
            {
                await Connection.Start();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return;
            }
        }

        private void Connection_Closed()
        {
            Connection = null;
        }
    }

1 个答案:

答案 0 :(得分:1)

正确识别Startup类可能很棘手,尤其是在自托管方案中。

对于您的代码,首先检查Configuration方法是否实际运行。如果不是,请添加

[assembly: OwinStartup(typeof(Program.Startup))]

命名空间上面的

,如下所述:Owin Startup Detection

您可以使用带有谓词的WebApp.Start重载完全绕过此问题:

 WebApp.Start(url, new Action<IAppBuilder>((app) =>
 {
     app.UseCors(CorsOptions.AllowAll);
     app.MapSignalR();
 }));

无论哪种方式,都可以导航到http://localhost:8080/signalr/hubs来测试您的连接。如果它显示signalR javascript文件,您将知道您的服务已启动并正在运行

相关问题