我有一个SignalR服务器(一个Web应用程序)和一个客户端(控制台应用程序)。通知SignalR IIS重新启动/关闭客户端
我希望我的客户端在我的服务器的IIS重启/关闭或服务器重启/关闭时得到通知。
可能吗?
我有一个SignalR服务器(一个Web应用程序)和一个客户端(控制台应用程序)。通知SignalR IIS重新启动/关闭客户端
我希望我的客户端在我的服务器的IIS重启/关闭或服务器重启/关闭时得到通知。
可能吗?
你可以像下面这样做
var connection = new HubConnection(hubUrl);
if (configureConnection != null)
configureConnection(connection);
var proxy = connection.CreateHubProxy("EventAggregatorProxyHub");
connection.Reconnected += reconnected;
其他事件
更新:在重新连接失败
Closed
将被调用(当IIS已连续比重新连接超时接受更长的时间)。
这意味着您应该使用connection.Start()
在关闭事件失败时重新连接,关闭事件将再次被调用,并且可以再次使用connection.Start()
重试。
下面是一个使用我的代码的一个例子,它才能生存,这两个IIS正在下降时,应用程序启动和它的股价下跌,同时运行
public class HubProxyFactory : IHubProxyFactory
{
public IHubProxy Create(string hubUrl, Action<IHubConnection> configureConnection, Action<IHubProxy> onStarted, Action reconnected, Action<Exception> faulted, Action connected)
{
var connection = new HubConnection(hubUrl);
if (configureConnection != null)
configureConnection(connection);
var proxy = connection.CreateHubProxy("EventAggregatorProxyHub");
connection.Reconnected += reconnected;
connection.Error += faulted;
var isConnected = false;
Action start =() =>
{
Task.Factory.StartNew(() =>
{
try
{
connection.Start().Wait();
if(isConnected)
reconnected();
else
{
isConnected = true;
onStarted(proxy);
connected();
}
}
catch(Exception ex)
{
faulted(ex);
}
});
};
connection.Closed += start;
start();
return proxy;
}
}
我截至目前唯一的解决办法(非常印度jubaad的)是每次在特定的时间间隔内尝试重新连接到SignalR服务器。
HubConnection _connection = new HubConnection(Properties.Settings.Default.UserLoginDataSource);
IHubProxy _hub;
void TryConnectingDataSource()
{
try
{
_connection.Stop();
_connection.Dispose();
_connection = new HubConnection(Properties.Settings.Default.UserLoginDataSource);
_connection.StateChanged += connection_StateChanged;
_hub = _connection.CreateHubProxy("myHub");
_connection.Start().Wait();
if (_connection.State == ConnectionState.Connected)
{
_hub.On("myHubCallback", new Action<Dictionary<string, Entities.Permissions.UserTypes>>(GetUserLoginList));
_hub.Invoke("myHubMethod");
}
}
catch (Exception x)
{
EventLogger.LogError(x);
}
}
如果我会被Catch抓住,那将意味着Connection出现问题,最有可能的是ISS关闭。
你的意思是重新启动整个IIS服务器或只是你的应用程序的工作进程? –
作为计划任务的触发器,或[Windows服务](http://forums.iis.net/t/1180869.aspx?IIS+Windows+Service)? – lloyd
@AliHasan,我相信两者都是一样的,但我更关注工作流程的重启。 – yogi