Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF SignalR server returns HTTP 400 Bad Request (Invalid host address)

I'm trying to set up a SignalR hub to be able to push notifications to a bunch of WPF clients over the web. I've tried to follow the basic guidelines and tutorials and have created a WPF SignalR Server (for testing purposes only). This has been put on a server in my LAN and this is how it looks:

Startup.cs

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        HubConfiguration hc = new HubConfiguration();
        hc.EnableDetailedErrors = true;

        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR(hc);
    }
}

MainWindow.xaml.cs

public IDisposable SignalR { get; set; }
const string ServerURI = "http://localhost:8554";

private void StartServer()
{
    try
    {
        SignalR = WebApp.Start(ServerURI);
    }
    catch (TargetInvocationException)
    {
        WriteToConsole("A server is already running at " + ServerURI);
        return;
    }
    this.Dispatcher.Invoke(() => btnStopHub.IsEnabled = true);
}

AdHocHub.cs

public class AdHocHub : Hub
{
    public void Send(string data)
    {
        Clients.All.notifyData(data);
    }

    public override Task OnConnected()
    {
        Application.Current.Dispatcher.Invoke(() => 
            ((MainWindow)Application.Current.MainWindow).WriteToConsole("Client connected: " + Context.ConnectionId));
        return base.OnConnected();
    }

    public Task OnDisconnected()
    {
        Application.Current.Dispatcher.Invoke(() =>
            ((MainWindow)Application.Current.MainWindow).WriteToConsole("Client disconnected: " + Context.ConnectionId));

        return base.OnDisconnected(true);
    }
}

The server starts just fine. However when I try to connect a client to it it refuses giving me a 400 - Bad Request. If I try to navigate to http://192.nnn.nnn.nnn/signalr all I get is Bad Request - Invalid Hostname. If I run the server and the client on the same machine Everything works just as it should. What am I doing wrong here?

The client calls are set up like this:

private async void ConnectAsync()
{
    Connection = new HubConnection(ServerURI);
    HubProxy = Connection.CreateHubProxy("AdHocHub");
    HubProxy.On<string>("notifyData", (notifyData) => this.Dispatcher.Invoke(() => txtEvents.AppendText(notifyData.ToString())));

    try
    {
        await Connection.Start();
    }
    catch (HttpRequestException ex)
    {
        MessageBox.Show(ex.ToString());
        lblStatus.Content = "Unable to connect to server: Start server before connecting clients.";
        return;
    }
    txtEvents.AppendText("Connected to server and listening...");
}

I've tried changing the URI in the server from hostname to its IP, but then I just get a TargetInvocationException, so that won't help.

As stated earlier, this entire setup seems to work fine so long as the client and server runs on the same machine, but not once I move it even though I've set the CorsOptions to AllowAll.

like image 283
JaggenSWE Avatar asked Feb 03 '15 13:02

JaggenSWE


1 Answers

(Converting my own comment to an answer since it seems to be the answer in most cases)

Change ServerURI in server from localhost to http://+:8554, else it will listen for localhost only. It works in same machine because its localhost.

But as soon as you change it to this new scheme, while debugging, you'll need visual studio running in admin mode or else TargetInvocationException will be thrown if UAC is on.

like image 76
Mat J Avatar answered Oct 13 '22 01:10

Mat J