SignalR WPF Client can't reach hub deployed on IIS when IIS runs on a different system

落花浮王杯 提交于 2019-12-12 11:32:14

问题


I just play a little bit with signalR. My application has only one simple hub which is stored in an ASP.NET Application and I wrote a WPF client, which interacts via the hubconnection and the created proxy with the ASP.NET Application. Everything works fine on my local PC. I deployed the ASP.NET Application on IIS.

Now I am getting to the point...

When I type the following into my browser on my own PC (pcthi-and)

http://pcthi-and:8080/signalr/hubs

I'll get what I want

When I type the same url into a browser of another pc I'll get the same response and everything looks fine.

But my Application only works on my pc and not on the other one. When I start the hubconnection on the other pc I don't get a connectionId.

I tried to change the url to my IP-Address without effect.

Browser call to hub works but the Application doesn't work.

The call looks like this:

private bool tryToConnectToCoffeService()
    {
        try
        {
            this.hubConnection = new HubConnection(ConfigurationManager.ConnectionStrings["coffeeConnection"].ConnectionString);

            this.hubConnection.Credentials = CredentialCache.DefaultNetworkCredentials;

            this.coffeeService = this.hubConnection.CreateHubProxy("coffee");

            this.hubConnection.Start();

            if (string.IsNullOrEmpty(hubConnection.ConnectionId))
            {
                return false;
            }

            return true;
        }
        catch(Exception ex)
        {
            return false;
        }
    }

The Global.asax:

public class Global : System.Web.HttpApplication
{

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapHubs();
    }

The hub like this

[HubName("coffee")]
public class CoffeeHub : Hub
{

My Hub Connection String is this:

"http://pcthi-and:8080/"

Or:

"http://My-Current-IP-Address:8080/"

I use SignalR 1.0 rc2.

Does anyone have an idea? Thanks for helping.

Cheers

Frank


回答1:


I think you need to change

hubConnection.Start();

to

hubConnection.Start().Wait();

If you are running .NET 4.5 you could make the tryToConnectToCoffeService method async and then await when you start the hub connection.

await hubConnection.Start();

It likely works today on localhost because the client can finish connecting before if (string.IsNullOrEmpty(hubConnection.ConnectionId)) executes.

It is probably taking longer to connect from another machine which exposes the race condition present when you don't wait for HubConnection.Start() to complete.



来源:https://stackoverflow.com/questions/14836065/signalr-wpf-client-cant-reach-hub-deployed-on-iis-when-iis-runs-on-a-different

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!