通知 SignalR 客户端 IIS 重新启动/关闭

3

我有一个SignalR服务器 (一个Web应用程序) 和一个客户端 (一个控制台应用程序)。

我希望我的客户端在我的服务器的IIS重新启动/关闭或服务器简单地重新启动/关闭时得到通知。

这是可能的吗?


你是指重新启动整个IIS服务器还是只重启你的应用程序的工作进程? - Ali Hasan
作为定时任务的触发器,或者是一个Windows服务? - lloyd
@AliHasan,我认为两者都是相同的,但我更想了解工作进程重启。 - yogi
你不应该先发布你尝试过的内容吗? - matt.
@enki.dev,将其添加为答案。 - yogi
2个回答

4
你可以像这样做
var connection = new HubConnection(hubUrl);
if (configureConnection != null)
    configureConnection(connection);

var proxy = connection.CreateHubProxy("EventAggregatorProxyHub");
connection.Reconnected += reconnected;

https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/blob/master/SignalR.EventAggregatorProxy.Client.DotNet/Bootstrap/Factories/HubProxyFactory.cs#L17

其他事件包括:

  • 重新连接——当连接关闭后尝试重新连接时触发
  • 已关闭——连接丢失时触发

更新:

已关闭将在重新连接失败时被调用(当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;
    }
}

我相信只有在重新启动后 iss 启动时,Reconnected 才能正常工作。我想在它关闭时得到通知。很抱歉问题中没有提到这一点,我会进行更新。 - yogi
看起来 Closed 似乎很有前途,可能有效。谢谢,我会试一下的。如果行的话,我会接受的 :) - yogi
没有,它没有起作用。当我停止我的 ISS 时,重新连接或关闭不会触发。但是,如果我的客户端启动并且 IIS 已经关闭,则会触发 Closed。感谢您的帮助,已点赞。 - yogi
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Anders
不是的,那相当于从命令提示符中运行 iisreset(但更符合实际的做法是重启应用程序池)。 - Anders
显示剩余4条评论

0

目前我唯一的解决方案(虽然很印度式)是尝试在特定时间间隔内重新连接到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 到了,那通常意味着连接出了问题,很可能是 ISS 崩溃了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接