Пример #1
0
        private static IHubProxy InitializeHubConnection(IReadOnlyConfiguration configuration, Action <string, int?, FunctionTypeEnum, bool, string, Action <string> > handler, out DisposalQueue toDispose, Action <string> logger)
        {
            toDispose = new DisposalQueue();

            var hubConnection = new HubConnection(configuration["Settings:WebAddress"],
                                                  new Dictionary <string, string>
            {
                {
                    configuration["Settings:SignalREngineFlagName"], true.ToString().ToLowerInvariant()
                }
            });

            hubConnection.StateChanged += change => logger($"InitializeHubConnection: StateChanged: '{change.OldState}' -> '{change.NewState}'!");

            hubConnection.JsonSerializer.Converters.Add(new NullConverter()); // handle NULLs

            var hubProxy = hubConnection.CreateHubProxy(configuration["Settings:SignalRHubName"]);

            toDispose.Enqueue(hubProxy.On <string, int?, FunctionTypeEnum, string>("sendMessage",
                                                                                   (clientID, moduleID, functionType, text)
                                                                                   => handler(clientID, moduleID, functionType, false, text, logger)));

            toDispose.Enqueue(hubProxy.On <string, int?, FunctionTypeEnum>("sendQuery",
                                                                           (clientID, moduleID, functionType)
                                                                           => handler(clientID, moduleID, functionType, true, null, logger)));

            try
            {
                hubConnection.Start().Wait();
            }
            catch (Exception e)
            {
                var message = $"InitializeHubConnection: ERROR:{Environment.NewLine}Exception '{typeof(Exception).Name}' occurred while initializing hub connection: {e.Message}.";
                if (e.InnerException != null)
                {
                    message += Environment.NewLine
                               + $"Inner exception '{e.InnerException.GetType().Name}': {e.InnerException.Message}.";
                }

                logger(message);

                toDispose = null;
                return(null);
            }

            logger($"InitializeHubConnection: Connection to hub started with ID '{hubConnection.ConnectionId}'!");

            toDispose.Enqueue(hubConnection);

            return(hubProxy);
        }
Пример #2
0
 /// <summary>
 ///     Relinquishes ownership of the lookup and adds it to the disposal queue.
 /// </summary>
 public void DisposeInBackground()
 {
     DisposalQueue.Enqueue(this);
 }