1

I created a SignalR Selfhosted application but can not access via browsers or Windows Phone (which is my client). I followed several tutorials and they pretty much say the same thing, I believe that the error is on my network configuration also notice that when you run the project, the IIS does not fire (not sure if it is also required).

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();
        }
    }

The error is:

The connection has not been established.

My Client class:

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 Answer 1

1

Getting the Startup class to be recognized correctly can be tricky, especially in self-host scenarios.

For your code, first check if the Configuration method is actually running. If it isn't, add

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

above the namespace decleration as described here: Owin Startup Detection

You can bypass this problem entirely by using the overload of WebApp.Start that takes a predicate:

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

Either way, test your connection by navigating to http://localhost:8080/signalr/hubs . If that shows you a signalR javascript file, you will know that your service is up and running

4
  • I'm giving start on windows phone project and the emulator can not access the address ... localhost:8080 or an ip setted directly. however testing the url that vc spoke, he asks to download a file, should be the JS you spoke, the browser appears JS properly. But to my misfortune, I just have a network problem and I can not test in a real way. Sep 18, 2015 at 22:02
  • @LuizNegrini You might want to check your apps manifest to see if you are requiring network permissions for it Sep 18, 2015 at 22:04
  • with javascript appearing in signalr/hubs does not mean that the project is set to asp.net mvc? Because my client project is windows phone silverlight. Sep 18, 2015 at 22:14
  • @LuizNegrini The javascript appearing means that your server is hosted correctly and listening for connections. It can be used by a website to generate a proxy, but is otherwise just telling you what is working. Unfortunately I can't see any obvious reason your phone project is failing. Sep 18, 2015 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.