예제 #1
0
        private async Task <string> InitConnectionAsync()
        {
            if (_connection == null)
            {
                using (await _lockInit.LockAsync())
                {
                    if (_connection == null)
                    {
                        if (_options.SignalRHubUrl != null)
                        {
                            _connection = new HubConnectionBuilder()
                                          .WithUrl(_options.SignalRHubUrl)
                                          .Build();
                            _notifier = new HubCallBackNotifier(_connection);
                            _connection.On <string, string>("Callback", HubConnection_Callback);
                        }
                    }
                }
            }
#if NETCOREAPP2_1
            try
            {
                await _connection.StartAsync();

                _connectionId = await _connection.InvokeAsync <string>("GetConnectionId");

                return(_connectionId);
            }
            catch (Exception e)
            {
                _logger.LogWarning(e, "_connection.StartAsync() failed.");
            }
#else
            // ReSharper disable once PossibleNullReferenceException
            if (_connection.State == HubConnectionState.Disconnected)
            {
                using (await _lockInit.LockAsync())
                {
                    if (_connection.State == HubConnectionState.Disconnected)
                    {
                        try
                        {
                            await _connection.StartAsync();

                            _connectionId = await _connection.InvokeAsync <string>("GetConnectionId");

                            return(_connectionId);
                        }
                        catch (Exception e)
                        {
                            _logger.LogWarning(e, "_connection.StartAsync() failed.");
                        }
                    }
                }
            }
#endif
            return(_connectionId);
        }
예제 #2
0
 public HttpClientOnceApiConvert(string apiUrl, string connectionId, HubCallBackNotifier notifier, int timeoutInterval)
 {
     _apiUrl          = apiUrl;
     _connectionId    = connectionId;
     _notifier        = notifier;
     _timeoutInterval = timeoutInterval + 1000 * 5;
     if (_notifier != null)
     {
         _notifier.Callback += Notifier_Callback;
     }
 }