Reputation: 397
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;
}
}
Upvotes: 1
Views: 1721
Reputation: 61379
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
Upvotes: 1