我一直在努力让我的控制台客户端应用程序连接到我的控制台服务器应用程序。 到目前为止,我得到500,“打开连接时出错...” 我不确定它甚至到了服务器。虽然看起来服务器正在监听 你知道为什么会这样吗?
服务器
我的中心:
[HubName("server")]
public class HubImpl : Hub
{
public string Hello()
{
return "Hello there!";
}
}
我的WebApplication包装器:
public class HubServer : IServer
{
private IDisposable _server;
private ServerConfiguration _configuration;
static HubServer()
{
RouteTables.Initialize();
}
public static IServer Create(ServerConfiguration conf = null)
{
return new HubServer(conf);
}
internal HubServer(ServerConfiguration conf)
{
ApplyConfiguration(conf);
}
private void ApplyConfiguration(ServerConfiguration conf)
{
_configuration = conf ?? new ServerConfiguration();
}
public void Dispose()
{
Stop();
}
public void Start()
{
var url = string.Concat("http://", _configuration.Host, ":", _configuration.Port, "/");
var options = new StartOptions
{
Url = url,
App = GetType().AssemblyQualifiedName
};
_server = WebApplication.Start<Startup>(options);
Console.WriteLine("Server running on {0}", url);
}
public void Stop()
{
_server.Dispose();
}
}
客户端
我的HubConnection包装器:
public class Client : IClient
{
private readonly ClientConfiguration _configuration;
private readonly HubConnection _connection;
private IHubProxy _hubProxy;
public Client(ClientConfiguration conf = null)
{
_configuration = conf ?? new ClientConfiguration();
// open web socket to the server
//Set connection
var url = string.Concat("http://", _configuration.Host, ":", _configuration.Port, "/");
_connection = new HubConnection(url);
Console.WriteLine("Connecting to " + url);
//Make proxy to hub based on hub name on server
_hubProxy = _connection.CreateHubProxy("server");
_connection.StateChanged += change => Console.WriteLine(change.OldState + " => " + change.NewState);
//Start connection
var connectionTask = TryConnecting();
connectionTask.Wait();
int retries = 0;
const int maxRetries = 10;
while (!connectionTask.Result)
{
Thread.Sleep(500);
connectionTask = TryConnecting();
connectionTask.Wait();
retries++;
if (retries == maxRetries) break;
}
}
private Task<bool> TryConnecting()
{
return _connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("There was an error opening the connection:{0}",
task.Exception.GetBaseException());
return false;
}
else
{
Console.WriteLine("Connected");
return true;
}
});
}
}