Microsoft.R.Host.Client.Session.RSessionProvider.TrySwitchBrokerAsync C# (CSharp) Méthode

TrySwitchBrokerAsync() public méthode

public TrySwitchBrokerAsync ( string name, string path = null, CancellationToken cancellationToken = default(CancellationToken) ) : Task
name string
path string
cancellationToken System.Threading.CancellationToken
Résultat Task
        public async Task<bool> TrySwitchBrokerAsync(string name, string path = null, CancellationToken cancellationToken = default(CancellationToken)) {
            using (_disposeToken.Link(ref cancellationToken)) {
                await TaskUtilities.SwitchToBackgroundThread();

                var brokerClient = CreateBrokerClient(name, path, cancellationToken);
                if (brokerClient == null) {
                    return false;
                }

                // Broker switching shouldn't be concurrent
                IAsyncReaderWriterLockToken lockToken;
                try {
                    lockToken = await _connectArwl.WriterLockAsync(cancellationToken);
                } catch (OperationCanceledException) {
                    brokerClient.Dispose();
                    return false;
                }

                if (brokerClient.Name.EqualsOrdinal(_brokerProxy.Name) &&
                    brokerClient.Uri.AbsoluteUri.PathEquals(_brokerProxy.Uri.AbsoluteUri)) {

                    brokerClient.Dispose();

                    try {
                        // Switching to the broker that is currently running and connected is always successful
                        if (IsConnected) {
                            return true;
                        }

                        await ReconnectAsync(cancellationToken);
                    } catch (Exception) {
                        return false;
                    } finally {
                        lockToken.Dispose();
                    }

                    IsConnected = true;
                    return true;
                }

                // First switch broker proxy so that all new sessions are created for the new broker
                var oldBroker = _brokerProxy.Set(brokerClient);
                if (_updateHostLoadLoopTask == null) {
                    _updateHostLoadLoopTask = UpdateHostLoadLoopAsync();
                }

                try {
                    BrokerChanging?.Invoke(this, EventArgs.Empty);
                    await SwitchBrokerAsync(cancellationToken);
                    oldBroker.Dispose();
                } catch (Exception ex) {
                    _brokerProxy.Set(oldBroker);
                    if (_disposeToken.IsDisposed) {
                        oldBroker.Dispose();
                    }
                    brokerClient.Dispose();
                    BrokerChangeFailed?.Invoke(this, EventArgs.Empty);
                    if (ex is OperationCanceledException || ex is RHostBrokerBinaryMissingException) {
                        // RHostDisconnectedException is derived from OperationCanceledException
                        return false;
                    }
                    throw;
                } finally {
                    lockToken.Dispose();
                }

                IsConnected = true;
                OnBrokerChanged();
                return true;
            }
        }

Usage Example

Exemple #1
0
        public async Task ValidateEncodings() {
            var etc = new EncodingTypeConverter();
            var codePages = etc.GetStandardValues();
            using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) {
                await sessionProvider.TrySwitchBrokerAsync(nameof(ValidateEncodings));
                using (var script = new VsRHostScript(sessionProvider)) {
                    foreach (var cp in codePages) {
                        if ((int)cp > 0) {
                            var expression = Invariant($"Sys.setlocale('LC_CTYPE', '.{cp}')\n");
                            using (var inter = await script.Session.BeginInteractionAsync()) {
                                await inter.RespondAsync(expression);
                            }

                            var res = await script.Session.EvaluateAsync("Sys.getlocale()", REvaluationKind.Normal);
                            var s = res.Result.ToString();

                            s.Should().NotBeNull().And.Contain(cp.ToString());
                        }
                    }
                }
            }
        }
All Usage Examples Of Microsoft.R.Host.Client.Session.RSessionProvider::TrySwitchBrokerAsync