C# WPF信号服务器返回HTTP 400错误请求(无效主机地址)

C# WPF信号服务器返回HTTP 400错误请求(无效主机地址),c#,wpf,signalr,signalr.client,C#,Wpf,Signalr,Signalr.client,我正在尝试设置一个信号器中心,以便能够通过web将通知推送到一组WPF客户端。我试图遵循基本的指导原则和教程,并创建了一个WPF SignalR服务器(仅用于测试目的)。这已放置在我的LAN中的服务器上,外观如下: Startup.cs class Startup { public void Configuration(IAppBuilder app) { HubConfiguration hc = new HubConfiguration();

我正在尝试设置一个信号器中心,以便能够通过web将通知推送到一组WPF客户端。我试图遵循基本的指导原则和教程,并创建了一个WPF SignalR服务器(仅用于测试目的)。这已放置在我的LAN中的服务器上,外观如下:

Startup.cs

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

        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR(hc);
    }
}
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);
}
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);
    }
}
MainWindow.xaml.cs

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

        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR(hc);
    }
}
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);
}
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);
    }
}
AdHocHub.cs

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

        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR(hc);
    }
}
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);
}
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);
    }
}
服务器启动正常。然而,当我试图将一个客户端连接到它时,它拒绝给我一个400错误的请求。如果我尝试导航到
http://192.nnn.nnn.nnn/signalr
我得到的只是错误的请求-无效的主机名。如果我在同一台机器上运行服务器和客户机,一切正常。我做错了什么

客户端调用的设置如下所示:

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...");
}
private async void ConnectAsync()
{
连接=新的HUB连接(ServerURI);
HubProxy=Connection.CreateHubProxy(“AdHocHub”);
On(“notifyData”,(notifyData)=>this.Dispatcher.Invoke(()=>txtEvents.AppendText(notifyData.ToString()));
尝试
{
等待连接。开始();
}
捕获(HttpRequestException-ex)
{
Show(例如ToString());
lblStatus.Content=“无法连接到服务器:在连接客户端之前启动服务器。”;
返回;
}
AppendText(“已连接到服务器并正在侦听…”);
}
我已经尝试将服务器中的URI从主机名更改为其IP,但是我只得到了一个
targetingException
,所以这没有帮助

如前所述,只要客户机和服务器在同一台机器上运行,整个设置似乎都可以正常工作,但即使我已将
CorsOptions
设置为
AllowAll
(将我自己的注释转换为答案,因为在大多数情况下它似乎是答案),我也不会移动它


将服务器中的
ServerURI
localhost
更改为
http://+:8554
,否则它将只侦听localhost。它在同一台机器上工作,因为它的本地主机


但一旦您将其更改为此新方案,在调试时,您将需要visual studio在管理模式下运行,否则,如果UAC处于启用状态,将引发
targetingException

将服务器中的
ServerURI
从localhost更改为
http://+:8554
。否则它将只侦听localhost。它在同一台机器上工作,因为它的
localhost
。但是,一旦您将其更改为此新方案,您将需要visual studio在管理模式下运行,否则,如果UAC处于启用状态,则会引发
targetingException
。这非常有效。非常感谢你帮我解决了这个头疼的问题。将其添加为答复,我将检查它是否已解决。:)@Mathew运行进程的用户需要什么样的权限才能承载到
http://+:port
地址?@DLeh您可以允许非管理员侦听,因为我遇到了与作为服务运行的服务器相同的问题。我在本地主机上使用了.LocalService。当我将它移动到远程服务器时,我忽略了这一点。最后,我使用了一个带有privs的.User帐户来完成作业,并使用http://+:使其正确运行。