Exemple #1
0
        private async Task ExpandNodeAsync(ObjectExplorerSession session, ExpandParams expandParams, CancellationToken cancellationToken, bool forceRefresh = false)
        {
            ExpandResponse response = null;

            response = await ExpandNode(session, expandParams.NodePath, forceRefresh);

            if (cancellationToken.IsCancellationRequested)
            {
                Logger.Write(LogLevel.Verbose, "OE expand canceled");
            }
            else
            {
                await serviceHost.SendEvent(ExpandCompleteNotification.Type, response);
            }
        }
        internal ExpandResponse QueueExpandNodeRequest(ObjectExplorerSession session, string nodePath, bool forceRefresh = false)
        {
            NodeInfo[]     nodes    = null;
            TreeNode       node     = session.Root.FindNodeByPath(nodePath);
            ExpandResponse response = new ExpandResponse {
                Nodes = new NodeInfo[] { }, ErrorMessage = node.ErrorMessage, SessionId = session.Uri, NodePath = nodePath
            };

            if (node != null && Monitor.TryEnter(node.BuildingMetadataLock, LanguageService.OnConnectionWaitTimeout))
            {
                try
                {
                    int       timeout   = (int)TimeSpan.FromSeconds(settings?.ExpandTimeout ?? ObjectExplorerSettings.DefaultExpandTimeout).TotalMilliseconds;
                    QueueItem queueItem = bindingQueue.QueueBindingOperation(
                        key: bindingQueue.AddConnectionContext(session.ConnectionInfo, connectionName),
                        bindingTimeout: timeout,
                        waitForLockTimeout: timeout,
                        bindOperation: (bindingContext, cancelToken) =>
                    {
                        if (forceRefresh)
                        {
                            nodes = node.Refresh().Select(x => x.ToNodeInfo()).ToArray();
                        }
                        else
                        {
                            nodes = node.Expand().Select(x => x.ToNodeInfo()).ToArray();
                        }
                        response.Nodes        = nodes;
                        response.ErrorMessage = node.ErrorMessage;
                        return(response);
                    });

                    queueItem.ItemProcessed.WaitOne();
                    if (queueItem.GetResultAsT <ExpandResponse>() != null)
                    {
                        response = queueItem.GetResultAsT <ExpandResponse>();
                    }
                }
                catch
                {
                }
                finally
                {
                    Monitor.Exit(node.BuildingMetadataLock);
                }
            }
            return(response);
        }
Exemple #3
0
        private async Task <ExpandResponse> CallServiceExpand(ExpandParams expandParams, RequestContext <bool> requestContext)
        {
            ExpandResponse result = null;

            serviceHostMock.AddEventHandling(ExpandCompleteNotification.Type, (et, p) => result = p);

            await service.HandleExpandRequest(expandParams, requestContext);

            Task task = service.ExpandTask;

            if (task != null)
            {
                await task;
            }
            return(result);
        }
Exemple #4
0
        private void RunExpandTask(ObjectExplorerSession session, ExpandParams expandParams, bool forceRefresh = false)
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            Task task = ExpandNodeAsync(session, expandParams, cancellationTokenSource.Token, forceRefresh);

            ExpandTask = task;
            Task.Run(async() =>
            {
                ObjectExplorerTaskResult result = await RunTaskWithTimeout(task,
                                                                           settings?.ExpandTimeout ?? ObjectExplorerSettings.DefaultExpandTimeout);

                if (result != null && !result.IsCompleted)
                {
                    cancellationTokenSource.Cancel();
                    ExpandResponse response = CreateExpandResponse(session, expandParams);
                    response.ErrorMessage   = result.Exception != null ? result.Exception.Message: $"Failed to expand node: {expandParams.NodePath} in session {session.Uri}";
                    await serviceHost.SendEvent(ExpandCompleteNotification.Type, response);
                }
                return(result);
            }).ContinueWithOnFaulted(null);
        }
Exemple #5
0
        internal ExpandResponse QueueExpandNodeRequest(ObjectExplorerSession session, string nodePath, bool forceRefresh = false)
        {
            NodeInfo[]     nodes    = null;
            TreeNode       node     = session.Root.FindNodeByPath(nodePath);
            ExpandResponse response = new ExpandResponse {
                Nodes = new NodeInfo[] { }, ErrorMessage = node.ErrorMessage, SessionId = session.Uri, NodePath = nodePath
            };

            if (node != null && Monitor.TryEnter(node.BuildingMetadataLock, LanguageService.OnConnectionWaitTimeout))
            {
                try
                {
                    int       timeout   = (int)TimeSpan.FromSeconds(settings?.ExpandTimeout ?? ObjectExplorerSettings.DefaultExpandTimeout).TotalMilliseconds;
                    QueueItem queueItem = bindingQueue.QueueBindingOperation(
                        key: bindingQueue.AddConnectionContext(session.ConnectionInfo, connectionName),
                        bindingTimeout: timeout,
                        waitForLockTimeout: timeout,
                        bindOperation: (bindingContext, cancelToken) =>
                    {
                        if (forceRefresh)
                        {
                            nodes = node.Refresh().Select(x => x.ToNodeInfo()).ToArray();
                        }
                        else
                        {
                            nodes = node.Expand().Select(x => x.ToNodeInfo()).ToArray();
                        }
                        response.Nodes        = nodes;
                        response.ErrorMessage = node.ErrorMessage;
                        try
                        {
                            // SMO changes the database when getting sql objects. Make sure the database is changed back to the original one
                            if (bindingContext.ServerConnection.CurrentDatabase != bindingContext.ServerConnection.DatabaseName)
                            {
                                bindingContext.ServerConnection.SqlConnectionObject.ChangeDatabase(bindingContext.ServerConnection.DatabaseName);
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Write(LogLevel.Warning, $"Failed to change the database in OE connection. error: {ex.Message}");
                            // We should just try to change the connection. If it fails, there's not much we can do
                        }
                        return(response);
                    });

                    queueItem.ItemProcessed.WaitOne();
                    if (queueItem.GetResultAsT <ExpandResponse>() != null)
                    {
                        response = queueItem.GetResultAsT <ExpandResponse>();
                    }
                }
                catch
                {
                }
                finally
                {
                    Monitor.Exit(node.BuildingMetadataLock);
                }
            }
            return(response);
        }